-
Notifications
You must be signed in to change notification settings - Fork 15
/
actions.py
626 lines (477 loc) · 20.3 KB
/
actions.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
import bpy
from bpy.props import *
from mathutils import *
from .common import *
class YNewAction(bpy.types.Operator):
bl_idname = "armature.y_new_action"
bl_label = "New Action"
bl_description = "New action"
bl_options = {'REGISTER', 'UNDO'}
action_name : StringProperty(
name = 'Action Name',
description = 'New action name',
default = 'Action'
)
enable_fake_user : BoolProperty(
name = 'Enable Fake User',
description = "Enable fake user so animation don't dissapear after saving",
default = True
)
reset_pose : BoolProperty(
name = 'Reset Pose',
description = 'Reset pose',
default = True
)
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
self.layout.prop(self, 'action_name')
self.layout.prop(self, 'enable_fake_user')
self.layout.prop(self, 'reset_pose')
@classmethod
def poll(cls, context):
obj = get_current_armature_object()
return obj
def execute(self, context):
obj = get_current_armature_object()
# Make sure animation data is exists
if not obj.animation_data:
obj.animation_data_create()
action = bpy.data.actions.new(self.action_name)
if self.reset_pose:
obj.animation_data.action = None
reset_pose_bones(obj)
obj.animation_data.action = action
if self.enable_fake_user:
action.use_fake_user = True
return {'FINISHED'}
class YRemoveAction(bpy.types.Operator):
bl_idname = "armature.y_remove_action"
bl_label = "Remove Action"
bl_description = "Remove action"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
obj = get_current_armature_object()
return obj and obj.animation_data
def execute(self, context):
obj = get_current_armature_object()
wm_props = context.window_manager.rigify_export_props
try: action = bpy.data.actions[wm_props.active_action]
except:
self.report({'ERROR'}, "No action selected!")
return {'CANCELLED'}
bpy.data.actions.remove(action)
wm_props.active_action = min(wm_props.active_action, len(bpy.data.actions)-1)
return {'FINISHED'}
class YToggleDeselectAction(bpy.types.Operator):
bl_idname = "armature.y_toggle_deselect_action"
bl_label = "Toggle Deselect Action"
bl_description = "Toggle Deselect action"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
obj = get_current_armature_object()
return obj and obj.animation_data
def execute(self, context):
obj = get_current_armature_object()
wm_props = context.window_manager.rigify_export_props
active_action = None
list_action = None
if obj.animation_data:
active_action = obj.animation_data.action
try: list_action = bpy.data.actions[wm_props.active_action]
except: pass
if active_action == list_action:
obj.animation_data.action = None
reset_pose_bones(obj)
else:
obj.animation_data.action = list_action
return {'FINISHED'}
class YRemoveNonTransformativeFrames(bpy.types.Operator):
bl_idname = "armature.y_remove_non_transformative_frames"
bl_label = "Remove Non Transformative Frames"
bl_description = "Remove non transformative frames"
bl_options = {'REGISTER', 'UNDO'}
all_actions : BoolProperty(
name = 'All Actions',
description = 'Remove untransformative frames on all actions rather than only on active action',
default = False
)
remove_nlas : BoolProperty(
name = 'Remove NLA Tracks',
description = 'Remove all NLA tracks to avoid mixed animations',
default = False
)
ignore_root : BoolProperty(
name = 'Do Not Remove Root Transformations',
description = 'Do not remove root transformations',
default = True
)
ignore_object_transform : BoolProperty(
name = 'Do Not Remove Object Transformations',
description = 'Do not remove object transformations',
default = True
)
tolerance : FloatProperty(
name = 'Tolerance',
description = 'Tolerance value for keyframes',
default = 0.0001
)
@classmethod
def poll(cls, context):
return get_current_armature_object()
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
self.layout.prop(self, 'all_actions')
self.layout.prop(self, 'remove_nlas')
self.layout.prop(self, 'ignore_root')
self.layout.prop(self, 'ignore_object_transform')
self.layout.prop(self, 'tolerance')
def execute(self, context):
obj = get_current_armature_object()
wm_props = context.window_manager.rigify_export_props
if self.all_actions:
actions = bpy.data.actions
else:
# Get action
try: actions = [bpy.data.actions[wm_props.active_action]]
except:
self.report({'ERROR'}, "No action selected!")
return {'CANCELLED'}
for action in actions:
msgs = remove_non_transformed_keyframes(action, self.ignore_object_transform, self.ignore_root, self.tolerance)
for msg in msgs:
self.report({'INFO'}, msg)
if len(msgs) > 0:
self.report({'INFO'}, "Total fcurves removed: " + str(len(msgs)))
# Remove NLA tracks
if self.remove_nlas:
for track in reversed(obj.animation_data.nla_tracks):
obj.animation_data.nla_tracks.remove(track)
#print(transformed_key_found)
return {'FINISHED'}
class YToggleActionSettings(bpy.types.Operator):
bl_idname = "scene.y_toggle_action_settings"
bl_label = "Toggle Action Settings"
bl_description = "Toggle action settings"
#bl_options = {'REGISTER', 'UNDO'}
prop : StringProperty(default='show_action_settings')
@classmethod
def poll(cls, context):
return True
def execute(self, context):
props = context.scene.rigify_export_props
if self.prop not in dir(props) or not self.prop.startswith('show_'):
return {'CANCELLED'}
cur_value = getattr(props, self.prop)
setattr(props, self.prop, not cur_value)
return {'FINISHED'}
class YToggleActionGlobalSettings(bpy.types.Operator):
bl_idname = "scene.y_toggle_action_global_settings"
bl_label = "Toggle Action List Global Settings"
bl_description = "Toggle action list global settings"
#bl_options = {'REGISTER', 'UNDO'}
prop : StringProperty(default='show_global_settings')
@classmethod
def poll(cls, context):
return True
def execute(self, context):
props = context.scene.rigify_export_props
if self.prop not in dir(props) or not self.prop.startswith('show_'):
return {'CANCELLED'}
cur_value = getattr(props, self.prop)
setattr(props, self.prop, not cur_value)
return {'FINISHED'}
def draw_action_manager(self, context):
obj = get_current_armature_object()
scene_props = context.scene.rigify_export_props
props = context.window_manager.rigify_export_props
listrow = self.layout.row()
col = listrow.column()
action = None
# Check active action
if obj.animation_data:
action = obj.animation_data.action
if action and (
props.active_action >= len(bpy.data.actions) or
action != bpy.data.actions[props.active_action]
):
index = [i for i,a in enumerate(bpy.data.actions) if a == action][0]
props.active_action = index
col.template_list("ACTION_UL_y_action_lists", "", bpy.data,
"actions", props, "active_action", rows=3, maxrows=5)
col = listrow.column(align=True)
col.operator('armature.y_new_action', text='', icon='ADD')
col.operator('armature.y_remove_action', text='', icon='REMOVE')
col.menu("ACTION_MT_y_action_list_special_menu", text='', icon='DOWNARROW_HLT')
col.operator('armature.y_toggle_deselect_action', text='', icon='OUTLINER_OB_ARMATURE')
col = self.layout.column()
#if action:
#col.operator('armature.y_toggle_deselect_action', icon='ACTION')
r = col.row()
rr = r.row()
if scene_props.show_action_settings: rr.alert = True
icon = 'PREFERENCES'
rr.operator('scene.y_toggle_action_settings', text='', icon=icon).prop = 'show_action_settings'
#col.label(text='Active: ' + action.name, icon='ACTION')
if action:
r.label(text='Active Action: ' + action.name)
else:
r.label(text='Active Action: -')
if scene_props.show_action_settings:
box = col.box()
bcol = box.column()
if not action:
bcol.label(text='No active action!')
else:
bcol.prop(action, 'use_frame_range')
if action.use_frame_range:
brow = bcol.row(align=True)
brow.prop(action, 'frame_start')
brow.prop(action, 'frame_end')
action_props = action.rigify_export_props
bcol.prop(action_props, 'enable_loop')
if action_props.enable_loop:
brow = bcol.row()
brow.active = (not action.use_frame_range) # and action_props.enable_loop
brow.prop(action_props, 'enable_skip_last_frame')
bcol.prop(action_props, 'enable_remove_untransformed', text='Remove Untransformed')
if action_props.enable_remove_untransformed:
bcol.prop(action_props, 'untransformed_tolerance', text='Tolerance')
r = col.row()
rr = r.row()
if scene_props.show_global_settings: rr.alert = True
icon = 'PREFERENCES' # if is_greater_than_280() else 'SCRIPTWIN'
rr.operator('scene.y_toggle_action_global_settings', text='', icon=icon).prop = 'show_global_settings'
#r.label(text='Action Manager Settings:', icon='PREFERENCES')
r.label(text='Action Manager Settings:')
if scene_props.show_global_settings:
box = col.box()
bcol = box.column()
bcol.prop(scene_props, 'sync_frames')
bcol.prop(scene_props, 'sync_bone_layers')
bcol.prop(scene_props, 'sync_unkeyframed_bones')
bcol.prop(scene_props, 'sync_rigify_props')
if scene_props.sync_rigify_props:
bbox = bcol.box()
bbcol = bbox.column(align=True)
bbcol.prop(scene_props, 'default_hand_ik')
bbcol.prop(scene_props, 'default_foot_ik')
bbcol.prop(scene_props, 'default_arm_follow')
bbcol.prop(scene_props, 'default_leg_follow')
class UE4HELPER_PT_item_RigifyExportActionPanel(bpy.types.Panel):
bl_label = "Action Manager"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Item"
@classmethod
def poll(cls, context):
return get_current_armature_object()
def draw(self, context):
draw_action_manager(self, context)
class UE4HELPER_PT_RigifyExportActionPanel(bpy.types.Panel):
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
#bl_context = "objectmode"
bl_label = "Action Manager"
bl_category = "Ucup Exporter"
@classmethod
def poll(cls, context):
return get_current_armature_object()
def draw(self, context):
draw_action_manager(self, context)
class ACTION_UL_y_action_lists(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
action_props = item.rigify_export_props
row = layout.row(align=True)
row.prop(item, 'name', text='', emboss=False, icon='ACTION')
rrow = row.row(align=True)
rrow.active = item.use_frame_range
rrow.prop(item, 'use_frame_range', text='', icon='ACTION_TWEAK')
#if item.use_frame_range:
# rrow = row.row(align=True)
# rrow.label(text='', icon='ACTION_TWEAK')
#if action_props.enable_loop and not item.use_frame_range:
# rrow = row.row(align=True)
# rrow.active = action_props.enable_skip_last_frame
# rrow.prop(action_props, 'enable_skip_last_frame', text='', icon='FRAME_PREV')
rrow = row.row(align=True)
rrow.active = action_props.enable_loop
rrow.prop(action_props, 'enable_loop', text='', icon='FILE_REFRESH')
rrow = row.row(align=True)
rrow.prop(item, 'use_fake_user', text='', emboss=False)
#rrow.prop(item, 'use_fake_user', text='')
rrow = row.row(align=True)
rrow.prop(action_props, 'enable_export', text='')
class YActionListSpecialMenu(bpy.types.Menu):
bl_idname = "ACTION_MT_y_action_list_special_menu"
bl_label = "Action Special Menu"
bl_description = "Action Special Menu"
@classmethod
def poll(cls, context):
return get_current_armature_object()
def draw(self, context):
self.layout.operator('armature.y_remove_non_transformative_frames', icon='ACTION')
def update_frame_range(self, context):
obj = get_current_armature_object()
scene = context.scene
scene_props = scene.rigify_export_props
wm_props = context.window_manager.rigify_export_props
if not scene_props.sync_frames: return
# Get action
action = bpy.data.actions[wm_props.active_action]
action_props = action.rigify_export_props
if obj.animation_data.action == action:
# Set start and end frame
if action.use_frame_range:
scene.frame_start = int(action.frame_start)
scene.frame_end = int(action.frame_end)
else:
scene.frame_start = int(action.frame_range[0])
scene.frame_end = int(action.frame_range[1])
# Skip last frame option
if action_props.enable_loop and action_props.enable_skip_last_frame:
scene.frame_end = scene.frame_end-1
def update_action(self, context):
obj = get_current_armature_object()
scene_props = context.scene.rigify_export_props
if self.active_action >= len(bpy.data.actions) or self.active_action < 0:
return
# Get action
action = bpy.data.actions[self.active_action]
action_props = action.rigify_export_props
# Reset all bone transformations first
if scene_props.sync_unkeyframed_bones:
reset_pose_bones(obj)
# Reset bone props
if scene_props.sync_rigify_props:
reset_pose_bone_props(obj, context.scene, action)
# Set action
if not obj.animation_data:
obj.animation_data_create()
obj.animation_data.action = action
# Update scene frame range
update_frame_range(action, context)
if scene_props.sync_bone_layers:
# Get all bone names related to action
bone_names = []
for fcurve in action.fcurves:
if fcurve.group and fcurve.group.name not in bone_names:
bone_names.append(fcurve.group.name)
# Get relevant layers
layers = []
for name in bone_names:
bone = obj.data.bones.get(name)
if bone:
for i in range(32):
if bone.layers[i] and i not in layers:
layers.append(i)
# Enable only relevant layers
if layers:
for i in range(32):
obj.data.layers[i] = i in layers
#print(bone_names)
class YSceneRigifyExportActionProps(bpy.types.PropertyGroup):
show_action_settings : BoolProperty(default=True)
show_global_settings : BoolProperty(default=False)
sync_bone_layers : BoolProperty(
name = 'Sync Bone Layers',
description = 'Sync bone layers when active action changes',
default = False
)
sync_frames : BoolProperty(
name = 'Sync Frames',
description = 'Sync frame start and end when active action changes',
default = False
)
sync_unkeyframed_bones : BoolProperty(
name = 'Sync Unkeyframed Bones',
description = 'Clear unkeyframed bones when changing action',
default = False
)
sync_rigify_props : BoolProperty(
name = 'Sync Unkeyframed Rigify Props',
description = 'Clear unkeyframed rigify properties when changing action',
default = False
)
default_hand_ik : BoolProperty(
name = 'Default Hand IK',
description = 'Use IK if Hand IK/FK switch is not keyframed',
default = False
)
default_foot_ik : BoolProperty(
name = 'Default Foot IK',
description = 'Use IK if Foot IK/FK switch is not keyframed',
default = True
)
default_arm_follow : BoolProperty(
name = 'Default Arm Follow',
description = 'Use Arm Limb Follow if its not keyframed',
default = False
)
default_leg_follow : BoolProperty(
name = 'Default Leg Follow',
description = 'Use Leg Limb Follow if its not keyframed',
default = False
)
class YWMRigifyExportActionProps(bpy.types.PropertyGroup):
active_action : IntProperty(default=0, update=update_action)
class YActionRigifyExportActionProps(bpy.types.PropertyGroup):
enable_export : BoolProperty(
name = 'Enable Export',
description = 'Export this action (only works on Godot for now)',
default = True
)
enable_loop : BoolProperty(
name = 'Enable Loop',
description = 'Enable animation loop (only works on Godot for now)',
default = False,
update=update_frame_range)
enable_skip_last_frame : BoolProperty(
name = 'Enable Skip Last Frame',
description = 'Enable skip the last frame (only works on Godot for now)',
default = True,
update=update_frame_range)
enable_remove_untransformed : BoolProperty(
name = 'Remove untransformed Fcurves (Export)',
description = 'remove untransformed fcurves at export',
default = False
)
untransformed_tolerance : FloatProperty(
name = 'Untransformed Keyframe Tolerance (Export)',
description = 'Untransformed keyframe tolerance',
default = 0.0001
)
def register():
bpy.utils.register_class(YNewAction)
bpy.utils.register_class(YRemoveAction)
bpy.utils.register_class(YToggleDeselectAction)
bpy.utils.register_class(YRemoveNonTransformativeFrames)
bpy.utils.register_class(YToggleActionSettings)
bpy.utils.register_class(YToggleActionGlobalSettings)
bpy.utils.register_class(UE4HELPER_PT_RigifyExportActionPanel)
bpy.utils.register_class(UE4HELPER_PT_item_RigifyExportActionPanel)
bpy.utils.register_class(YSceneRigifyExportActionProps)
bpy.utils.register_class(YWMRigifyExportActionProps)
bpy.utils.register_class(YActionRigifyExportActionProps)
bpy.utils.register_class(ACTION_UL_y_action_lists)
bpy.utils.register_class(YActionListSpecialMenu)
bpy.types.Scene.rigify_export_props = PointerProperty(type=YSceneRigifyExportActionProps)
bpy.types.Action.rigify_export_props = PointerProperty(type=YActionRigifyExportActionProps)
bpy.types.WindowManager.rigify_export_props = PointerProperty(type=YWMRigifyExportActionProps)
def unregister():
bpy.utils.unregister_class(YNewAction)
bpy.utils.unregister_class(YRemoveAction)
bpy.utils.unregister_class(YToggleDeselectAction)
bpy.utils.unregister_class(YRemoveNonTransformativeFrames)
bpy.utils.unregister_class(YToggleActionSettings)
bpy.utils.unregister_class(YToggleActionGlobalSettings)
bpy.utils.unregister_class(UE4HELPER_PT_RigifyExportActionPanel)
bpy.utils.unregister_class(UE4HELPER_PT_item_RigifyExportActionPanel)
bpy.utils.unregister_class(YSceneRigifyExportActionProps)
bpy.utils.unregister_class(YWMRigifyExportActionProps)
bpy.utils.unregister_class(YActionRigifyExportActionProps)
bpy.utils.unregister_class(ACTION_UL_y_action_lists)
bpy.utils.unregister_class(YActionListSpecialMenu)