forked from terrysimons/spine-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkeletonJson.py
More file actions
256 lines (203 loc) · 10.8 KB
/
SkeletonJson.py
File metadata and controls
256 lines (203 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import json
import os
import sys
import SkeletonData
import BoneData
import SlotData
import Skin
import AttachmentLoader
import Animation
def readCurve(timeline, keyframeIndex, valueMap):
try:
curve = valueMap['curve']
except KeyError:
return timeline
if curve == 'stepped':
timeline.setStepped(keyframeIndex)
else:
timeline.setCurve(keyframeIndex,
float(curve[0]),
float(curve[1]),
float(curve[2]),
float(curve[3]))
return timeline
class SkeletonJson(object):
def __init__(self, attachmentLoader):
super(SkeletonJson, self).__init__()
self.attachmentLoader = attachmentLoader
self.scale = 1.0
self.flipY = False
def readSkeletonDataFile(self, file, path=None):
if path:
file = '{path}/{file}'.format(path=path, file=file)
file = os.path.realpath(file)
jasonPayload = None
with open(file, 'r') as jsonFile:
jsonPayload = ''.join(jsonFile.readlines())
return self.readSkeletonData(jsonPayload=jsonPayload)
def readSkeletonData(self, jsonPayload):
try:
root = json.loads(jsonPayload)
except ValueError:
if os.path.isfile(jsonPayload):
print('The API has changed. You need to load skeleton data with readSkeletonDataFile(), not readSkeletonData()')
sys.exit()
skeletonData = SkeletonData.SkeletonData()
for boneMap in root.get('bones', []):
boneData = BoneData.BoneData(name=boneMap['name'])
if 'parent' in boneMap:
boneData.parent = skeletonData.findBone(boneMap['parent'])
if not boneData.parent:
raise Exception('Parent bone not found: %s' % boneMap['name'])
boneData.length = float(boneMap.get('length', 0.0)) * self.scale
boneData.x = float(boneMap.get('x', 0.0)) * self.scale
boneData.y = float(boneMap.get('y', 0.0)) * self.scale
boneData.rotation = float(boneMap.get('rotation', 0.0))
boneData.scaleX = float(boneMap.get('scaleX', 1.0))
boneData.scaleY = float(boneMap.get('scaleY', 1.0))
skeletonData.bones.append(boneData)
for slotMap in root.get('slots', []):
slotName = slotMap['name']
boneName = slotMap['bone']
boneData = skeletonData.findBone(boneName)
if not BoneData:
raise Exception('Slot bone not found: %s' % boneName)
slotData = SlotData.SlotData(name=slotName, boneData=boneData)
if 'color' in slotMap:
s = slotMap['color']
slotData.r = int(slotMap['color'][0:2], 16)
slotData.g = int(slotMap['color'][2:4], 16)
slotData.b = int(slotMap['color'][4:6], 16)
slotData.a = int(slotMap['color'][6:8], 16)
if 'attachment' in slotMap:
slotData.attachmentName = slotMap['attachment']
skeletonData.slots.append(slotData)
skinsMap = root.get('skins', {})
for skinName in skinsMap.keys():
skin = Skin.Skin(skinName)
skeletonData.skins.append(skin)
if skinName == 'default':
skeletonData.defaultSkin = skin
slotMap = skinsMap[skinName]
for slotName in slotMap.keys():
slotIndex = skeletonData.findSlotIndex(slotName)
attachmentsMap = slotMap[slotName]
for attachmentName in attachmentsMap.keys():
attachmentMap = attachmentsMap[attachmentName]
type = None
typeString = attachmentMap.get('type', 'region')
if typeString == 'region':
type = AttachmentLoader.AttachmentType.region
elif typeString == 'regionSequence':
type = AttachmentLoader.AttachmentType.regionSequence
else:
raise Exception('Unknown attachment type: %s (%s)' % (attachment['type'],
attachmentName))
attachment = self.attachmentLoader.newAttachment(type,
attachmentMap.get('name', attachmentName))
if type == AttachmentLoader.AttachmentType.region or type == AttachmentLoader.AttachmentType.regionSequence:
regionAttachment = attachment
regionAttachment.name = attachmentName
regionAttachment.x = float(attachmentMap.get('x', 0.0)) * self.scale
regionAttachment.y = float(attachmentMap.get('y', 0.0)) * self.scale
regionAttachment.scaleX = float(attachmentMap.get('scaleX', 1.0))
regionAttachment.scaleY = float(attachmentMap.get('scaleY', 1.0))
regionAttachment.rotation = float(attachmentMap.get('rotation', 0.0))
regionAttachment.width = float(attachmentMap.get('width', 32)) * self.scale
regionAttachment.height = float(attachmentMap.get('height', 32)) * self.scale
skin.addAttachment(slotIndex, attachmentName, attachment)
animations = root.get('animations', {})
for animationName in animations:
animationMap = animations.get(animationName, {})
animationData = self.readAnimation(name=animationName,
root=animationMap,
skeletonData=skeletonData)
skeletonData.animations.append(animationData)
return skeletonData
def readAnimation(self, name, root, skeletonData):
if not skeletonData:
raise Exception('skeletonData cannot be null.')
timelines = []
duration = 0.0
bones = root.get('bones', {})
for boneName in bones.keys():
boneIndex = skeletonData.findBoneIndex(boneName)
if boneIndex == -1:
raise Exception('Bone not found: %s' % boneName)
timelineMap = bones[boneName]
for timelineName in timelineMap.keys():
values = timelineMap[timelineName]
if timelineName == 'rotate':
timeline = Animation.Timeline.RotateTimeline(len(values))
timeline.boneIndex = boneIndex
keyframeIndex = 0
for valueMap in values:
time = valueMap['time']
timeline.setKeyframe(keyframeIndex, time, valueMap['angle'])
timeline = readCurve(timeline, keyframeIndex, valueMap)
keyframeIndex += 1
timelines.append(timeline)
if timeline.getDuration() > duration:
duration = timeline.getDuration()
elif timelineName == 'translate' or timelineName == 'scale':
timeline = None
timelineScale = 1.0
if timelineName == 'scale':
timeline = Animation.Timeline.ScaleTimeline(len(values))
else:
timeline = Animation.Timeline.TranslateTimeline(len(values))
timelineScale = self.scale
timeline.boneIndex = boneIndex
keyframeIndex = 0
for valueMap in values:
time = valueMap['time']
timeline.setKeyframe(keyframeIndex,
valueMap['time'],
valueMap.get('x', 0.0),
valueMap.get('y', 0.0))
timeline = readCurve(timeline, keyframeIndex, valueMap)
keyframeIndex += 1
timelines.append(timeline)
if timeline.getDuration() > duration:
duration = timeline.getDuration()
else:
raise Exception('Invalid timeline type for a bone: %s (%s)' % (timelineName, boneName))
slots = root.get('slots', {})
for slotName in slots.keys():
slotIndex = skeletonData.findSlotIndex(slotName)
if slotIndex == -1:
raise Exception('Slot not found: %s' % slotName)
timelineMap = slots[slotName]
for timelineName in timelineMap.keys():
values = timelineMap[timelineName]
if timelineName == 'color':
timeline = Animation.Timeline.ColorTimeline(len(values))
timeline.slotIndex = slotIndex
keyframeIndex = 0
for valueMap in values:
timeline.setKeyframe(keyframeIndex,
valueMap['time'],
int(valueMap['color'][0:2], 16),
int(valueMap['color'][2:4], 16),
int(valueMap['color'][4:6], 16),
int(valueMap['color'][6:8], 16))
timeline = readCurve(timeline, keyframeIndex, valueMap)
keyframeIndex += 1
timelines.append(timeline)
if timeline.getDuration > duration:
duration = timeline.getDuration()
elif timelineName == 'attachment':
timeline = Animation.Timeline.AttachmentTimeline(len(values))
timeline.slotIndex = slotIndex
keyframeIndex = 0
for valueMap in values:
valueName = valueMap['name']
timeline.setKeyframe(keyframeIndex, valueMap['time'], '' if not valueName else valueName)
keyframeIndex += 1
timelines.append(timeline)
if timeline.getDuration > duration:
duration = timeline.getDuration()
else:
raise Exception('Invalid timeline type for a slot: %s (%s)' % (timelineName, slotName))
animation = Animation.Animation(name, timelines, duration)
return animation