This repository was archived by the owner on Dec 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadditive_animation_modifiers.py
More file actions
405 lines (343 loc) · 15.9 KB
/
additive_animation_modifiers.py
File metadata and controls
405 lines (343 loc) · 15.9 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
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
395
396
397
398
399
400
401
402
403
404
405
bl_info = {
"name": "Additive animation modifiers",
"author": "Przemysław Bągard",
"blender": (2,7,9),
"version": (0,0,1),
"location": "Armature > Additive animation modifiers",
"description": "Adds additive animation tracks to current pose.",
"category": "Animation"
}
#usefull links:
# https://docs.blender.org/api/blender_python_api_2_74_0/bpy.app.handlers.html
# https://github.com/artellblender/additive_keyer
# https://docs.blender.org/api/blender_python_api_2_77_0/bpy.types.UILayout.html
# https://blenderartists.org/forum/showthread.php?209221-calculate-bone-location-rotation-from-fcurve-animation-data
# bpy.context.object.pose.bones['forearm.L']
# action = bpy.context.object.data.additive_animations[0].action
import bpy
from bpy.props import IntProperty, CollectionProperty, BoolProperty, StringProperty, PointerProperty
from bpy.types import Panel, UIList
import mathutils
from mathutils import Vector, Quaternion, Euler, Matrix
from bpy.app.handlers import persistent
# Functions
def getCurve(action, bone, dpath, index):
curveName = 'pose.bones["' + bone.name + '"].' + dpath
curve = action.fcurves.find(curveName, index = index)
#if curve:
# print(curveName + "[" + str(index) + "]" ".evaluate(" + str(bpy.context.scene.frame_current) + ") = " + str(curve.evaluate(bpy.context.scene.frame_current)))
#else:
# print("No " + curveName + "[" + str(index) + "]" "!")
return curve
def getActionLocation(action, bone, frame=1):
pos = Vector()
for i in range(0, 3):
fc = getCurve(action, bone, 'location', index = i)
if fc != None:
pos[i] = fc.evaluate(frame)
return pos
def getActionRotation(action, bone, frame=1):
rot = Quaternion()
rot.identity()
if bone.rotation_mode != 'QUATERNION':
rot = Euler(Vector(), bone.rotation_mode)
for i in range(0, 3):
fc = getCurve(action, bone, 'rotation_euler', index = i)
if fc != None:
rot[i] = fc.evaluate(frame)
return rot.to_matrix()
else:
for i in range(0, 4):
fc = getCurve(action, bone, 'rotation_quaternion', index = i)
if fc != None:
rot[i] = fc.evaluate(frame)
return rot.to_matrix()
return rot.to_matrix()
def getActionScale(action, bone, frame=1):
scale = Vector((1, 1, 1))
for i in range(0, 3):
fc = getCurve(action, bone, 'scale', index = i)
if fc != None:
scale[i] = fc.evaluate(frame)
return scale
def getActionMatrix(action, bone, frame=1):
scale = getActionScale(action, bone, frame)
size_matrix = Matrix.Identity(4)
for i in range(3):
size_matrix.col[i].magnitude = scale[i]
rot_matrix = getActionRotation(action, bone, frame).to_4x4()
#matrix = rot_matrix*size_matrix
matrix = rot_matrix
matrix.translation = getActionLocation(action, bone, frame)
return matrix
def applyActions(obj, actions):
current_frame = bpy.context.scene.frame_current
bones = obj.pose.bones
for bone in bones:
matrix_total = Matrix()
for action in actions:
matrix = getActionMatrix(action, bone, current_frame)
matrix_total = matrix_total*matrix
bone.matrix = bone.matrix*matrix_total
def deapplyActions(obj, actions):
current_frame = bpy.context.scene.frame_current
bones = obj.pose.bones
for bone in bones:
matrix_total = Matrix()
for action in actions:
matrix = getActionMatrix(action, bone, current_frame)
matrix_total = matrix_total*matrix
bone.matrix = bone.matrix*matrix_total.inverted()
def insertKeyframe(types):
scene = bpy.context.scene
bones = bpy.context.selected_pose_bones
#scene.tool_settings.use_keyframe_insert_auto = False
for object in scene.objects:
if object.type != 'ARMATURE':
continue
actions = []
for additive_animations in object.data.additive_animations:
if additive_animations.action and additive_animations.enabled:
action = additive_animations.action
actions.append(action)
deapplyActions(object, actions)
for type in types:
bpy.ops.anim.keyframe_insert_menu(type=type)
for object in scene.objects:
if object.type != 'ARMATURE':
continue
actions = []
for additive_animations in object.data.additive_animations:
if additive_animations.action and additive_animations.enabled:
action = additive_animations.action
actions.append(action)
applyActions(object, actions)
def clearPose(obj):
for pb in obj.pose.bones:
#Set the rotation to 0
if pb.rotation_mode == 'QUATERNION':
pb.rotation_quaternion.identity()
else:
pb.rotation_euler = Euler(Vector(), pb.rotation_mode )
#Set the scale to 1
pb.scale = Vector( (1, 1, 1) )
#Set the location at rest (edit) pose bone position
pb.location = Vector()
# bpy.app.handlers.frame_change_post.clear()
# bpy.app.handlers.frame_change_post.append(additiveAnimationModifiersPostHandler)
def recalculateFrame(self, context):
frame = bpy.context.scene.frame_current
context.scene.frame_set(frame)
@persistent
def additiveAnimationModifiersPreHandler(scene):
if not scene.additive_animation_enabled:
return
for object in scene.objects:
if object.type != 'ARMATURE':
continue
clearPose(object)
@persistent
def additiveAnimationModifiersPostHandler(scene):
if not scene.additive_animation_enabled:
return
for object in scene.objects:
if object.type != 'ARMATURE':
continue
actions = []
for additive_animations in object.data.additive_animations:
if additive_animations.action and additive_animations.enabled:
action = additive_animations.action
actions.append(action)
applyActions(object, actions)
scene.additive_animation_update_is_needed = True
@persistent
def postSceneUpdate(scene):
if scene.additive_animation_update_is_needed == True:
#print ('postscene: ' + str(scene.additive_animation_update_is_needed))
for object in scene.objects:
if object.type != 'MESH':
continue
object.data.update()
scene.additive_animation_update_is_needed = False
# ui list item actions
class Uilist_actions(bpy.types.Operator):
bl_idname = "additive_animations.list_action"
bl_label = "List Action"
action = bpy.props.EnumProperty(
items=(
('UP', "Up", ""),
('DOWN', "Down", ""),
('REMOVE', "Remove", ""),
('ADD', "Add", ""),
)
)
def invoke(self, context, event):
scn = context.object.data
idx = scn.additive_animations_index
try:
item = scn.additive_animations[idx]
except IndexError:
pass
else:
if self.action == 'DOWN' and idx < len(scn.additive_animations) - 1:
item_next = scn.additive_animations[idx+1].name
scn.additive_animations.move(idx, idx + 1)
scn.additive_animations_index += 1
recalculateFrame(None, context)
#info = 'Item %d selected' % (scn.additive_animations_index + 1)
#self.report({'INFO'}, info)
elif self.action == 'UP' and idx >= 1:
item_prev = scn.additive_animations[idx-1].name
scn.additive_animations.move(idx, idx-1)
scn.additive_animations_index -= 1
recalculateFrame(None, context)
#info = 'Item %d selected' % (scn.additive_animations_index + 1)
#self.report({'INFO'}, info)
elif self.action == 'REMOVE':
#info = 'Item %s removed from list' % (scn.additive_animations[scn.additive_animations_index].name)
scn.additive_animations_index -= 1
#self.report({'INFO'}, info)
scn.additive_animations.remove(idx)
recalculateFrame(None, context)
if self.action == 'ADD':
item = scn.additive_animations.add()
item.id = len(scn.additive_animations)
scn.additive_animations_index = (len(scn.additive_animations)-1)
recalculateFrame(None, context)
#info = '%s added to list' % (item.name)
#self.report({'INFO'}, info)
return {"FINISHED"}
# -------------------------------------------------------------------
# draw
# -------------------------------------------------------------------
# additive_animations list
class AdditiveAnimation_items(UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
split = layout.split(0.95)
split2 = split.split(0.2)
split2.label("Action: %d" % (index))
split2.prop(item, "action", text="", emboss=True, translate=False)
split.prop(item, "enabled", text="", emboss=False, translate=False, icon='VISIBLE_IPO_ON' if item.enabled else 'VISIBLE_IPO_OFF', toggle=True, icon_only=True)
def invoke(self, context, event):
pass
# draw the panel
class UIListPanelExample(Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "Additive animation modifiers"
bl_idname = "OBJECT_PT_additive_animation_modifiers"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "data"
def draw(self, context):
layout = self.layout
object = context.object
if not object or object.type != 'ARMATURE':
return
armature = object.data
row = layout.row()
row.prop(context.scene, "additive_animation_enabled", text="Enable addon for scene")
row = layout.row()
row.template_list("AdditiveAnimation_items", "", armature, "additive_animations", armature, "additive_animations_index", rows=2)
col = row.column(align=True)
col.operator("additive_animations.list_action", icon='ZOOMIN', text="").action = 'ADD'
col.operator("additive_animations.list_action", icon='ZOOMOUT', text="").action = 'REMOVE'
col.separator()
col.operator("additive_animations.list_action", icon='TRIA_UP', text="").action = 'UP'
col.operator("additive_animations.list_action", icon='TRIA_DOWN', text="").action = 'DOWN'
row = layout.row()
col = row.column(align=True)
col.operator("anim.insert_keyframe_menu")
class AdditiveAnimationInsertKeyframeMenu(bpy.types.Menu):
bl_idname = "ADDITIVE_ANIMATION_insert_keyframe_menu"
bl_label = "Insert keyframe menu"
# Set the menu operators and draw functions
def draw(self, context):
layout = self.layout
layout.operator("additive_animations.insert_key_loc", text="Insert key loc")
layout.operator("additive_animations.insert_key_rot", text="Insert key rot")
#layout.operator("additive_animations.insert_key_scale", text="Insert key scale")
layout.operator("additive_animations.insert_key_locrot", text="Insert key loc and rot")
#layout.operator("additive_animations.insert_key_locrotscale", text="Insert key loc and rot and scale")
#layout.operator("additive_animations.insert_key_loc", text="Insert key loc").type = 'ACTIVE'
#layout.operator("rigidbody.objects_add", text="B Add Passive").type = 'PASSIVE'
#layout.prop(context.scene.render, "engine")
# insert rotation keyframe
class AdditiveAnimationShowMenuOperator(bpy.types.Operator):
bl_idname = "anim.insert_keyframe_menu"
bl_label = "Show insert keyframe menu"
bl_description = "Shows menu with insert key rotation without additive animation modifiers"
def execute(self, context):
bpy.ops.wm.call_menu(name="ADDITIVE_ANIMATION_insert_keyframe_menu")
return{'FINISHED'}
class Uilist_insertKeyLoc(bpy.types.Operator):
bl_idname = "additive_animations.insert_key_loc"
bl_label = "Insert location without additive animation"
bl_description = "Insert key location without additive animation modifiers"
def execute(self, context):
insertKeyframe(['Location']);
return{'FINISHED'}
class Uilist_insertKeyRot(bpy.types.Operator):
bl_idname = "additive_animations.insert_key_rot"
bl_label = "Insert rot without additive animation"
bl_description = "Insert key rotation without additive animation modifiers"
def execute(self, context):
insertKeyframe(['Rotation']);
return{'FINISHED'}
#class Uilist_insertKeyScale(bpy.types.Operator):
# bl_idname = "additive_animations.insert_key_scale"
# bl_label = "Insert scale without additive animation"
# bl_description = "Insert key scale without additive animation modifiers"
#
# def execute(self, context):
# insertKeyframe(['Scaling']);
# return{'FINISHED'}
class Uilist_insertKeyLocRot(bpy.types.Operator):
bl_idname = "additive_animations.insert_key_locrot"
bl_label = "Insert location and rotation without additive animation"
bl_description = "Insert key location and rotation without additive animation modifiers"
def execute(self, context):
insertKeyframe(['Location', 'Rotation']);
return{'FINISHED'}
#class Uilist_insertKeyLocRotScale(bpy.types.Operator):
# bl_idname = "additive_animations.insert_key_locrotscale"
# bl_label = "Insert location and rotation and scale without additive animation"
# bl_description = "Insert key location and rotation and scale without additive animation modifiers"
#
# def execute(self, context):
# insertKeyframe(['Location', 'Rotation', 'Scaling']);
# return{'FINISHED'}
# Create additive_animations property group
class CustomProp(bpy.types.PropertyGroup):
'''name = StringProperty() '''
id = IntProperty()
enabled = BoolProperty(default=True, update=recalculateFrame);
action = PointerProperty(name="Action", type=bpy.types.Action, update=recalculateFrame)
# bpy.data.window_managers[0].keyconfigs.active.keymaps['Mesh'].keymap_items.new('op.idname',value='PRESS',type=' A',ctrl=True,alt=True,shift=True,oskey=True)
# bpy.data.window_managers[0].keyconfigs.active.keymaps['Animation'].keymap_items.new('op.idname',value='PRESS',type=' A',ctrl=True,alt=True,shift=True,oskey=True)
# -------------------------------------------------------------------
# register
# -------------------------------------------------------------------
def add_to_menu(self, context):
self.layout.operator("additive_animations.insert_key_rot")
bpy.utils.register_class(AdditiveAnimationInsertKeyframeMenu)
def register():
bpy.utils.register_module(__name__)
bpy.types.Armature.additive_animations = CollectionProperty(type=CustomProp)
bpy.types.Armature.additive_animations_index = IntProperty()
bpy.types.Scene.additive_animation_enabled = BoolProperty(default=False, update=recalculateFrame)
bpy.types.Scene.additive_animation_update_is_needed = BoolProperty(default=False)
bpy.app.handlers.scene_update_pre.append(postSceneUpdate)
bpy.app.handlers.frame_change_pre.append(additiveAnimationModifiersPreHandler)
bpy.app.handlers.frame_change_post.append(additiveAnimationModifiersPostHandler)
#bpy.types.ANIM_OT_keyframe_insert_menu.append(add_to_menu)
def unregister():
bpy.utils.unregister_module(__name__)
del bpy.types.Armature.additive_animations
del bpy.types.Armature.additive_animations_index
del bpy.types.Scene.additive_animation_enabled
del bpy.types.Scene.additive_animation_update_is_needed
bpy.app.handlers.scene_update_pre.remove(postSceneUpdate)
bpy.app.handlers.frame_change_post.remove(additiveAnimationModifiersPostHandler)
#bpy.types.ANIM_OT_keyframe_insert_menu.remove(add_to_menu)
if __name__ == "__main__":
register()