-
Notifications
You must be signed in to change notification settings - Fork 82
/
rigutils.py
2267 lines (1902 loc) · 81.9 KB
/
rigutils.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
# Copyright (C) 2021 Victor Soupday
# This file is part of CC/iC Blender Tools <https://github.com/soupday/cc_blender_tools>
#
# CC/iC Blender Tools is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# CC/iC Blender Tools is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with CC/iC Blender Tools. If not, see <https://www.gnu.org/licenses/>.
import bpy
from mathutils import Vector, Matrix, Quaternion, Euler
from random import random
import re
from . import springbones, bones, modifiers, rigify_mapping_data, utils, vars
def edit_rig(rig):
if rig and utils.edit_mode_to(rig):
return True
utils.log_error(f"Unable to edit rig: {rig}!")
return False
def select_rig(rig):
if rig and utils.object_mode_to(rig):
return True
utils.log_error(f"Unable to select rig: {rig}!")
return False
def pose_rig(rig):
if rig and utils.pose_mode_to(rig):
return True
utils.log_error(f"Unable to pose rig: {rig}!")
return False
def name_in_data_paths(action, name):
for fcurve in action.fcurves:
if name in fcurve.data_path:
return True
return False
def name_in_pose_bone_data_paths_regex(action, name):
name = ".*" + name
for fcurve in action.fcurves:
if re.match(name, fcurve.data_path):
return True
return False
def bone_name_in_armature_regex(arm, name):
for bone in arm.data.bones:
if re.match(name, bone.name):
return True
return False
def is_G3_action(action):
if action:
if len(action.fcurves) > 0:
for bone_name in rigify_mapping_data.CC3_BONE_NAMES:
if not name_in_data_paths(action, bone_name):
return False
return True
return False
def is_G3_armature(armature):
if armature:
if len(armature.data.bones) > 0:
for bone_name in rigify_mapping_data.CC3_BONE_NAMES:
if bone_name not in armature.data.bones:
return False
return True
return False
def is_iClone_action(action):
if action:
if len(action.fcurves) > 0:
for bone_name in rigify_mapping_data.ICLONE_BONE_NAMES:
if not name_in_data_paths(action, bone_name):
return False
return True
return False
def is_iClone_armature(armature):
if armature:
if len(armature.data.bones) > 0:
for bone_name in rigify_mapping_data.ICLONE_BONE_NAMES:
if bone_name not in armature.data.bones:
return False
return True
return False
def is_ActorCore_action(action):
if action:
if len(action.fcurves) > 0:
for bone_name in rigify_mapping_data.ACTOR_CORE_BONE_NAMES:
if not name_in_data_paths(action, bone_name):
return False
return True
return False
def is_ActorCore_armature(armature):
if armature:
if len(armature.data.bones) > 0:
for bone_name in rigify_mapping_data.ACTOR_CORE_BONE_NAMES:
if bone_name not in armature.data.bones:
return False
return True
return False
def is_GameBase_action(action):
if action:
if len(action.fcurves) > 0:
for bone_name in rigify_mapping_data.GAME_BASE_BONE_NAMES:
if not name_in_data_paths(action, bone_name):
return False
return True
return False
def is_GameBase_armature(armature):
if armature:
if len(armature.data.bones) > 0:
for bone_name in rigify_mapping_data.GAME_BASE_BONE_NAMES:
if bone_name not in armature.data.bones:
return False
return True
return False
def is_Mixamo_action(action):
if action:
if len(action.fcurves) > 0:
for bone_name in rigify_mapping_data.MIXAMO_BONE_NAMES:
if not name_in_pose_bone_data_paths_regex(action, bone_name):
return False
return True
return False
def is_Mixamo_armature(armature):
if armature:
if len(armature.data.bones) > 0:
for bone_name in rigify_mapping_data.MIXAMO_BONE_NAMES:
if not bone_name_in_armature_regex(armature, bone_name):
return False
return True
return False
def is_rigify_action(action):
if action:
if len(action.fcurves) > 0:
for bone_name in rigify_mapping_data.RIGIFY_BONE_NAMES:
if not name_in_data_paths(action, bone_name):
return False
return True
return False
def is_rigify_armature(armature):
if armature:
if len(armature.data.bones) > 0:
for bone_name in rigify_mapping_data.RIGIFY_BONE_NAMES:
if bone_name not in armature.data.bones:
return False
return True
return False
def is_rl_rigify_action(action):
if action:
if len(action.fcurves) > 0:
for bone_name in rigify_mapping_data.RL_RIGIFY_BONE_NAMES:
if not name_in_data_paths(action, bone_name):
return False
return True
return False
def is_rl_rigify_armature(armature):
if armature:
if len(armature.data.bones) > 0:
for bone_name in rigify_mapping_data.RL_RIGIFY_BONE_NAMES:
if bone_name not in armature.data.bones:
return False
return True
return False
def is_rl_armature(armature):
if (is_ActorCore_armature(armature) or
is_G3_armature(armature) or
is_GameBase_armature(armature) or
is_iClone_armature(armature)):
return True
return False
def get_rig_generation(armature):
if is_ActorCore_armature(armature):
return "ActorCore"
elif is_G3_armature(armature):
return "G3"
elif is_GameBase_armature(armature):
return "GameBase"
elif is_iClone_armature(armature):
return "G3"
else:
return "Unknown"
def is_unity_action(action):
return "_Unity" in action.name and "|A|" in action.name
def get_armature_action_source_type(armature, action=None):
if armature and not action and armature.type == "ARMATURE":
if is_G3_armature(armature):
return "G3", "G3 (CC3/CC3+)"
if is_iClone_armature(armature):
return "G3", "G3 (iClone)"
if is_ActorCore_armature(armature):
return "G3", "G3 (ActorCore)"
if is_GameBase_armature(armature):
return "GameBase", "GameBase (CC3/CC3+)"
if is_Mixamo_armature(armature):
return "Mixamo", "Mixamo"
if is_rl_rigify_armature(armature):
return "Rigify+", "Rigify+"
if is_rigify_armature(armature):
return "Rigify", "Rigify"
if armature and action and armature.type == "ARMATURE":
if is_G3_armature(armature) and is_G3_action(action):
return "G3", "G3 (CC3/CC3+)"
if is_iClone_armature(armature) and is_iClone_action(action):
return "G3", "G3 (iClone)"
if is_ActorCore_armature(armature) and is_ActorCore_action(action):
return "G3", "G3 (ActorCore)"
if is_GameBase_armature(armature) and is_GameBase_action(action):
return "GameBase", "GameBase (CC3/CC3+)"
if is_Mixamo_armature(armature) and is_Mixamo_action(action):
return "Mixamo", "Mixamo"
if is_rl_rigify_armature(armature) and is_rl_rigify_action(action):
return "Rigify+", "Rigify+"
if is_rigify_armature(armature) and is_rigify_action(action):
return "Rigify", "Rigify"
# detect other types as they become available...
return "Unknown", "Unknown"
def find_source_actions(source_action, source_rig=None):
src_set_id, src_set_gen, src_type_id, src_object_id = get_motion_set(source_action)
src_prefix, src_rig_id, src_type_id, src_object_id, src_motion_id = decode_action_name(source_action)
if not src_motion_id:
src_motion_id = get_action_motion_id(source_action)
actions = {
"motion_info": {
"prefix": src_prefix,
"rig_id": src_rig_id,
"motion_id": src_motion_id,
"set_id": src_set_id,
"set_generation": src_set_gen,
},
"count": 0,
"armature": None,
"keys": {},
}
# try matching actions by set_id (disabled for now: testing name patterns first)
if src_set_id:
utils.log_info(f"Looking for motion set id: {src_set_id}")
for action in bpy.data.actions:
if "rl_set_id" in action:
set_id, set_gen, type_id, object_id = get_motion_set(action)
if set_id == src_set_id:
if type_id == "KEY":
utils.log_info(f" - Found shape-key action: {action.name} for {object_id}")
actions["keys"][object_id] = action
elif type_id == "ARM":
utils.log_info(f" - Found armature action: {action.name}")
actions["armature"] = action
return actions
# try matching actions by action name pattern
if src_type_id and src_motion_id:
# match actions by name pattern
utils.log_info(f"Looking for shape-key actions matching: [<{src_prefix}>|]{src_rig_id}|[K|A]|[<obj>|]{src_motion_id}")
for action in bpy.data.actions:
motion_prefix, rig_id, type_id, object_id, motion_id = decode_action_name(action)
if (motion_id and object_id not in actions and
motion_prefix == src_prefix and
rig_id == src_rig_id and
utils.partial_match(motion_id, src_motion_id)):
if type_id == "K":
utils.log_info(f" - Found shape-key action: {action.name} for {object_id}")
actions["keys"][object_id] = action
elif type_id == "A":
utils.log_info(f" - Found armature action: {action.name}")
actions["armature"] = action
return actions
# try to fetch shape-key actions from source armature child objects, if supplied
elif source_rig:
utils.log_info(f"Looking for shape-key actions in armature child objects: {source_rig.name}")
action = utils.safe_get_action(source_rig)
if action:
utils.log_info(f" - Found armature action: {action.name}")
actions["armature"] = action
for obj in source_rig.children:
obj_id = get_action_obj_id(obj)
if obj.type == "MESH":
action = utils.safe_get_action(obj.data.shape_keys)
if action:
utils.log_info(f" - Found shape-key action: {action.name} for {obj_id}")
actions["keys"][obj_id] = action
return actions
return actions
def get_main_body_action(source_actions):
# find the "Body" action
for obj_id in source_actions["keys"]:
action: bpy.types.Action = source_actions["keys"][obj_id]
l_name = obj_id.lower()
if l_name == "body":
utils.log_info(f" - Using main body action: {action.name}")
return action
# find the action with the most shape keys
action_with_most_keys = None
num_keys = 0
for obj_id in source_actions["keys"]:
action = source_actions["keys"][obj_id]
if len(action.fcurves) > num_keys:
num_keys = len(action.fcurves)
action_with_most_keys = action
if action_with_most_keys:
utils.log_info(f" - Using action with most shape keys: {action_with_most_keys.name}")
else:
utils.log_info(f" - No shape key actions in this Motion set!")
return action_with_most_keys
def apply_source_armature_action(dst_rig, source_actions, copy=False,
motion_id=None, motion_prefix=None,
set_id=None, set_generation=None):
obj_used = []
actions_used = []
rig_id = get_rig_id(dst_rig)
rl_arm_id = utils.get_rl_object_id(dst_rig)
if not motion_id:
motion_id = source_actions["motion_info"]["motion_id"]
if motion_prefix is None:
motion_prefix = source_actions["motion_info"]["prefix"]
utils.log_info(f"Applying source armature action:")
action = source_actions["armature"]
if action:
if copy and motion_id:
action_name = make_armature_action_name(rig_id, motion_id, motion_prefix)
utils.log_info(f" - Copying action: {action.name} to {action_name}")
action = utils.copy_action(action, action_name)
if set_id and set_generation:
add_motion_set_data(action, set_id, set_generation, rl_arm_id=rl_arm_id)
utils.log_info(f" - Applying action: {action.name} to {rig_id}")
utils.safe_set_action(dst_rig, action)
obj_used.append(dst_rig)
actions_used.append(action)
return obj_used, actions_used
def apply_source_key_actions(dst_rig, source_actions, all_matching=False, copy=False,
motion_id=None, motion_prefix=None,
set_id=None, set_generation=None,
filter=None):
obj_used = []
key_actions = {}
rig_id = get_rig_id(dst_rig)
if motion_id == "" or motion_id == "TEMP":
motion_id = "TEMP_" + utils.generate_random_id(12)
if motion_id is None:
motion_id = source_actions["motion_info"]["motion_id"]
if motion_prefix is None:
motion_prefix = source_actions["motion_info"]["prefix"]
# TODO this should really collect all named shape key animation tracks across all source objects
# and create actions specific to each target object.
# e.g. facial hair meshes have tongue shape keys which the body action alone doesn't have.
# apply to exact matches first
utils.log_info(f"Applying source key actions: (copy={copy}, motion_id={motion_id})")
objects = utils.get_child_objects(dst_rig)
for obj in objects:
if filter and obj not in filter: continue
if obj.type == "MESH":
if utils.object_has_shape_keys(obj):
obj_id = get_action_obj_id(obj)
if (obj_id in source_actions["keys"] and
obj_has_action_shape_keys(obj, source_actions["keys"][obj_id])):
action = source_actions["keys"][obj_id]
if copy and motion_id:
action_name = make_key_action_name(rig_id, motion_id, obj_id, motion_prefix)
utils.log_info(f" - Copying action: {action.name} to {action_name}")
action = utils.copy_action(action, action_name)
if set_id and set_generation:
add_motion_set_data(action, set_id, set_generation, obj_id=obj_id)
utils.log_info(f" - Applying action: {action.name} to {obj_id}")
utils.safe_set_action(obj.data.shape_keys, action)
obj_used.append(obj)
key_actions[obj_id] = action
else:
utils.safe_set_action(obj.data.shape_keys, None)
# apply to other compatible shape key objects
if all_matching:
utils.log_info(f"Applying other matching source key actions:")
body_action = get_main_body_action(source_actions)
for obj in objects:
if filter and obj not in filter: continue
if obj not in obj_used and utils.object_has_shape_keys(obj):
obj_id = get_action_obj_id(obj)
if body_action:
if obj_has_action_shape_keys(obj, body_action):
action = body_action
if copy and motion_id:
action_name = make_key_action_name(rig_id, motion_id, obj_id, motion_prefix)
utils.log_info(f" - Copying action: {action.name} to {action_name}")
action = utils.copy_action(action, action_name)
if set_id and set_generation:
add_motion_set_data(action, set_id, set_generation, obj_id=obj_id)
utils.log_info(f" - Applying action: {action.name} to {obj_id}")
utils.safe_set_action(obj.data.shape_keys, action)
obj_used.append(obj)
key_actions[obj_id] = action
return key_actions
def obj_has_action_shape_keys(obj, action: bpy.types.Action):
if obj.data.shape_keys and obj.data.shape_keys.key_blocks:
for key in obj.data.shape_keys.key_blocks:
for fcurve in action.fcurves:
if key.name in fcurve.data_path:
return True
return False
def get_rig_id(rig):
rig_id = utils.strip_name(rig.name.strip()).replace("|", "_")
return rig_id
def decode_action_name(action):
"""Decode action name into prefix, rig_id, type("A"|"K"), object_id, motion_id.
if the action name does not follow this naming pattern all values return None."""
if type(action) is str:
action_name = action
else:
action_name = action.name
#utils.log_detail(f"Decoding Action name: {action_name}")
try:
ids = action_name.split("|")
for i, id in enumerate(ids):
ids[i] = id.strip()
if ids[1] == "A":
type_id = "A"
prefix = ""
rig_id = ids[0]
obj_id = ""
elif ids[2] == "A":
type_id = "A"
prefix = ids[0]
rig_id = ids[1]
obj_id = ""
elif ids[1] == "K":
type_id = "K"
prefix = ""
rig_id = ids[0]
obj_id = ids[2]
elif ids[2] == "K":
type_id = "K"
prefix = ids[0]
rig_id = ids[1]
obj_id = ids[3]
motion_id = ids[-1]
if type_id != "A" and type_id != "K":
motion_id = None
raise Exception("Invalid action type id!")
#utils.log_detail(f"rig_id: {rig_id}, type_id: {type_id}, obj_id: {obj_id}, motion_id: {motion_id}, prefix: {prefix}")
except Exception as e:
prefix = None
rig_id = None
type_id = None
obj_id = None
motion_id = None
#utils.log_detail("Invalid motion action name!")
return prefix, rig_id, type_id, obj_id, motion_id
def get_action_motion_id(action, default_name="Motion"):
if action:
motion_id = action.name.split("|")[-1].strip()
else:
motion_id = ""
if not motion_id and default_name:
motion_id = f"{default_name}_{utils.generate_random_id(8)}"
return motion_id
def get_motion_prefix(action, default_prefix=""):
prefix, rig_id, type_id, obj_id, motion_id = decode_action_name(action)
prefix = prefix.strip()
if not prefix:
return default_prefix
else:
return prefix
def get_action_obj_id(obj):
obj_id = utils.strip_cc_base_name(obj.name).replace("|", "_")
return obj_id
def get_formatted_prefix(motion_prefix):
motion_prefix = motion_prefix.strip().replace("|", "_")
while motion_prefix.endswith("_"):
motion_prefix = motion_prefix[:-1]
if motion_prefix and not motion_prefix.endswith("|"):
motion_prefix += "|"
return motion_prefix
def get_unique_set_motion_id(rig_id, motion_id, motion_prefix, exclude_set_id=None):
test_name = make_armature_action_name(rig_id, motion_id, motion_prefix)
base_name = test_name
num_suffix = 0
while test_name in bpy.data.actions:
if exclude_set_id and "rl_set_id" in bpy.data.actions[test_name]:
if exclude_set_id == bpy.data.actions[test_name]["rl_set_id"]:
break
num_suffix += 1
test_name = f"{base_name}_{num_suffix:03d}"
if num_suffix > 0:
motion_id += f"_{num_suffix:03d}"
return motion_id
def make_armature_action_name(rig_id, motion_id, motion_prefix):
f_prefix = get_formatted_prefix(motion_prefix)
return f"{f_prefix}{rig_id}|A|{motion_id}"
def make_key_action_name(rig_id, motion_id, obj_id, motion_prefix):
f_prefix = get_formatted_prefix(motion_prefix)
return f"{f_prefix}{rig_id}|K|{obj_id}|{motion_id}"
def set_armature_action_name(action, rig_id, motion_id, motion_prefix):
action.name = make_armature_action_name(rig_id, motion_id, motion_prefix)
def set_key_action_name(action, rig_id, motion_id, obj_id, motion_prefix):
action.name = make_key_action_name(rig_id, motion_id, obj_id, motion_prefix)
def generate_motion_set(rig, motion_id, motion_prefix):
f_prefix = get_formatted_prefix(motion_prefix)
rl_set_id = utils.generate_random_id(32)
rl_set_generation, source_label = get_armature_action_source_type(rig)
if "rl_set_generation" not in rig:
rig["rl_set_generation"] = rl_set_generation
return rl_set_id, rl_set_generation
def get_motion_set(action):
set_id = None
set_generation = None
action_type_id = None
key_object = None
try:
if "rl_set_id" in action:
set_id = action["rl_set_id"]
if "rl_set_generation" in action:
set_generation = action["rl_set_generation"]
if "rl_key_object" in action:
key_object = action["rl_key_object"]
if "rl_action_type" in action:
action_type_id = action["rl_action_type"]
except:
set_id = None
set_generation = None
action_type_id = None
key_object = None
return set_id, set_generation, action_type_id, key_object
def add_motion_set_data(action, set_id, set_generation, obj_id=None, rl_arm_id=None):
action["rl_set_id"] = set_id
action["rl_set_generation"] = set_generation
if obj_id is not None:
action["rl_action_type"] = "KEY"
action["rl_key_object"] = obj_id
else:
action["rl_action_type"] = "ARM"
if rl_arm_id is not None:
action["rl_armature_id"] = rl_arm_id
def load_motion_set(rig, set_armature_action):
source_actions = find_source_actions(set_armature_action, None)
apply_source_armature_action(rig, source_actions, copy=False)
apply_source_key_actions(rig, source_actions, all_matching=True, copy=False)
def clear_motion_set(rig):
utils.safe_set_action(rig, None)
objects = utils.get_child_objects(rig)
for obj in objects:
if obj.type == "MESH":
if utils.object_has_shape_keys(obj):
utils.safe_set_action(obj.data.shape_keys, None)
def clear_all_actions(objects):
for obj in objects:
if utils.object_exists_is_armature(obj):
utils.safe_set_action(obj, None)
elif utils.object_exists_is_mesh(obj):
if utils.object_has_shape_keys(obj):
utils.safe_set_action(obj.data.shape_keys, None)
def push_motion_set(rig: bpy.types.Object, set_armature_action, push_index = 0):
source_actions = find_source_actions(set_armature_action, None)
frame = bpy.context.scene.frame_current
set_arm_action: bpy.types.Action = source_actions["armature"]
length = int(set_arm_action.frame_range[1]) - int(set_arm_action.frame_range[0])
objects = utils.get_child_objects(rig)
# find all available NLA tracks
nla_data = []
if rig.animation_data.nla_tracks:
nla_data.append(rig.animation_data.nla_tracks)
for obj in objects:
if obj.data.shape_keys and obj.data.shape_keys.animation_data:
if obj.data.shape_keys.animation_data.nla_tracks:
nla_data.append(obj.data.shape_keys.animation_data.nla_tracks)
# count the mininum number of shared tracks across all action objects
min_tracks = 0
for nla_tracks in nla_data:
l = len(nla_tracks)
if l > 0:
if min_tracks == 0:
min_tracks = l
min_tracks = min(min_tracks, l)
# find the first available track that can fit the motion set
available_tracks = [True] * min_tracks
for nla_tracks in nla_data:
for i in range(0, min_tracks):
track: bpy.types.NlaTrack = nla_tracks[i]
strip: bpy.types.NlaStrip
for strip in track.strips:
if frame >= strip.frame_start and frame < strip.frame_end:
available_tracks[i] = False
elif (frame + length) >= strip.frame_start and (frame + length) < strip.frame_end:
available_tracks[i] = False
elif strip.frame_start < frame and strip.frame_end >= frame + length:
available_tracks[i] = False
track_index = -1
for i, available in enumerate(available_tracks):
if available:
track_index = i
break
# push the actions
action = source_actions["armature"]
rig: bpy.types.Object
if rig.animation_data is None:
rig.animation_data_create()
if not rig.animation_data.nla_tracks or track_index == -1:
track = rig.animation_data.nla_tracks.new()
else:
track = rig.animation_data.nla_tracks[track_index]
try:
strip = track.strips.new(action.name, frame, action)
except:
track = rig.animation_data.nla_tracks.new()
strip = track.strips.new(action.name, frame, action)
strip.name = f"{action.name}|{push_index:03d}"
for obj in objects:
obj_id = get_action_obj_id(obj)
if obj.type == "MESH" and obj_id in source_actions["keys"]:
action = source_actions["keys"][obj_id]
if obj.data.shape_keys:
if not obj.data.shape_keys.animation_data:
obj.data.shape_keys.animation_data_create()
if not obj.data.shape_keys.animation_data.nla_tracks or track_index == -1:
track = obj.data.shape_keys.animation_data.nla_tracks.new()
else:
track = obj.data.shape_keys.animation_data.nla_tracks[track_index]
try:
strip = track.strips.new(action.name, frame, action)
except:
track = obj.data.shape_keys.animation_data.nla_tracks.new()
strip = track.strips.new(action.name, frame, action)
strip.name = f"{action.name}|{push_index:03d}"
def get_nla_tracks(data):
try:
if data and data.animation_data and data.animation_data.nla_tracks:
return data.animation_data.nla_tracks
except:
return None
def get_all_nla_strips(data, obj, strips=None):
if strips is None:
strips = {}
tracks = get_nla_tracks(data)
if tracks:
for track in tracks:
for strip in track.strips:
strips[strip] = (obj, track)
return strips
def get_strips_by_sets(set_ids: set):
all_strips = {}
for obj in bpy.data.objects:
if utils.object_exists(obj):
if obj.type == "ARMATURE":
get_all_nla_strips(obj, obj, all_strips)
elif obj.type == "MESH":
get_all_nla_strips(obj.data.shape_keys, obj, all_strips)
strip: bpy.types.NlaStrip
strips = {}
for strip in all_strips:
strip_set_id = utils.custom_prop(strip.action, "rl_set_id")
for sel_set_id, sel_auto_index in set_ids:
if strip_set_id == sel_set_id:
strip_auto_index = utils.get_auto_index_suffix(strip.name)
if strip_auto_index == sel_auto_index:
obj, track = all_strips[strip]
strips[strip] = (obj, track)
return strips
def unselect_all_but_strip(active_strip):
all_strips = {}
for obj in bpy.data.objects:
if utils.object_exists(obj):
if obj.type == "ARMATURE":
get_all_nla_strips(obj, obj, all_strips)
elif obj.type == "MESH":
get_all_nla_strips(obj.data.shape_keys, obj, all_strips)
for strip in all_strips:
if strip != active_strip and strip.select:
strip.select = False
def select_strips_by_set(active_strip: bpy.types.NlaStrip):
strips = bpy.context.selected_nla_strips.copy()
set_ids = set()
for strip in strips:
set_id = utils.custom_prop(strip.action, "rl_set_id")
strip_auto_index = utils.get_auto_index_suffix(strip.name)
if set_id and strip_auto_index:
set_ids.add((set_id, strip_auto_index))
if not active_strip and bpy.context.selected_nla_strips:
active_strip = bpy.context.selected_nla_strips[0]
if active_strip:
unselect_all_but_strip(active_strip)
strips = get_strips_by_sets(set_ids)
for strip in strips:
strip.select = True
def align_strips(strips, to_strip: bpy.types.NlaStrip=None, left=True):
strip: bpy.types.NlaStrip
left_frame = None if not to_strip else to_strip.frame_start
right_frame = None if not to_strip else to_strip.frame_end
# if no active strip, get the left most and right most frame in all strips
if not to_strip:
for strip in strips:
if left_frame is None:
left_frame = strip.frame_start
if right_frame is None:
right_frame = strip.frame_end
left_frame = min(strip.frame_start, left_frame)
right_frame = max(strip.frame_end, right_frame)
# align strips
# TODO sort strips in reverse order of direction
for strip in strips:
length = strip.frame_end - strip.frame_start
if left:
strip.frame_start = left_frame
strip.frame_end = strip.frame_start + length
strip.frame_start = strip.frame_end - length
else:
strip.frame_end = right_frame
strip.frame_start = strip.frame_end - length
strip.frame_end = strip.frame_start + length
def size_strips(strips, to_strip: bpy.types.NlaStrip=None, longest=True, reset=False):
to_length = 0
if to_strip:
to_length = to_strip.frame_end - to_strip.frame_start
min_length = None
max_length = None
strip: bpy.types.NlaStrip
# find the shortest and longest strip lengths
for strip in strips:
length = strip.frame_end - strip.frame_start
if min_length is None:
min_length = length
if max_length is None:
max_length = length
min_length = min(length, min_length)
max_length = max(length, max_length)
if not to_strip:
to_length = max_length if longest else min_length
for strip in strips:
if reset:
action_length = int(strip.action.frame_range[1] - strip.action.frame_range[0])
strip.frame_end = strip.frame_start + action_length
strip.frame_start = strip.frame_end - action_length
elif to_length > 0:
strip.frame_end = strip.frame_start + to_length
strip.frame_start = strip.frame_end - to_length
def set_action_set_fake_user(action, use_fake_user):
set_id = utils.custom_prop(action, "rl_set_id")
if set_id:
for action in bpy.data.actions:
action_set_id = utils.custom_prop(action, "rl_set_id")
if action_set_id == set_id:
action.use_fake_user = use_fake_user
utils.update_ui(all=True)
def delete_motion_set(action):
set_id = utils.custom_prop(action, "rl_set_id")
if set_id:
to_remove = []
for action in bpy.data.actions:
action_set_id = utils.custom_prop(action, "rl_set_id")
if action_set_id == set_id:
to_remove.append(action)
for action in to_remove:
bpy.data.actions.remove(action)
utils.update_ui(all=True)
def rename_armature(arm, name):
armature_object = None
armature_data = None
try:
armature_object = bpy.data.objects[name]
except:
pass
try:
armature_data = bpy.data.armatures[name]
except:
pass
utils.force_object_name(arm, name)
utils.force_armature_name(arm.data, name)
return armature_object, armature_data
def restore_armature_names(armature_object, armature_data, name):
if armature_object:
utils.force_object_name(armature_object, name)
if armature_data:
utils.force_armature_name(armature_data, name)
def get_rigify_ik_fk_influence(rig):
ik_fk = 0
num_bones = 0
ik_fk_control_bones = ["upper_arm_parent.L", "upper_arm_parent.R", "thigh_parent.L", "thigh_parent.R"]
for bone_name in ik_fk_control_bones:
if bone_name in rig.pose.bones:
num_bones += 1
pose_bone = rig.pose.bones[bone_name]
ik_fk += pose_bone["IK_FK"]
if num_bones > 0:
ik_fk /= num_bones
return ik_fk
def set_rigify_ik_fk_influence(rig, influence):
ik_fk_control_bones = ["upper_arm_parent.L", "upper_arm_parent.R", "thigh_parent.L", "thigh_parent.R"]
for bone_name in ik_fk_control_bones:
if bone_name in rig.pose.bones:
pose_bone = rig.pose.bones[bone_name]
pose_bone["IK_FK"] = influence
def poke_rig(rig):
"""Switches modes on the armature and sets the root pose bone location to force updates
after changing custom paramters i.e. IK_FK on the limbs."""
state = utils.store_mode_selection_state()
select_rig(rig)
pose_bone: bpy.types.PoseBone = rig.pose.bones[0]
loc = pose_bone.location
pose_bone.location = loc
pose_rig(rig)
utils.restore_mode_selection_state(state)
def set_bone_tail_length(bone: bpy.types.EditBone, tail):
"""Set the length based on a new tail position, but don't set the tail directly
as it may cause changes in the bone roll and angles."""
if bone and tail:
if type(tail) is bpy.types.EditBone:
new_tail = tail.head.copy()
elif type(tail) is Vector:
new_tail = tail.copy()
length = (new_tail - bone.head).length
bone.length = length
def set_bone_deform(bone: bpy.types.EditBone, use_deform):
try:
if bone:
bone.use_deform = use_deform
except: ...
def fix_cc3_standard_rig(cc3_rig):
if edit_rig(cc3_rig):
left_eye = bones.get_edit_bone(cc3_rig, "CC_Base_L_Eye")
right_eye = bones.get_edit_bone(cc3_rig, "CC_Base_R_Eye")
left_hand = bones.get_edit_bone(cc3_rig, ["CC_Base_L_Hand", "hand_l"])
right_hand = bones.get_edit_bone(cc3_rig, ["CC_Base_R_Hand", "hand_r"])
left_foot = bones.get_edit_bone(cc3_rig, ["CC_Base_L_Foot", "foot_l"])
right_foot = bones.get_edit_bone(cc3_rig, ["CC_Base_R_Foot", "foot_r"])
head = bones.get_edit_bone(cc3_rig, ["CC_Base_Head", "head"])
left_upper_arm = bones.get_edit_bone(cc3_rig, ["CC_Base_L_Upperarm", "upperarm_l"])
right_upper_arm = bones.get_edit_bone(cc3_rig, ["CC_Base_R_Upperarm", "upperarm_r"])
left_lower_arm = bones.get_edit_bone(cc3_rig, ["CC_Base_L_Forearm", "lowerarm_l"])
right_lower_arm = bones.get_edit_bone(cc3_rig, ["CC_Base_R_Forearm", "lowerarm_r"])
left_thigh = bones.get_edit_bone(cc3_rig, ["CC_Base_L_Thigh", "thigh_l"])
right_thigh = bones.get_edit_bone(cc3_rig, ["CC_Base_R_Thigh", "thigh_r"])
left_calf = bones.get_edit_bone(cc3_rig, ["CC_Base_L_Calf", "calf_l"])
right_calf = bones.get_edit_bone(cc3_rig, ["CC_Base_R_Calf", "calf_r"])
hip = bones.get_edit_bone(cc3_rig, ["CC_Base_Hip", "hip"])
root = bones.get_edit_bone(cc3_rig, ["CC_Base_BoneRoot", "RL_BoneRoot", "root"])
# fix deform state
set_bone_deform(left_thigh, False)
set_bone_deform(right_thigh, False)
set_bone_deform(left_calf, False)
set_bone_deform(right_calf, False)
set_bone_deform(left_upper_arm, False)
set_bone_deform(right_upper_arm, False)
set_bone_deform(left_lower_arm, False)
set_bone_deform(right_lower_arm, False)
set_bone_deform(hip, False)
set_bone_deform(root, False)
# eyes
eye_z = None
if left_eye and right_eye:
eye_z = ((left_eye.head + right_eye.head) * 0.5).z
# head
if head:
head_tail = head.tail.copy()
head_tail.z = eye_z + (eye_z - head.head.z) * 0.5
set_bone_tail_length(head, head_tail)
# arms
set_bone_tail_length(left_upper_arm, left_lower_arm)
set_bone_tail_length(right_upper_arm, right_lower_arm)
set_bone_tail_length(left_lower_arm, left_hand)
set_bone_tail_length(right_lower_arm, right_hand)
# legs
set_bone_tail_length(left_thigh, left_calf)
set_bone_tail_length(right_thigh, right_calf)
set_bone_tail_length(left_calf, left_foot)
set_bone_tail_length(right_calf, right_foot)
select_rig(cc3_rig)
def reset_rotation_modes(rig, rotation_mode = "QUATERNION"):
pose_bone: bpy.types.PoseBone