-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixamo_converter.py
250 lines (197 loc) · 10.4 KB
/
mixamo_converter.py
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
import bpy
import mathutils
import math
class EMOTICONS_UTILS_PT_mixamo_panel(bpy.types.Panel):
bl_label = "Convert Mixamo to Emoticons"
bl_idname = "EMOTICONS_UTILS_PT_mixamo_panel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "Emoticons Utils"
def draw(self, context):
layout = self.layout
scene = context.scene
properties = scene.emoticonsutils
layout.prop_search(properties, "emoticons_armature", scene, "objects", text="Emoticons")
layout.prop_search(properties, "mixamo_armature", scene, "objects", text="Mixamo")
layout.label(text="Frame range:")
layout.prop(properties, "frame_start")
layout.prop(properties, "frame_end")
layout.prop(properties, "frame_step")
layout.prop(properties, "use_hands")
layout.prop(properties, "use_human_parent_space")
layout.prop(properties, "limit_rotation_axis")
layout.prop(properties, "use_euler_filter")
layout.label(text="Convert and copy the animation")
layout.operator("emoticons_utils.convert_mixamo")
class EMOTICONS_UTILS_OT_convert_mixamo(bpy.types.Operator):
bl_label = "Convert Mixamo"
bl_idname = "emoticons_utils.convert_mixamo"
def execute(self, context):
scene = context.scene
properties = scene.emoticonsutils
if properties.emoticons_armature is None or properties.mixamo_armature is None:
self.report({'ERROR'}, 'Emoticons or Mixamo armature is not selected')
return {"CANCELLED"}
for frame in range(properties.frame_start, properties.frame_end + 1, properties.frame_step):
scene.frame_set(frame)
#go through every mixamo bone
for mixamoBone in properties.mixamo_armature.pose.bones:
boneName = self.parseMixamoName(mixamoBone.name, properties)
if boneName == None:
continue
deltarotation = self.getDeltaRotation(boneName)
rotation = mathutils.Quaternion((mixamoBone.rotation_quaternion[0],
mixamoBone.rotation_quaternion[1],
mixamoBone.rotation_quaternion[2],
mixamoBone.rotation_quaternion[3]))
rotation = rotation @ deltarotation
boneindex = properties.emoticons_armature.pose.bones.find(boneName)
#get the bone in the emoticons armature from the parsed name
if boneindex != -1:
bone = properties.emoticons_armature.pose.bones[boneindex]
bone.rotation_quaternion = rotation
if "body" in bone.name:
if "low" in bone.name:
if properties.use_human_parent_space:
bone.rotation_quaternion = self.getParentSpace(mixamoBone, 2) @ bone.rotation_quaternion
else:
bone.location[0] = -(mixamoBone.location[0] * properties.mixamo_armature.scale[0])
bone.location[1] = (mixamoBone.location[1] * properties.mixamo_armature.scale[1])
bone.location[2] = -(mixamoBone.location[2] * properties.mixamo_armature.scale[2])
self.mirrorQuat(bone.rotation_quaternion, 'Y')
elif "arm" in bone.name:
if ".end" in bone.name:
self.mirrorQuat(bone.rotation_quaternion, 'X')
if "low" in bone.name:
self.swapComponents(bone.rotation_quaternion, 1, 2)
if "left" in bone.name:
self.mirrorQuat(bone.rotation_quaternion, 'YZ')
if "right" in bone.name:
self.mirrorQuat(bone.rotation_quaternion, 'XZ')
else:
#take into account shoulder space
if properties.use_human_parent_space:
bone.rotation_quaternion = self.getParentSpace(mixamoBone, 1) @ bone.rotation_quaternion
self.mirrorQuat(bone.rotation_quaternion, 'Z')
elif "leg" in bone.name:
self.mirrorQuat(bone.rotation_quaternion, 'Z')
elif "head" in bone.name:
#take into account neck space
if properties.use_human_parent_space:
bone.rotation_quaternion = self.getParentSpace(mixamoBone, 1) @ bone.rotation_quaternion
bone.rotation_euler = bone.rotation_quaternion.to_euler()
if properties.limit_rotation_axis and ("low" in bone.name and (("arm" in bone.name and not ".end" in bone.name) or "leg" in bone.name)):
bone.rotation_euler[1] = 0
bone.rotation_euler[2] = 0
bone.keyframe_insert(data_path="rotation_euler", frame=frame)
bone.keyframe_insert(data_path="location", frame=frame)
if (properties.frame_end - properties.frame_start + 1) > 1:
self.selectSoloObj(properties.emoticons_armature)
for bone in properties.emoticons_armature.pose.bones:
bpy.context.object.data.bones.active = bone.bone
self.eulerFilter(properties.emoticons_armature, properties)
return {'FINISHED'}
def parseMixamoName(self, name, properties):
name = name.lower()
if "up" in name:
if "left" in name:
if "leg" in name:
return "left_leg"
elif "right" in name:
if "leg" in name:
return "right_leg"
else:
if "hips" in name:
return "body"
elif "head" in name and not "end" in name:
return "head"
elif "spine2" in name:
return "low_body"
elif "left" in name:
if "arm" in name:
if "fore" in name:
return "low_left_arm"
else:
return "left_arm"
elif "leg" in name:
return "low_left_leg"
elif "mixamorig:lefthand" == name and properties.use_hands:
return "low_left_arm.end"
elif "right" in name:
if "arm" in name:
if "fore" in name:
return "low_right_arm"
else:
return "right_arm"
elif "leg" in name:
return "low_leg_right"
elif "mixamorig:righthand" == name and properties.use_hands:
return "low_right_arm.end"
#this method mirrors the quaternion using one axis or a plane
def mirrorQuat(self, quat, axis):
axis.upper()
indices = [1,2,3]
indices[ord(axis[0]) - 88] = -1
if len(axis) == 2:
indices[ord(axis[1]) - 88] = -1
for i in indices:
if i != -1:
quat[i] *= -1
def swapComponents(self, quat, i, j):
tmp = quat[i]
quat[i] = quat[j]
quat[j] = tmp
def getDeltaRotation(self, name):
if "arm" in name and not "low" in name:
if "left" in name:
return mathutils.Quaternion((0.0, 0.0, 1.0), math.radians(90.0))
elif "right" in name:
return mathutils.Quaternion((0.0, 0.0, 1.0), math.radians(-90.0))
return mathutils.Quaternion((0.0, 0.0, 0.0), math.radians(0.0))
def getParentSpace(self, bone, depth):
if depth == 1:
return bone.parent.rotation_quaternion
return self.getParentSpace(bone.parent, depth - 1) @ bone.parent.rotation_quaternion
def eulerFilter(self, object_with_animaton, properties):
if not properties.use_euler_filter:
return
object_with_animaton.select_set(True)
action = object_with_animaton.animation_data.action
fcurves = action.fcurves
context_override = bpy.context.copy()
context_override["object"] = object_with_animaton
context_override["active_object"] = object_with_animaton
context_override["selected_objects"] = [object_with_animaton]
context_override["selected_editable_objects"] = [object_with_animaton]
context_override["selected_editable_objects"] = [object_with_animaton]
context_override["active_action"] = action
context_override["selected_editable_actions"] = [action]
context_override["active_editable_fcurve"] = fcurves[0]
context_override["selected_visible_fcurves"] = fcurves
context_override["selected_editable_fcurves"] = fcurves
window = bpy.context.window
screen = window.screen
window = bpy.context.window
screen = window.screen
#avoid TOPBAR - for some reason it caused problems
for area in screen.areas:
if area.type != 'TOPBAR':
oldtype = area.type
area.type = 'GRAPH_EDITOR'
context_override["window"] = window
context_override["screen"] = screen
context_override["area"] = area
context_override["region"] = area.regions[0]
try:
with bpy.context.temp_override(**context_override):
bpy.ops.graph.euler_filter()
except Exception as e:
print(str(e))
area.type = oldtype
break
def selectSoloObj(self, obj):
#make the obj the only selected active object
if bpy.context.view_layer.objects.active is not None:
bpy.context.view_layer.objects.active.select_set(False)
bpy.context.view_layer.objects.active = obj
obj.select_set(True)