-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathrigutils.py
More file actions
4685 lines (3927 loc) · 181 KB
/
rigutils.py
File metadata and controls
4685 lines (3927 loc) · 181 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
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, time, os
from . import springbones, bones, facerig, modifiers, rigify_mapping_data, lib, utils, vars
from typing import List
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, slot_type=None):
channel = utils.get_action_channelbag(action, slot_type=slot_type)
if channel:
for fcurve in channel.fcurves:
if name in fcurve.data_path:
return True
return False
def name_in_pose_bone_data_paths_regex(action, name, slot_type=None):
channel = utils.get_action_channelbag(action, slot_type=slot_type)
if channel:
name = ".*" + name
for fcurve in channel.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 rig_has_bones(armature: bpy.types.Object, bones: list):
if armature:
if len(armature.data.bones) > 0:
for bone_name in bones:
if type(bone_name) is list:
found = False
for bn in bone_name:
if bn in armature.data.bones:
found = True
if not found:
return False
elif bone_name not in armature.data.bones:
return False
return True
return False
def action_has_bones(action: bpy.types.Action, bones: list):
channel = utils.get_action_channelbag(action, slot_type="OBJECT")
if channel:
if len(channel.fcurves) > 0:
for bone_name in bones:
if type(bone_name) is list:
found = False
for bn in bone_name:
if name_in_data_paths(action, bn, slot_type="OBJECT"):
found = True
if not found:
return False
elif not name_in_data_paths(action, bone_name, slot_type="OBJECT"):
return False
return True
return False
def is_G3_action(action):
return action_has_bones(action, rigify_mapping_data.CC3_BONE_NAMES)
def is_G3_armature(armature):
return rig_has_bones(armature, rigify_mapping_data.CC3_BONE_NAMES)
def is_iClone_action(action):
return action_has_bones(action, rigify_mapping_data.ICLONE_BONE_NAMES)
def is_iClone_armature(armature):
return rig_has_bones(armature, rigify_mapping_data.ICLONE_BONE_NAMES)
def is_ActorCore_action(action):
return action_has_bones(action, rigify_mapping_data.ACTOR_CORE_BONE_NAMES)
def is_ActorCore_armature(armature):
return rig_has_bones(armature, rigify_mapping_data.ACTOR_CORE_BONE_NAMES)
def is_GameBase_action(action):
return action_has_bones(action, rigify_mapping_data.GAME_BASE_BONE_NAMES)
def is_GameBase_armature(armature):
return rig_has_bones(armature, rigify_mapping_data.GAME_BASE_BONE_NAMES)
def is_Mixamo_action(action):
return action_has_bones(action, rigify_mapping_data.MIXAMO_BONE_NAMES)
def is_Mixamo_armature(armature):
return rig_has_bones(armature, rigify_mapping_data.MIXAMO_BONE_NAMES)
def is_rigify_action(action):
return action_has_bones(action, rigify_mapping_data.RIGIFY_BONE_NAMES)
def is_rigify_armature(armature):
return rig_has_bones(armature, rigify_mapping_data.RIGIFY_BONE_NAMES)
def is_rl_rigify_action(action):
return action_has_bones(action, rigify_mapping_data.RIGIFY_PLUS_BONE_NAMES)
def is_rigify_plus_armature(armature):
return rig_has_bones(armature, rigify_mapping_data.RIGIFY_PLUS_BONE_NAMES)
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_rigify_plus_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_rigify_plus_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_ids(source_action)
src_prefix = get_motion_prefix(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_motion_id(source_action)
actions = {
"motion_info": {
"prefix": src_prefix,
"motion_id": src_motion_id,
"set_id": src_set_id,
"set_generation": src_set_gen,
"slotted": False,
},
"count": 0,
"armature": None,
"armatures": [],
"keys": {},
"objects": {},
}
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_ids(action)
unused = utils.get_prop(action, "rl_unused_action", False)
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" or type_id == "SLOTTED":
utils.log_info(f" - Found armature action: {action.name}")
actions["armatures"].append(action)
if not unused and not actions["armature"]:
actions["armature"] = action
actions["slotted"] = (type_id == "SLOTTED")
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_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
actions["objects"][obj_id] = obj
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]
channel = utils.get_action_channelbag(action, slot_type="KEY")
if channel:
if len(channel.fcurves) > num_keys:
num_keys = len(channel.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
"""
# TODO: No longer used.
def apply_source_actions(dst_rig, source_actions, copy=False,
motion_id=None, motion_prefix=None,
set_id=None, set_generation=None,
all_matching=False, filter=None):
apply_source_armature_action(dst_rig, source_actions, copy=copy,
motion_id=motion_id, motion_prefix=motion_prefix,
set_id=set_id, set_generation=set_generation)
apply_source_key_actions(dst_rig, source_actions, copy=copy,
all_matching=all_matching,
motion_id=motion_id, motion_prefix=motion_prefix,
set_id=set_id, set_generation=set_generation,
filter=filter)
# TODO: No longer used.
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"]
is_slotted = source_actions["slotted"]
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, slotted=is_slotted)
utils.log_info(f" - Applying action: {action.name} to {rig_id}")
load_slotted_action(dst_rig, action)
obj_used.append(dst_rig)
actions_used.append(action)
return obj_used, actions_used
# TODO: No longer 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 = {}
if source_actions["slotted"]:
return 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)
old_action = utils.safe_get_action(obj.data.shape_keys)
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, old_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)
old_action = utils.safe_get_action(obj.data.shape_keys)
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, old_action)
return key_actions
"""
def obj_has_action_shape_keys(obj, action: bpy.types.Action):
channel = utils.get_action_channelbag(action, slot_type="KEY")
if channel:
if obj.data.shape_keys and obj.data.shape_keys.key_blocks:
for key in obj.data.shape_keys.key_blocks:
for fcurve in channel.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 get_armature_id(rig):
return utils.get_prop(rig, "rl_armature_id", None)
def get_motion_id(action):
if type(action) is str:
action_name = action
else:
action_name = action.name
ids = action_name.split("|")
if ids:
return ids[-1]
return action_name
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}")
motion_id = action_name
prefix = ""
rig_id = ""
type_id = "A"
obj_id = ""
try:
ids = action_name.split("|")
for i, id in enumerate(ids):
ids[i] = id.strip()
L = len(ids)
K = L
if "A" in ids or "UA" in ids or "S" in ids:
K -= 1
elif "K" in ids or "UK" in ids:
K -= 2
if L > 0:
motion_id = ids[-1]
if K >= 3:
prefix = ids[0]
rig_id = ids[1]
elif K >= 2:
rig_id = ids[0]
if "rl_action_type" in action:
if action["rl_action_type"] == "ARM":
type_id = "A"
elif action["rl_action_type"] == "KEY":
type_id = "K"
if "rl_key_object" in action:
obj_id = action["rl_key_object"]
elif action["rl_action_type"] == "SLOTTED":
type_id = "S"
else:
if "A" in ids:
type_id = "A"
elif "K" in ids:
type_id = "K"
i = ids.index("K")
if L > i+1:
obj_id = ids[i+1]
elif "S" in ids:
type_id = "S"
except Exception as e:
prefix = None
rig_id = None
type_id = None
obj_id = None
motion_id = action_name
return prefix, rig_id, type_id, obj_id, motion_id
def get_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 = decode_action_name(action)[0]
if prefix is None:
return default_prefix
elif prefix:
return prefix.strip()
else:
return prefix
def get_obj_id(obj):
obj_id = utils.strip_cc_base_name(obj.name).replace("|", "_")
return obj_id
def get_formatted_prefix(motion_prefix):
if motion_prefix is None:
motion_prefix = ""
motion_prefix = motion_prefix.strip().replace("|", "_")
while motion_prefix.endswith("_"):
motion_prefix = motion_prefix[:-1]
return motion_prefix
def get_unique_set_motion_id(rig_id, motion_id, motion_prefix, exclude_set_id=None, slotted=False):
test_name = make_armature_action_name(rig_id, motion_id, motion_prefix, slotted=slotted)
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 generate_action_name(rig_id, code_id, obj_id, motion_id, motion_prefix):
f_prefix = get_formatted_prefix(motion_prefix)
name = ""
if f_prefix:
name += f"{f_prefix}"
if rig_id:
if name: name += "|"
name += f"{rig_id}"
if code_id:
if name: name += "|"
name += f"{code_id}"
if obj_id:
if name: name += "|"
name += f"{obj_id}"
if motion_id:
if name: name += "|"
name += f"{motion_id}"
return name
#return f"{f_prefix}{rig_id}|{code_id}|{motion_id}"
def make_armature_action_name(rig_id, motion_id, motion_prefix, slotted=False):
if slotted:
return generate_action_name(rig_id, "", "", motion_id, motion_prefix)
else:
return generate_action_name(rig_id, "A", "", motion_id, motion_prefix)
def make_key_action_name(rig_id, motion_id, obj_id, motion_prefix):
return generate_action_name(rig_id, "K", obj_id, motion_id, motion_prefix)
def set_armature_action_name(action, rig_id, motion_id, motion_prefix, slotted=False):
action.name = make_armature_action_name(rig_id, motion_id, motion_prefix, slotted=slotted)
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 get_set_generation(rig):
return utils.get_prop(rig, "rl_set_generation", None)
def update_rig_set_generation(rig):
rl_set_generation, source_label = get_armature_action_source_type(rig)
if ("rl_set_generation" not in rig or
rig["rl_set_generation"] != rl_set_generation):
rig["rl_set_generation"] = rl_set_generation
return rl_set_generation
def generate_set_id():
return utils.generate_random_id(32)
def generate_motion_set(rig, motion_id, motion_prefix):
rl_set_id = generate_set_id()
rl_set_generation = update_rig_set_generation(rig)
return rl_set_id, rl_set_generation
def get_motion_set_ids(action):
set_id = utils.get_prop(action, "rl_set_id", None)
set_generation = utils.get_prop(action, "rl_set_generation", None)
action_type_id = utils.get_prop(action, "rl_action_type", None)
key_object = utils.get_prop(action, "rl_key_object", None)
return set_id, set_generation, action_type_id, key_object
def get_motion_set_actions(action_or_set_id):
actions = []
if type(action_or_set_id) is bpy.types.Action:
set_id = utils.get_prop(action_or_set_id, "rl_set_id", None)
else:
set_id = action_or_set_id
if set_id:
for action in bpy.data.actions:
if utils.get_prop(action, "rl_set_id") == set_id:
actions.append(action)
return actions
def get_actions_frame_range(actions):
action: bpy.types.Action = None
frame_start = None
frame_end = None
for action in actions:
start = action.frame_range[0]
end = action.frame_range[1]
if frame_start is None:
frame_start = start
frame_end = end
if start < frame_start:
frame_start = start
if end > frame_end:
frame_end = end
return frame_start, frame_end
def get_motion_set_frame_range(action_or_set_id):
actions = get_motion_set_actions(action_or_set_id)
return get_actions_frame_range(actions)
def replace_motion_set(set_id, old_set_id):
actions = get_motion_set_actions(set_id)
old_actions = get_motion_set_actions(old_set_id)
temp_id = generate_set_id()
name = old_actions[0].name if len(old_actions) == 1 else None
for action in actions:
if name:
utils.force_action_name(action, name)
utils.set_prop(action, "rl_set_id", old_set_id)
for action in old_actions:
utils.set_prop(action, "rl_set_id", temp_id)
delete_motion_set(temp_id)
def add_motion_set_data(action, set_id, set_generation, obj_id=None, arm_id=None, slotted=False):
action["rl_set_id"] = set_id
action["rl_set_generation"] = set_generation
if slotted:
action["rl_action_type"] = "SLOTTED"
elif obj_id is not None:
action["rl_action_type"] = "KEY"
action["rl_key_object"] = obj_id
else:
action["rl_action_type"] = "ARM"
if arm_id is not None:
action["rl_armature_id"] = arm_id
def update_motion_set_index(action):
props = vars.props()
props.action_set_list_index = utils.index_in_collection(action, bpy.data.actions)
def load_motion_set(rig, action, move=False, temp=None):
prefs = vars.prefs()
if action:
utils.log_info(f"Load Motion Set: {action.name} {'Move ' if move else ''}{'Temp ' if temp else ''}")
else:
utils.log_info(f"Clearing Motion Set ...")
# clear the pose and shape-keys first before loading the motion set
clear_motion_set(rig)
arm_action = action
if action:
utils.log_indent()
if prefs.use_action_slots():
arm_action = load_slotted_action(rig, action,
move=move, temp=temp)
else:
arm_action = load_separate_actions(rig, action,
move=move, temp=temp)
if arm_action:
update_motion_set_index(arm_action)
utils.log_recess()
return arm_action
def new_motion_set(rig):
prefs = vars.prefs()
slotted = prefs.use_action_slots()
rig_id = get_rig_id(rig)
rl_arm_id = utils.get_rl_object_id(rig)
set_id = generate_set_id()
set_generation = utils.get_prop(rig, "rl_set_generation")
motion_id = "New"
motion_id = get_unique_set_motion_id(rig_id, motion_id, "", slotted=slotted)
action_name = make_armature_action_name(rig_id, motion_id, "", slotted=slotted)
action = bpy.data.actions.new(action_name)
slot, channel = add_action_ob_slot_channelbag(action, rig)
add_motion_set_data(action, set_id, set_generation, arm_id=rl_arm_id, slotted=slotted)
utils.safe_set_action(rig, action, slot=slot)
utils.safe_set_action(rig.data, None, create=False)
for child in rig.children:
if utils.object_exists_is_mesh(child) and utils.object_has_shape_keys(child):
utils.safe_set_action(child, None, create=False)
utils.safe_set_action(child.data, None, create=False)
utils.safe_set_action(child.data.shape_keys, None, create=False)
update_motion_set_index(action)
return action
def clear_motion_set(rig):
mode_selection = utils.store_mode_selection_state()
has_actions = utils.safe_get_action(rig)
if not has_actions:
reset_pose(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)
if not has_actions:
reset_shape_keys(obj)
utils.restore_mode_selection_state(mode_selection)
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):
prefs = vars.prefs()
source_actions = find_source_actions(set_armature_action, None)
frame = bpy.context.scene.frame_current
set_arm_action: bpy.types.Action = source_actions["armature"]
start = int(set_arm_action.frame_range[0])
end = int(set_arm_action.frame_range[1])
length = end - start
is_slotted = prefs.use_action_slots() and utils.get_prop(set_arm_action, "rl_action_type") == "SLOTTED"
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: bpy.types.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.action_frame_start = start
strip.action_frame_end = end
strip.name = f"{action.name}|{push_index:03d}"
for obj in objects:
if is_slotted:
if "KEAll Keys" in action.slots:
obj_id = "KEAll Keys"
else:
obj_id = "KE" + obj.name
if obj.type == "MESH" and obj_id in action.slots:
slot = action.slots[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.action_slot = slot
strip.action_frame_start = start
strip.action_frame_end = end
strip.name = f"{action.name}|{push_index:03d}"
else:
obj_id = get_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.action_frame_start = start
strip.action_frame_end = end
strip.name = f"{action.name}|{push_index:03d}"
def clear_animation_data(obj: bpy.types.Object):
if obj.type == "ARMATURE" or obj.type == "MESH":
# remove action
utils.safe_set_action(obj, None)
# remove strips
# this removes drivers too...
#obj.animation_data_clear()
ad = obj.animation_data
if ad:
while ad.nla_tracks:
ad.nla_tracks.remove(ad.nla_tracks[0])
if obj.type == "MESH":
# remove shape key action
utils.safe_set_action(obj.data.shape_keys, None)
# remove shape key strips
if obj.data.shape_keys and obj.data.shape_keys.animation_data:
obj.data.shape_keys.animation_data_clear()
ad = obj.data.shape_keys.animation_data
if ad:
while ad.nla_tracks:
ad.nla_tracks.remove(ad.nla_tracks[0])
def reset_nla_tracks(obj):
track = None
if obj.type == "ARMATURE" or obj.type == "MESH":
# remove action
utils.safe_set_action(obj, None)
# remove strips
ad = obj.animation_data
if ad:
while ad.nla_tracks:
ad.nla_tracks.remove(ad.nla_tracks[0])
if obj.type == "MESH":
# remove shape key action
utils.safe_set_action(obj.data.shape_keys, None)
# remove shape key strips
if obj.data.shape_keys and obj.data.shape_keys.animation_data:
ad = obj.data.shape_keys.animation_data
if ad:
while ad.nla_tracks:
ad.nla_tracks.remove(ad.nla_tracks[0])
return track
def create_key_proxy_object(obj_id, action: bpy.types.Action=None,
shape_keys=None, parent=None, channel=None):
# create object
bpy.ops.mesh.primitive_cube_add(size=0.1, enter_editmode=False,
align='WORLD',
location=(0, 0, 0),
scale=(1, 1, 1))
obj: bpy.types.Object = utils.get_active_object()
obj.shape_key_add(name="Basis")
name = f"Key_Proxy_{obj_id}"
obj.name = name
obj.data.name = name
obj["key_proxy"] = "WqebNXksi9wLQwco1hyFQMlIYcbqWGZF"
obj.data["key_proxy"] = "WqebNXksi9wLQwco1hyFQMlIYcbqWGZF"
if parent:
obj.parent = parent
obj.hide_set(True)
if channel:
for fcurve in channel.fcurves:
data_path = fcurve.data_path
if data_path.startswith("key_blocks["):
key_name = data_path[12:-8]
key = obj.shape_key_add(name=key_name)
key.slider_max = 1.5
key.slider_min = -1.5
elif action:
channel = utils.get_action_channelbag(action, slot_type="KEY")
for fcurve in channel.fcurves:
data_path = fcurve.data_path
if data_path.startswith("key_blocks["):
key_name = data_path[12:-8]
key = obj.shape_key_add(name=key_name)
key.slider_max = 1.5
key.slider_min = -1.5
elif shape_keys:
if "Basis" in shape_keys:
shape_keys.remove("Basis")
for key_name in shape_keys:
key = obj.shape_key_add(name=key_name)
key.slider_max = 1.5
key.slider_min = -1.5
return obj
def get_shape_key_action_objects(rigify_rig, source_rig, source_action=None, shape_keys=None):
objects = []
if source_rig and source_action:
source_actions = find_source_actions(source_action, source_rig)
if source_actions["slotted"]:
channelbags = utils.get_action_channelbags(source_action)
for channel in channelbags:
slot = channel.slot