-
Notifications
You must be signed in to change notification settings - Fork 5
/
change_rotation_mode_addon.py
394 lines (305 loc) · 11 KB
/
change_rotation_mode_addon.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
'''
Quat/Euler Rotation Mode Converter v0.2
This script/addon:
- Changes (pose) bone rotation mode
- Converts keyframes from one rotation mode to another
- Creates fcurves/keyframes in target rotation mode
- Deletes previous fcurves/keyframes.
- Converts multiple bones
- Converts multiple Actions
TO-DO:
- To convert object's rotation mode (alrady done in Mutant Bob script,
but not done in this one.
- To understand "EnumProperty" and write it well.
- Code clean
- ...
GitHub: https://github.com/MarioMey/rotation_mode_addon/
BlenderArtist thread: http://blenderartists.org/forum/showthread.php?388197-Quat-Euler-Rotation-Mode-Converter
Mutant Bob did the "hard code" of this script. Thanks him!
blender.stackexchange.com/questions/40711/how-to-convert-quaternions-keyframes-to-euler-ones-in-several-actions
Version log:
0.1 - Initial release
0.2 - Pratik Solanki (http://www.dragoneex.com/) fixed the installation as an addon.
'''
bl_info = {
"name": "Quat/Euler Rotation Mode Converter",
"author": "Mario Mey / Mutant Bob",
"version": (0, 2, 1),
"blender": (2, 76, 0),
'location': '',
"description": "Converts bones rotation mode",
"warning": "",
"wiki_url": "",
"tracker_url": "https://github.com/MarioMey/rotation_mode_addon/",
"category": "Animation"}
import bpy
from bpy.props import (StringProperty,
BoolProperty,
IntProperty,
FloatProperty,
FloatVectorProperty,
EnumProperty,
PointerProperty,
CollectionProperty
)
order_list = ['QUATERNION', 'XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX']
class convert():
def get_or_create_fcurve(self, action, data_path, array_index=-1, group=None):
for fc in action.fcurves:
if fc.data_path == data_path and (array_index<0 or fc.array_index == array_index):
return fc
fc = action.fcurves.new(data_path, array_index)
fc.group = group
return fc
def add_keyframe_quat(self, action, quat, frame, bone_prefix, group):
for i in range(len(quat)):
fc = self.get_or_create_fcurve(action, bone_prefix+"rotation_quaternion", i, group)
pos = len(fc.keyframe_points)
fc.keyframe_points.add(1)
fc.keyframe_points[pos].co = [frame, quat[i]]
fc.update()
def add_keyframe_euler(self, action, euler, frame, bone_prefix, group):
for i in range(len(euler)):
fc = self.get_or_create_fcurve(action, bone_prefix+"rotation_euler", i, group)
pos = len(fc.keyframe_points)
fc.keyframe_points.add(1)
fc.keyframe_points[pos].co = [frame, euler[i]]
fc.update()
def frames_matching(self, action, data_path):
frames = set()
for fc in action.fcurves:
if fc.data_path == data_path:
fri = [kp.co[0] for kp in fc.keyframe_points]
frames.update(fri)
return frames
# Converts only one group/bone in one action - Quat to euler
def group_qe(self, obj, action, bone, bone_prefix, order):
pose_bone = bone
data_path = bone_prefix + "rotation_quaternion"
frames = self.frames_matching(action, data_path)
group = action.groups[bone.name]
for fr in frames:
quat = bone.rotation_quaternion.copy()
for fc in action.fcurves:
if fc.data_path == data_path:
quat[fc.array_index] = fc.evaluate(fr)
euler = quat.to_euler(order)
self.add_keyframe_euler(action, euler, fr, bone_prefix, group)
bone.rotation_mode = order
# Converts only one group/bone in one action - Euler to Quat
def group_eq(self, obj, action, bone, bone_prefix, order):
pose_bone = bone
data_path = bone_prefix + "rotation_euler"
frames = self.frames_matching(action, data_path)
group = action.groups[bone.name]
for fr in frames:
euler = bone.rotation_euler.copy()
for fc in action.fcurves:
if fc.data_path == data_path:
euler[fc.array_index] = fc.evaluate(fr)
quat = euler.to_quaternion()
self.add_keyframe_quat(action, quat, fr, bone_prefix, group)
bone.rotation_mode = order
# One Action - One Bone
def one_act_one_bon(self, obj, action, bone, order):
do = False
bone_prefix = ''
# What kind of conversion
cond1 = order == 'XYZ'
cond2 = order == 'XZY'
cond3 = order == 'YZX'
cond4 = order == 'YXZ'
cond5 = order == 'ZXY'
cond6 = order == 'ZYX'
order_euler = cond1 or cond2 or cond3 or cond4 or cond5 or cond6
order_quat = order == 'QUATERNION'
for fcurve in action.fcurves:
# Una fcurve puede no tener grupo.
if hasattr(fcurve, 'group') and fcurve.group.name == bone.name:
# If To-Euler conversion
if order != 'QUATERNION':
if fcurve.data_path.endswith('rotation_quaternion'):
do = True
bone_prefix = fcurve.data_path[:-len('rotation_quaternion')]
break
# If To-Quat conversion
else:
if fcurve.data_path.endswith('rotation_euler'):
do = True
bone_prefix = fcurve.data_path[:-len('rotation_euler')]
break
# If To-Euler conversion
if do and order != 'QUATERNION':
# Converts the group/bone from Quat to Euler
self.group_qe(obj, action, bone, bone_prefix, order)
# Removes quaternion fcurves
for key in action.fcurves:
if key.data_path == 'pose.bones["' + bone.name + '"].rotation_quaternion':
action.fcurves.remove(key)
# If To-Quat conversion
elif do:
# Converts the group/bone from Euler to Quat
self.group_eq(obj, action, bone, bone_prefix, order)
# Removes euler fcurves
for key in action.fcurves:
if key.data_path == 'pose.bones["' + bone.name + '"].rotation_euler':
action.fcurves.remove(key)
# Changes rotation mode to new one
bone.rotation_mode = order
# One Action, selected bones
def one_act_sel_bon(self, obj, action, pose_bones, order):
for bone in pose_bones:
self.one_act_one_bon(obj, action, bone, order)
# One action, all Bones (in Action)
def one_act_every_bon(self, obj, action, order):
# Collects pose_bones that are in the action
pose_bones = set()
# Checks all fcurves
for fcurve in action.fcurves:
# Look for the ones that has rotation_euler
if order == 'QUATERNION':
if fcurve.data_path.endswith('rotation_euler'):
# If the bone from action really exists
if fcurve.group.name in obj.pose.bones:
if obj.pose.bones[fcurve.group.name] not in pose_bones:
pose_bones.add(obj.pose.bones[fcurve.group.name])
else:
print(fcurve.group.name, 'does not exist in Armature. Fcurve-group is not affected')
# Look for the ones that has rotation_quaternion
else:
if fcurve.data_path.endswith('rotation_quaternion'):
# If the bone from action really exists
if fcurve.group.name in obj.pose.bones:
if obj.pose.bones[fcurve.group.name] not in pose_bones:
pose_bones.add(obj.pose.bones[fcurve.group.name])
else:
print(fcurve.group.name, 'does not exist in Armature. Fcurve-group is not affected')
# Convert current action and pose_bones that are in each action
for bone in pose_bones:
self.one_act_one_bon(obj, action, bone, order)
# All Actions, selected bones
def all_act_sel_bon(self, obj, pose_bones, order):
for action in bpy.data.actions:
for bone in pose_bones:
self.one_act_one_bon(obj, action, bone, order)
# All actions, All Bones (in each Action)
def all_act_every_bon(self, obj, order):
for action in bpy.data.actions:
self.one_act_every_bon(obj, action, order)
convert = convert()
def initSceneProperties(PropertyGroup):
bpy.types.Scene.order_list = bpy.props.EnumProperty(
items = [('QUATERNION', 'QUATERNION', 'QUATERNION' ),
('XYZ', 'XYZ', 'XYZ' ),
('XZY', 'XZY', 'XZY' ),
('YXZ', 'YXZ', 'YXZ' ),
('YZX', 'YZX', 'YZX' ),
('ZXY', 'ZXY', 'ZXY' ),
('ZYX', 'ZYX', 'ZYX' ) ],
name = "Order",
description = "The targe rotation mode")
scn['order_list'] = 0
# GUI (Panel)
#
class ToolsPanel(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_category = "Tools"
bl_context = "posemode"
bl_label = 'Quat/Euler Converter'
# draw the gui
def draw(self, context):
layout = self.layout
scn = context.scene
#~ toolsettings = context.tool_settings
col = layout.column(align=True)
row = col.row(align=True)
layout.prop(scn, 'order_list')
col = layout.column(align=True)
row = col.row(align=True)
col.label(text="Current Action:")
col.operator('current.selected')
col.operator('current.every')
row = col.row(align=True)
col = layout.column(align=True)
col.label(text="All Actions:")
col.operator('all.selected')
col.operator('all.every')
class CONVERT_OT_current_action_selected_bones(bpy.types.Operator):
bl_label = 'Selected Bones'
bl_idname = 'current.selected'
bl_description = 'Converts selected bones in current Action'
bl_options = {'REGISTER', 'UNDO'}
# on mouse up:
def invoke(self, context, event):
self.execute(context)
return {'FINISHED'}
def execute(op, context):
obj = bpy.context.active_object
pose_bones = bpy.context.selected_pose_bones
action = obj.animation_data.action
order = order_list[bpy.context.scene['order_list']]
convert.one_act_sel_bon(obj, action, pose_bones, order)
return {'FINISHED'}
class CONVERT_OT_current_action_every_bones(bpy.types.Operator):
bl_label = 'All Bones'
bl_idname = 'current.every'
bl_description = 'Converts every bone in current Action'
bl_options = {'REGISTER', 'UNDO'}
# on mouse up:
def invoke(self, context, event):
self.execute(context)
return {'FINISHED'}
def execute(op, context):
obj = bpy.context.active_object
pose_bones = bpy.context.selected_pose_bones
action = obj.animation_data.action
order = order_list[bpy.context.scene['order_list']]
convert.one_act_every_bon(obj, action, order)
return {'FINISHED'}
class CONVERT_OT_all_actions_selected_bones(bpy.types.Operator):
bl_label = 'Selected Bone'
bl_idname = 'all.selected'
bl_description = 'Converts selected bones in every Action'
bl_options = {'REGISTER', 'UNDO'}
# on mouse up:
def invoke(self, context, event):
self.execute(context)
return {'FINISHED'}
def execute(op, context):
obj = bpy.context.active_object
pose_bones = bpy.context.selected_pose_bones
order = order_list[bpy.context.scene['order_list']]
convert.all_act_sel_bon(obj, pose_bones, order)
return {'FINISHED'}
class CONVERT_OT_all_action_every_bones(bpy.types.Operator):
bl_label = 'All Bone'
bl_idname = 'all.every'
bl_description = 'Converts every bone in every Action'
bl_options = {'REGISTER', 'UNDO'}
# on mouse up:
def invoke(self, context, event):
self.execute(context)
return {'FINISHED'}
def execute(op, context):
obj = bpy.context.active_object
order = order_list[bpy.context.scene['order_list']]
convert.all_act_every_bon(obj, order)
def register():
bpy.utils.register_module(__name__)
bpy.types.Scene.order_list = bpy.props.EnumProperty(
items = [('QUATERNION', 'QUATERNION', 'QUATERNION' ),
('XYZ', 'XYZ', 'XYZ' ),
('XZY', 'XZY', 'XZY' ),
('YXZ', 'YXZ', 'YXZ' ),
('YZX', 'YZX', 'YZX' ),
('ZXY', 'ZXY', 'ZXY' ),
('ZYX', 'ZYX', 'ZYX' ) ],
name = "Order",
description = "The targe rotation mode")
#bpy.types.Scene.convertrot = PointerProperty(type=initSceneProperties)
def unregister():
bpy.utils.unregister_module(__name__)
del bpy.types.Scene.order_list
if __name__ == "__main__":
register()