-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrootmotion.py
1735 lines (1314 loc) · 58.1 KB
/
rootmotion.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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
bl_info = {
"name": "Rootmotion",
"author": "Likkez",
"version": (2, 0),
"blender": (4, 0, 2),
"description": "Transfer movement from feet to root. Transfer movement from root bone to object root.",
"wiki_url": "",
"category": "Animation",
}
import bpy
import mathutils
import math
#TODO: add an unroot for object mode.
#TODO: review the code, make it less retarded.
def spawn_empty(name, display_type='PLAIN_AXES', size=1.0):
empty = bpy.data.objects.new( name, None )
bpy.context.scene.collection.objects.link( empty )
empty.empty_display_size = .2
empty.empty_display_type = display_type
empty.empty_display_size = size
return empty
def get_master(bone):
while bone.parent!=None:
bone=bone.parent
return bone
def spawn_armature(name,location):
arm=bpy.data.armatures.new(name)
arm_obj=bpy.data.objects.new(name, arm)
bpy.context.scene.collection.objects.link(arm_obj)
arm_obj.location=location
return arm_obj
def key_insert(posebone, Loc = True, Rot = True, Scale = True):
if Loc:
posebone.keyframe_insert(data_path="location")
if Rot:
if posebone.rotation_mode == "QUATERNION":
posebone.keyframe_insert(data_path="rotation_quaternion")
else:
posebone.keyframe_insert(data_path="rotation_euler")
if Scale:
posebone.keyframe_insert(data_path="scale")
def rootmotion_create_proxy(arm, master_bone, no_master_anim=True):
# #save armature layers
# arm_layers=[]
# for layer in arm.data.layers:
# arm_layers.append(layer)
scene = bpy.context.scene
children=[]
for bone in master_bone.children:
children.append(bone)
for bone in arm.pose.bones:
if (not bone.parent) and (bone!=master_bone):
for pbone in bone.children:
if (not pbone in children):
children.append(pbone)
all_bones=children.copy()
all_bones.append(master_bone)
#create armature
bpy.ops.object.mode_set(mode='OBJECT')
arm_rm=spawn_armature(arm.name + 'ROOTMOTION_REMOVEME_qwernoinsgSsda',(0,0,0))
arm_rm.matrix_world=arm.matrix_world
bpy.ops.object.select_all(action='DESELECT')
arm_rm.select_set(True)
bpy.context.view_layer.objects.active=arm_rm
bpy.ops.object.mode_set(mode='EDIT')
for bone in all_bones:
edit_bone=arm_rm.data.edit_bones.new(bone.name)
edit_bone.tail=(0,0,5)
edit_bone.head=(0,0,0)
bpy.ops.object.mode_set(mode='POSE')
for bone in arm_rm.pose.bones:
try:
arm.pose.bones[bone.name]
# if bone.name!=master_bone.name:
constr=bone.constraints.new('COPY_TRANSFORMS')
constr.target=arm
constr.subtarget=bone.name
except:
pass
#apply pose
bpy.ops.pose.armature_apply(selected=False)
#remove contraint from the master
if no_master_anim:
armrm_master = arm_rm.pose.bones.get(master_bone.name)
for constr in armrm_master.constraints:
armrm_master.constraints.remove(constr)
arm.data.pose_position='POSE'
#bake action for arm_rm
bpy.ops.nla.bake(frame_start=bpy.context.scene.frame_start, frame_end=bpy.context.scene.frame_end, only_selected=False, visual_keying=True, clear_constraints=True, bake_types={'POSE'})
#add constraints to required bones
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
arm.select_set(True)
bpy.context.view_layer.objects.active=arm
bpy.ops.object.mode_set(mode='POSE')
# bpy.ops.armature.collection_show_all()
bpy.ops.pose.select_all(action='DESELECT')
for bone in all_bones:
if bone.name!=master_bone.name:
bone.bone.select=True
constr=bone.constraints.new('COPY_TRANSFORMS')
constr.target=arm_rm
constr.subtarget=bone.name
constr.name="ROOTMOTION_REMOVEME_qwernoinsgSsda"
#restore layers on armature
# for idx,layer in enumerate(arm_layers):
# arm.data.layers[idx]=layer
#select root bone
bpy.ops.pose.select_all(action='DESELECT')
master_bone.bone.select = True
#hide proxy armature
arm_rm.hide_viewport=True
return arm_rm
def bakebones(arm, phrase='ROOTMOTION_REMOVEME_qwernoinsgSsda'):
bpy.ops.object.mode_set(mode='POSE')
bpy.ops.pose.select_all(action='DESELECT')
for bone in arm.pose.bones:
for constr in bone.constraints:
if phrase in constr.name:
bone.bone.select=True
pass
#bake action for armature
bpy.ops.nla.bake(frame_start=bpy.context.scene.frame_start, frame_end=bpy.context.scene.frame_end, only_selected=True, visual_keying=True, clear_constraints=False, use_current_action=True, bake_types={'POSE'})
#remove constraints
for bone in arm.pose.bones:
for constr in bone.constraints:
if phrase in constr.name:
bone.constraints.remove(constr)
def rootmotion_remove_proxy(arm):
# #save armature layers
# arm_layers=[]
# for layer in arm.data.layers:
# arm_layers.append(layer)
bakebones(arm)
#delete temp armature
arm_rm = bpy.data.objects[arm.name + 'ROOTMOTION_REMOVEME_qwernoinsgSsda']
if arm_rm.animation_data:
bpy.data.actions.remove(arm_rm.animation_data.action)
arm_rm_arm=arm_rm.data
bpy.data.objects.remove(arm_rm)
bpy.data.armatures.remove(arm_rm_arm)
# #restore layers on armature
# for idx,layer in enumerate(arm_layers):
# arm.data.layers[idx]=layer
def getrootbone_selection():
rootobject = bpy.data.objects.get(bpy.context.scene.RT_root_object)
if rootobject:
if rootobject.type == 'ARMATURE':
return rootobject.pose.bones.get(bpy.context.scene.RT_root_bone)
return None
def valid_curve(context, obj, fcurve, ignore_hidden = True, internal_valid = False):
if fcurve.is_valid and internal_valid or (not ((ignore_hidden and fcurve.hide) or fcurve.lock or fcurve.is_empty)):
if 'pose.bones' in fcurve.data_path:
split_data_path = fcurve.data_path.split(sep='"')
if len(split_data_path) > 1:
bone_name = split_data_path[1]
bone = obj.pose.bones.get(bone_name)
if bone and (bone in context.selected_pose_bones):
return True
return False
#TODO: Rewrite this whole chunk, its too bloated. Make it use the rootmotion_create_proxy, rootmotion_remove_proxy
class RT_OT_rootmotion(bpy.types.Operator):
bl_description = "Select all feet bones and run. Alternatively, select the Root bone"
bl_idname = 'rootmotion.rootmotion'
bl_label = "RootMotion"
bl_options = set({'REGISTER', 'UNDO'})
def execute(self, context):
scene=bpy.context.scene
up_axis=scene.RT_up_axis
keep_offset=scene.RT_keep_offset
current_positions=scene.RT_current_positions
rotation_offset=scene.RT_rotation_offset
enable_rotation=scene.RT_rotate_root
limit_location=scene.RT_limit_location
imprefect_hierarchy=True
arm=bpy.context.selected_editable_objects[0]
foot_list=bpy.context.selected_pose_bones.copy() ######
master_bone=get_master(foot_list[0]) ####
children=[]
for bone in master_bone.children:
children.append(bone)
if imprefect_hierarchy:
for bone in arm.pose.bones:
if (not bone.parent) and (bone!=master_bone):
for pbone in bone.children:
children.append(pbone)
all_bones=children.copy()
all_bones.append(master_bone)
if not current_positions:
bpy.context.scene.frame_set(bpy.context.scene.frame_start)
# #save armature layers
# arm_layers=[]
# for layer in arm.data.layers:
# arm_layers.append(layer)
if not current_positions:
arm.data.pose_position='REST'
#check if root bone is selected or not
if foot_list[0].parent:
constraints_mute=[]
for bone in all_bones:
if bone.constraints:
for constr in bone.constraints:
constraints_mute.append(constr.mute)
constr.mute=True
#create armature
bpy.ops.object.mode_set(mode='OBJECT')
arm_rm=spawn_armature('Rootmotion',(0,0,0))
arm_rm.matrix_world=arm.matrix_world
bpy.ops.object.select_all(action='DESELECT')
arm_rm.select_set(True)
bpy.context.view_layer.objects.active=arm_rm
bpy.ops.object.mode_set(mode='EDIT')
feet_bone=arm_rm.data.edit_bones.new('feet_bone')
feet_bone.tail=(0,0,5)
feet_bone.head=(0,0,0)
#bpy.ops.object.mode_set(mode='POSE')
for bone in all_bones:
edit_bone=arm_rm.data.edit_bones.new(bone.name)
edit_bone.tail=(0,0,5)
edit_bone.head=(0,0,0)
arm_rm.data.edit_bones[master_bone.name].parent=feet_bone
bpy.ops.object.mode_set(mode='POSE')
for bone in arm_rm.pose.bones:
try:
arm.pose.bones[bone.name]
constr=bone.constraints.new('COPY_TRANSFORMS')
constr.target=arm
constr.subtarget=bone.name
except:
pass
master_bone_temp=arm_rm.pose.bones[master_bone.name]
#feet constraints
feet_bone = arm_rm.pose.bones['feet_bone']
#check if rotation is enabled
foot_constraints=['COPY_ROTATION', 'COPY_LOCATION']
if not enable_rotation:
foot_constraints=['COPY_LOCATION']
for idx, foot in enumerate(foot_list):
for constr_type in foot_constraints:
constr=feet_bone.constraints.new(constr_type)
constr.target=arm
constr.subtarget=foot.name
if constr_type=='COPY_ROTATION':
constr.use_y=False
constr.use_x=False
if idx!=0:
constr.influence=1/len(foot_list)
#lock to floor
if limit_location:
constr=feet_bone.constraints.new('FLOOR')
constr.target=arm
constr.subtarget=''
constr.floor_location='FLOOR_NEGATIVE_Z'
constr=feet_bone.constraints.new('FLOOR')
constr.target=arm
constr.subtarget=''
constr.floor_location='FLOOR_Z'
#master offset constraint
if not keep_offset:
constr=master_bone_temp.constraints.new('COPY_LOCATION')
constr.target=arm_rm
constr.subtarget=feet_bone.name
constr.owner_space='WORLD'
#apply pose
bpy.ops.pose.armature_apply(selected=False)
for constr in master_bone_temp.constraints:
master_bone_temp.constraints.remove(constr)
#master constraints (lock to floor, limit rotation)
if limit_location:
constr=master_bone_temp.constraints.new('FLOOR')
constr.target=arm
constr.subtarget=''
constr.floor_location='FLOOR_NEGATIVE_Z'
constr=master_bone_temp.constraints.new('FLOOR')
constr.target=arm
constr.subtarget=''
constr.floor_location='FLOOR_Z'
# constr=master_bone_temp.constraints.new('LIMIT_ROTATION')
# constr.owner_space='LOCAL_WITH_PARENT'
# constr.use_limit_x = not up_axis == 'X'
# constr.use_limit_y = not up_axis == 'Y'
# constr.use_limit_z = not up_axis == 'Z'
#apply rotation offset to master
master_bone_temp.rotation_mode='XYZ'
master_bone_temp.rotation_euler=((rotation_offset*(up_axis=='X')),
(rotation_offset*(up_axis=='Y')),
(rotation_offset*(up_axis=='Z')))
arm.data.pose_position='POSE'
#bake action for feet_bone
bpy.ops.nla.bake(frame_start=bpy.context.scene.frame_start, frame_end=bpy.context.scene.frame_end, only_selected=False, visual_keying=True, clear_constraints=True, bake_types={'POSE'})
#add constraints to required bones
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
arm.select_set(True)
bpy.context.view_layer.objects.active=arm
bpy.ops.object.mode_set(mode='POSE')
# bpy.ops.armature.collection_show_all()
bpy.ops.pose.select_all(action='DESELECT')
copy_transforms_constraints=[]
for bone in all_bones:
bone.bone.select=True
constr=bone.constraints.new('COPY_TRANSFORMS')
constr.target=arm_rm
constr.subtarget=bone.name
copy_transforms_constraints.append(constr)
#bake action for armature
bpy.ops.nla.bake(frame_start=bpy.context.scene.frame_start, frame_end=bpy.context.scene.frame_end, only_selected=True, visual_keying=True, clear_constraints=False, use_current_action=True, bake_types={'POSE'})
#delete copy transforms constraints and unmute others
i=0
for idx,bone in enumerate(all_bones):
if bone.constraints:
bone.constraints.remove(copy_transforms_constraints[idx])
if bone.constraints:
for constr in bone.constraints:
constr.mute=constraints_mute[i]
i+=1
#transfering animation from root to object
else:
constraints_mute=[]
if master_bone.constraints:
for constr in master_bone.constraints:
constraints_mute.append(constr.mute)
constr.mute=True
bpy.ops.object.mode_set(mode='OBJECT')
empty_ct=spawn_empty('Rootmotion1')
constr=empty_ct.constraints.new('COPY_TRANSFORMS')
constr.target=arm
constr.subtarget=master_bone.name
bpy.context.view_layer.update()
arm_rm=spawn_armature('RootMotion_ObjectTransforms',(0,0,0))
arm_rm.parent=empty_ct
arm_rm.matrix_world=arm.matrix_world
bpy.ops.object.select_all(action='DESELECT')
arm_rm.select_set(True)
bpy.context.view_layer.objects.active=arm_rm
bpy.ops.object.mode_set(mode='EDIT')
master_bone_temp=arm_rm.data.edit_bones.new(master_bone.name)
master_bone_temp.tail=(0,0,5)
master_bone_temp.head=(0,0,0)
bpy.ops.object.mode_set(mode='POSE')
master_bone_temp=arm_rm.pose.bones[0]
constr=master_bone_temp.constraints.new('COPY_TRANSFORMS')
constr.target=arm
constr.subtarget=master_bone.name
#apply pose
bpy.ops.pose.armature_apply(selected=False)
master_bone_temp.constraints.remove(constr)
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
arm.data.pose_position='POSE'
empty_ct.select_set(True)
bpy.context.view_layer.objects.active=empty_ct
#bake action for empty
bpy.ops.nla.bake(frame_start=bpy.context.scene.frame_start, frame_end=bpy.context.scene.frame_end, only_selected=True, visual_keying=True, clear_constraints=True, use_current_action=True, bake_types={'OBJECT'})
bpy.ops.object.select_all(action='DESELECT')
arm.data.pose_position='POSE'
arm.select_set(True)
bpy.context.view_layer.objects.active=arm
constr=arm.constraints.new('COPY_TRANSFORMS')
constr.target=arm_rm
constr.subtarget=''
constr_master_transforms=master_bone.constraints.new('COPY_TRANSFORMS')
constr_master_transforms.target=arm_rm
constr_master_transforms.subtarget=master_bone.name
#bake action for armature
bpy.ops.nla.bake(frame_start=bpy.context.scene.frame_start, frame_end=bpy.context.scene.frame_end, only_selected=True, visual_keying=True, clear_constraints=True, use_current_action=True, bake_types={'OBJECT'})
bpy.ops.object.mode_set(mode='POSE')
bpy.ops.pose.select_all(action='DESELECT')
master_bone.bone.select=True
#bake root bone
bpy.ops.nla.bake(frame_start=bpy.context.scene.frame_start, frame_end=bpy.context.scene.frame_end, only_selected=True, visual_keying=True, clear_constraints=False, use_current_action=True, bake_types={'POSE'})
master_bone.constraints.remove(constr_master_transforms)
if master_bone.constraints:
i=0
for idx, constr in enumerate(master_bone.constraints):
constr.mute=constraints_mute[i]
i+=1
#delete empty
if empty_ct.animation_data:
bpy.data.actions.remove(empty_ct.animation_data.action)
bpy.data.objects.remove(empty_ct)
#delete temp armature
if arm_rm.animation_data:
bpy.data.actions.remove(arm_rm.animation_data.action)
arm_rm_arm=arm_rm.data
bpy.data.objects.remove(arm_rm)
bpy.data.armatures.remove(arm_rm_arm)
# #restore layers on armature
# for idx,layer in enumerate(arm_layers):
# arm.data.layers[idx]=layer
return {'FINISHED'}
class RT_OT_unroot(bpy.types.Operator):
bl_description = "This transfers motion from root to it's children"
bl_idname = 'rootmotion.unroot'
bl_label = "UnRoot"
bl_options = set({'REGISTER', 'UNDO'})
def execute(self, context):
scene=bpy.context.scene
arm=bpy.context.selected_editable_objects[0]
master_bone = getrootbone_selection()
if not master_bone:
return {'CANCELLED'}
arm.data.pose_position='REST'
bpy.context.scene.frame_set(bpy.context.scene.frame_start)
arm_rm = rootmotion_create_proxy(arm, master_bone)
constr=master_bone.constraints.new('COPY_TRANSFORMS')
constr.target=arm_rm
constr.subtarget=master_bone.name
constr.name="ROOTMOTION_REMOVEME_qwernoinsgSsda"
rootmotion_remove_proxy(arm)
return {'FINISHED'}
class RT_OT_SetRootBone(bpy.types.Operator):
bl_description = "Select root bone from Active"
bl_idname = 'rootmotion.setrootbone'
bl_label = "FromActive"
bl_options = set({'REGISTER'})
def execute(self, context):
if context.active_pose_bone:
context.scene.RT_root_object = context.object.name
context.scene.RT_root_bone = context.active_pose_bone.name
return {'FINISHED'}
else:
return {'CANCELLED'}
class RT_OT_ToObject(bpy.types.Operator):
bl_description = "This transfers Root animation to Object mode animation"
bl_idname = 'rootmotion.toobject'
bl_label = "ToObject"
bl_options = set({'REGISTER', 'UNDO'})
def execute(self, context):
scene=bpy.context.scene
arm=bpy.context.selected_editable_objects[0]
master_bone = getrootbone_selection()
if not master_bone:
return {'CANCELLED'}
bpy.context.scene.frame_set(bpy.context.scene.frame_start)
bpy.ops.object.mode_set(mode='OBJECT')
arm.data.pose_position='REST'
context.view_layer.update()
#spawn in bone empty
empty_bone=spawn_empty('Rootmotion_Bone')
empty_bone.matrix_world = arm.matrix_world @ master_bone.matrix
#spawn in object empty
empty_obj=spawn_empty('Rootmotion_Obj')
empty_obj.parent = empty_bone
empty_obj.matrix_parent_inverse = empty_bone.matrix_world.inverted()
empty_obj.matrix_world = arm.matrix_world
#copy animation to empty
constr=empty_bone.constraints.new('COPY_TRANSFORMS')
constr.target=arm
constr.subtarget=master_bone.name
arm.data.pose_position='POSE'
#bake action for empty
bpy.ops.object.select_all(action='DESELECT')
empty_bone.select_set(True)
bpy.context.view_layer.objects.active=empty_bone
bpy.ops.nla.bake(frame_start=bpy.context.scene.frame_start, frame_end=bpy.context.scene.frame_end, only_selected=True, visual_keying=True, clear_constraints=True, use_current_action=True, bake_types={'OBJECT'})
#copy animation to armature object mode
constr=arm.constraints.new('COPY_TRANSFORMS')
constr.target=empty_obj
bpy.ops.object.select_all(action='DESELECT')
arm.select_set(True)
bpy.context.view_layer.objects.active=arm
bpy.ops.nla.bake(frame_start=bpy.context.scene.frame_start, frame_end=bpy.context.scene.frame_end, only_selected=True, visual_keying=True, clear_constraints=True, use_current_action=True, bake_types={'OBJECT'})
#clear animation on armature root bone
bpy.ops.object.mode_set(mode='POSE')
bpy.ops.pose.select_all(action='DESELECT')
master_bone.bone.select = True
constr=master_bone.constraints.new('COPY_TRANSFORMS')
constr.target=empty_bone
bpy.ops.nla.bake(frame_start=bpy.context.scene.frame_start, frame_end=bpy.context.scene.frame_end, only_selected=True, visual_keying=True, clear_constraints=True, use_current_action=True, bake_types={'POSE'})
#cleanup
if empty_obj.animation_data:
bpy.data.actions.remove(empty_obj.animation_data.action)
bpy.data.objects.remove(empty_obj)
if empty_bone.animation_data:
bpy.data.actions.remove(empty_bone.animation_data.action)
bpy.data.objects.remove(empty_bone)
return {'FINISHED'}
class RT_OT_SetRootBone(bpy.types.Operator):
bl_description = "Select root bone from Active"
bl_idname = 'rootmotion.setrootbone'
bl_label = "FromActive"
bl_options = set({'REGISTER'})
def execute(self, context):
if context.active_pose_bone:
context.scene.RT_root_object = context.object.name
context.scene.RT_root_bone = context.active_pose_bone.name
return {'FINISHED'}
else:
return {'CANCELLED'}
# class RT_OT_SetKeyRange(bpy.types.Operator):
# bl_description = "Set range to apply effect to."
# bl_idname = 'rootmotion.setkeyrange'
# bl_label = "SetStartEnd"
# bl_options = set({'REGISTER'})
# def execute(self, context):
# if context.active_pose_bone:
# context.scene.RT_root_object = context.object.name
# context.scene.RT_root_bone = context.active_pose_bone.name
# return {'FINISHED'}
# else:
# return {'CANCELLED'}
class RT_OT_unslide(bpy.types.Operator):
bl_description = "Select the bones touching the floor. The operator moves the parent to make the bone stable."
bl_idname = 'rootmotion.unslide'
bl_label = "UnSlide"
bl_options = set({'REGISTER', 'UNDO'})
Limit_X: bpy.props.BoolProperty(
name="Limit X",
description="Don't move along X axis",
default=False
)
Limit_Y: bpy.props.BoolProperty(
name="Limit Y",
description="Don't move along Y axis",
default=False
)
Limit_Z: bpy.props.BoolProperty(
name="Limit Z",
description="Don't move along Z axis",
default=False
)
def execute(self, context):
# arm = bpy.context.object
targetbone = getrootbone_selection()
if (not targetbone) or (not bpy.context.selected_pose_bones):
return {'CANCELLED'}
#check if root is also selected
for bone in bpy.context.selected_pose_bones:
if bone == targetbone:
targetbone.bone.select=False
break
numbones = len(bpy.context.selected_pose_bones)
#check if step is negative
direction = 1
if context.scene.RT_step_size < 0:
direction = -1
for iter_idx in range(0, context.scene.RT_step_size, direction):
bpy.context.view_layer.update()
#targetbone insert keyframe
targetbone.keyframe_insert(data_path="location")
#frame 1 matrices
f1_bone2_gloc = mathutils.Vector((0,0,0))
for bone in bpy.context.selected_pose_bones:
arm = bone.id_data
f1_bone2_gloc += arm.convert_space(pose_bone=bone, matrix=bone.matrix.copy(), from_space='POSE', to_space='WORLD').decompose()[0] / numbones
bpy.context.scene.frame_set(bpy.context.scene.frame_current+direction)
#frame 2 matrices
f2_bone2_gloc = mathutils.Vector((0,0,0))
for bone in bpy.context.selected_pose_bones:
arm = bone.id_data
f2_bone2_gloc += arm.convert_space(pose_bone=bone, matrix=bone.matrix.copy(), from_space='POSE', to_space='WORLD').decompose()[0] / numbones
#find transform of bone2
limit = mathutils.Vector((not self.Limit_X,not self.Limit_Y,not self.Limit_Z))
transform = (f1_bone2_gloc-f2_bone2_gloc)*limit
##apply location to targetbone
arm = targetbone.id_data
mat_pose=arm.convert_space(pose_bone=targetbone, matrix=targetbone.matrix.copy(), from_space='POSE', to_space='WORLD')
mat_pose=mat_pose + mathutils.Matrix.Translation(transform)
mat_pose=arm.convert_space(pose_bone=targetbone, matrix=mat_pose, from_space='WORLD', to_space='LOCAL')
targetbone_loc = mat_pose.decompose()[0]
targetbone.location = targetbone_loc
#targetbone insert keyframe
targetbone.keyframe_insert(data_path="location")
return {'FINISHED'}
class RT_OT_TempParent(bpy.types.Operator):
bl_description = "Follow selected bone like a parent."
bl_idname = 'rootmotion.tempparent'
bl_label = "TempParent"
bl_options = set({'REGISTER', 'UNDO'})
def execute(self, context):
targetbone = getrootbone_selection()
if (not targetbone) or (not context.selected_pose_bones):
return {'CANCELLED'}
#check if step is negative
direction = 1
if context.scene.RT_step_size < 0:
direction = -1
for iter_idx in range(0, context.scene.RT_step_size, direction):
context.view_layer.update()
# #targetbone insert keyframe
key_insert(targetbone)
bone = context.active_pose_bone
arm1 = bone.id_data
arm2 = targetbone.id_data
matrix_1 = arm1.convert_space(pose_bone=bone, matrix=bone.matrix.copy(), from_space='POSE', to_space='WORLD')
matrix_2 = arm2.convert_space(pose_bone=targetbone, matrix=targetbone.matrix.copy(), from_space='POSE', to_space='WORLD')
matrix_1.invert()
matrix_diff = matrix_1 @ matrix_2
context.scene.frame_set(bpy.context.scene.frame_current+direction)
matrix_trg = arm1.convert_space(pose_bone=bone, matrix=bone.matrix.copy(), from_space='POSE', to_space='WORLD') @ matrix_diff
targetbone.matrix = arm2.convert_space(pose_bone=targetbone, matrix=matrix_trg, from_space='WORLD', to_space='POSE')
# #targetbone insert keyframe
key_insert(targetbone)
return {'FINISHED'}
class RT_OT_continue(bpy.types.Operator):
bl_description = "Continue the root motion to the next frame."
bl_idname = 'rootmotion.continue'
bl_label = "Continue"
bl_options = set({'REGISTER', 'UNDO'})
Limit_X: bpy.props.BoolProperty(
name="Limit X",
description="Don't move along X axis",
default=False
)
Limit_Y: bpy.props.BoolProperty(
name="Limit Y",
description="Don't move along Y axis",
default=False
)
Limit_Z: bpy.props.BoolProperty(
name="Limit Z",
description="Don't move along Z axis",
default=False
)
def execute(self, context):
arm = bpy.context.object
targetbone = getrootbone_selection()
if not targetbone:
return {'CANCELLED'}
# #targetbone insert keyframe
# targetbone.keyframe_insert(data_path="location")
#check if step is negative
direction = 1
if context.scene.RT_step_size < 0:
direction = -1
for iter_idx in range(0, context.scene.RT_step_size, direction):
bpy.context.view_layer.update()
#frame 1 matrices
loc1 = targetbone.location.copy()
bpy.context.scene.frame_set(bpy.context.scene.frame_current+((-1)*direction))
#frame 2 matrices
loc2 = targetbone.location.copy()
bpy.context.scene.frame_set(bpy.context.scene.frame_current+(2*direction))
#find transform of bone2
limit = mathutils.Vector((not self.Limit_X,not self.Limit_Y,not self.Limit_Z))
transform = (loc1-loc2)*limit
#apply location to targetbone
targetbone.location += transform
#targetbone insert keyframe
targetbone.keyframe_insert(data_path="location")
return {'FINISHED'}
class RT_OT_snap(bpy.types.Operator):
bl_description = "Snap a bone to the floor. Can select multiple bones to use as a reference (Active bone gets snapped, reference bones are used to measure distance to the floor)."
bl_idname = 'rootmotion.snap'
bl_label = "SnapToFloor"
bl_options = set({'REGISTER', 'UNDO'})
SourceAxis: bpy.props.EnumProperty(
items =[("0", "X", ""),
("1", "Y", ""),
("2", "Z", "")],
default='2',
name = "Source Axis",
description="Axis to use for calculating distance."
)
TargetAxis: bpy.props.EnumProperty(
items =[("0", "X", ""),
("1", "Y", ""),
("2", "Z", "")],
default='2',
name = "Target Axis",
description="Axis to use for applying calculated distance."
)
Invert: bpy.props.BoolProperty(
name="Invert Target",
description="Invert target axis",
default=False
)
Floor: bpy.props.FloatProperty(
name="Floor Z Value",
description="",
default=0.0
)
NumIters: bpy.props.IntProperty(
name="Number of Iterations",
description="Increase this number if you are trying to land IK knees.",
default=1,
min=1,
soft_max=20
)
IterInfluence: bpy.props.FloatProperty(
name="Influence of an iteration.",
description="Can increase accuracy when remapping between different axis. (If iterations are set to 1 do not change this value.)",
default=1.0,
min=0.0,
max=1.0
)
Step: bpy.props.BoolProperty(
name="Step Forward and Key",
description="Step one frame forward after running and insert a keyframe.",
default=False
)
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
arm = bpy.context.object
num_refbones=len(bpy.context.selected_pose_bones)-1
bone1=bpy.context.active_pose_bone
#check if step is negative
direction = 1
if context.scene.RT_step_size < 0:
direction = -1
for iter_idx in range(0, context.scene.RT_step_size, direction):
for i in range(0,self.NumIters):
bpy.context.view_layer.update()
transform=mathutils.Vector((0,0,0))
# bone1_gloc, temp, temp = arm.convert_space(pose_bone=bone1, matrix=bone1.matrix.copy(), from_space='POSE', to_space='WORLD').decompose()
#handling multiple bones.
for bone in bpy.context.selected_pose_bones:
if (num_refbones!=0) and (bone==bone1):
continue
else:
bone_gloc, temp, temp = arm.convert_space(pose_bone=bone, matrix=bone.matrix.copy(), from_space='POSE', to_space='WORLD').decompose()
transform = ((transform - bone_gloc) + mathutils.Vector(((self.SourceAxis=='0') * self.Floor,(self.SourceAxis=='1') * self.Floor,(self.SourceAxis=='2') * self.Floor))) * self.IterInfluence
#calculate offset