-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathbones.py
1509 lines (1300 loc) · 55.5 KB
/
bones.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
import mathutils
from math import pi, atan
from . import drivers, utils, vars
from rna_prop_ui import rna_idprop_ui_create
def cmp_rl_bone_names(name, bone_name):
"""Reduce supplied bone names to their base form without prefixes and compare."""
if bone_name.startswith("RL_"):
bone_name = bone_name[3:]
elif bone_name.startswith("CC_Base_"):
bone_name = bone_name[8:]
if name.startswith("RL_"):
name = name[3:]
elif name.startswith("CC_Base_"):
name = name[8:]
return name == bone_name
def get_rl_edit_bone(rig, name) -> bpy.types.EditBone:
if name:
if name in rig.data.edit_bones:
return rig.data.edit_bones[name]
# remove "CC_Base_" from start of bone name and try again...
if name.startswith("CC_Base_"):
name = name[8:]
if name in rig.data.edit_bones:
return rig.data.edit_bones[name]
if name.startswith("RL_"):
name = name[3:]
if name in rig.data.edit_bones:
return rig.data.edit_bones[name]
return None
def get_rl_bone(rig, name):
if name:
if name in rig.data.bones:
return rig.data.bones[name]
# remove "CC_Base_" from start of bone name and try again...
if name.startswith("CC_Base_"):
name = name[8:]
if name in rig.data.bones:
return rig.data.bones[name]
if name.startswith("RL_"):
name = name[3:]
if name in rig.data.bones:
return rig.data.bones[name]
return None
def get_rl_pose_bone(rig, name) -> bpy.types.PoseBone:
if name:
if name in rig.pose.bones:
return rig.pose.bones[name]
# remove "CC_Base_" from start of bone name and try again...
if name.startswith("CC_Base_"):
name = name[8:]
if name in rig.pose.bones:
return rig.pose.bones[name]
if name.startswith("RL_"):
name = name[3:]
if name in rig.pose.bones:
return rig.pose.bones[name]
return None
def get_edit_bone(rig, name) -> bpy.types.EditBone:
if name:
if type(name) is list:
for n in name:
if n in rig.data.edit_bones:
return rig.data.edit_bones[n]
else:
if name in rig.data.edit_bones:
return rig.data.edit_bones[name]
return None
def get_bone(rig, name) -> bpy.types.Bone:
if name:
if type(name) is list:
for n in name:
if n in rig.data.bones:
return rig.data.bones[n]
else:
if name in rig.data.bones:
return rig.data.bones[name]
return None
def get_pose_bone(rig, name) -> bpy.types.PoseBone:
if name:
if type(name) is list:
for n in name:
if n in rig.pose.bones:
return rig.pose.bones[n]
else:
if name in rig.pose.bones:
return rig.pose.bones[name]
return None
def find_target_pose_bone(rig, rl_bone_name, bone_mapping = None) -> bpy.types.PoseBone:
target_bone_name = find_target_bone_name(rig, rl_bone_name, bone_mapping)
if target_bone_name in rig.pose.bones:
return rig.pose.bones[target_bone_name]
return None
def is_target_bone_name(bone_name, target_name):
if not bone_name or not target_name:
return False
if target_name == bone_name:
return True
if cmp_rl_bone_names(target_name, bone_name):
return True
target_name = rl_export_bone_name(target_name)
if target_name == bone_name:
return True
if cmp_rl_bone_names(target_name, bone_name):
return True
def rl_export_bone_name(bone_name):
bone_name = bone_name.replace(' ', '_')
bone_name = bone_name.replace('(', '_')
bone_name = bone_name.replace(')', '_')
bone_name = bone_name.replace('&', '_')
return bone_name
def find_target_bone_name(rig, rl_bone_name, bone_mapping=None):
if not rig or not rl_bone_name:
return None
target_bone_name = None
if bone_mapping:
target_bone_name = get_rigify_meta_bone(rig, bone_mapping, rl_bone_name)
else:
target_bone_name = rl_bone_name
if target_bone_name in rig.pose.bones:
return target_bone_name
for pose_bone in rig.pose.bones:
if cmp_rl_bone_names(target_bone_name, pose_bone.name):
return pose_bone.name
target_bone_name = rl_export_bone_name(target_bone_name)
for pose_bone in rig.pose.bones:
if cmp_rl_bone_names(target_bone_name, pose_bone.name):
return pose_bone.name
return None
def find_pivot_bone(rig, bone_name):
if bone_name in rig.data.bones:
bone: bpy.types.Bone = rig.data.bones[bone_name]
for child in bone.children:
if child.name.startswith("CC_Base_Pivot"):
return child
return None
def get_rigify_meta_bone(rigify_rig, bone_mapping, cc3_bone_name):
if cc3_bone_name == "RL_BoneRoot" or cc3_bone_name == "CC_Base_BoneRoot":
return "root"
for bone_map in bone_mapping:
if bone_map[1] == cc3_bone_name:
# try to find the parent in the ORG bones
org_bone_name = f"ORG-{bone_map[0]}"
if org_bone_name in rigify_rig.data.bones:
return org_bone_name
# then try the DEF bones
def_bone_name = f"DEF-{bone_map[0]}"
if def_bone_name in rigify_rig.data.bones:
return def_bone_name
return None
def get_rigify_meta_bones(rigify_rig, bone_mapping, cc3_bone_name):
meta_bone_names = []
if cc3_bone_name == "RL_BoneRoot" or cc3_bone_name == "CC_Base_BoneRoot":
return ["root"]
for bone_map in bone_mapping:
if bone_map[1] == cc3_bone_name:
# try to find the parent in the ORG bones
org_bone_name = f"ORG-{bone_map[0]}"
if org_bone_name in rigify_rig.data.bones:
meta_bone_names.append(org_bone_name)
# then try the DEF bones
def_bone_name = f"DEF-{bone_map[0]}"
if def_bone_name in rigify_rig.data.bones:
meta_bone_names.append(def_bone_name)
return meta_bone_names
def get_align_vector(axis):
if axis == "X":
return mathutils.Vector((1,0,0))
if axis == "Y":
return mathutils.Vector((0,1,0))
if axis == "Z":
return mathutils.Vector((0,0,1))
if axis == "-X":
return mathutils.Vector((-1,0,0))
if axis == "-Y":
return mathutils.Vector((0,-1,0))
if axis == "-Z":
return mathutils.Vector((0,0,-1))
return None
def align_edit_bone_roll(edit_bone : bpy.types.EditBone, axis):
align_vector = get_align_vector(axis)
if align_vector:
edit_bone.align_roll(align_vector)
def rename_bone(rig, from_name, to_name):
if utils.edit_mode_to(rig):
bone = get_edit_bone(rig, from_name)
if bone and to_name not in rig.data.edit_bones:
bone.name = to_name
else:
utils.log_error(f"Bone {from_name} cannot be renamed as {to_name} already exists in rig!")
def copy_edit_bone(rig, src_name, dst_name, parent_name, scale):
if utils.edit_mode_to(rig):
src_bone = get_edit_bone(rig, src_name)
if src_bone and dst_name not in rig.data.edit_bones:
dst_bone = rig.data.edit_bones.new(dst_name)
dst_bone.head = src_bone.head
dst_bone.tail = src_bone.head + (src_bone.tail - src_bone.head) * scale
dst_bone.roll = src_bone.roll
if parent_name != "":
if parent_name in rig.data.edit_bones:
dst_bone.parent = rig.data.edit_bones[parent_name]
else:
utils.log_error(f"Unable to find parent bone {parent_name} in rig!")
return dst_bone
else:
if src_name not in rig.data.edit_bones:
utils.log_error(f"Unable to find source bone {src_name} in rig!")
if dst_name in rig.data.edit_bones:
utils.log_error(f"Destination bone {dst_name} already exists in rig!")
else:
utils.log_error(f"Unable to edit rig!")
return None
def new_edit_bone(rig, bone_name, parent_name, allow_existing = True):
if utils.edit_mode_to(rig):
can_add = allow_existing or bone_name not in rig.data.edit_bones
if can_add:
bone = rig.data.edit_bones.new(bone_name)
bone.head = mathutils.Vector((0,0,0))
bone.tail = bone.head + mathutils.Vector((0,0,0.05))
bone.roll = 0
if parent_name != "":
if parent_name in rig.data.edit_bones:
bone.parent = rig.data.edit_bones[parent_name]
else:
utils.log_error(f"Unable to find parent bone {parent_name} in rig!")
return bone
else:
utils.log_error(f"Destination bone {bone_name} already exists in rig!")
else:
utils.log_error(f"Unable to edit rig!")
return None
def reparent_edit_bone(rig, bone_name, parent_name):
if utils.edit_mode_to(rig):
if bone_name in rig.data.bones:
bone = rig.data.edit_bones[bone_name]
if bone:
if parent_name != "":
parent_bone = get_edit_bone(rig, parent_name)
if parent_bone:
bone.parent = parent_bone
return bone
else:
utils.log_error(f"Could not find parent bone: {parent_name} in Rig!")
else:
utils.log_error(f"Could not find target bone: {bone_name} in Rig!")
else:
utils.log_error(f"Unable to edit rig!")
return None
def copy_rl_edit_bone(cc3_rig, dst_rig, cc3_name, dst_name, dst_parent_name, scale):
if utils.edit_mode_to(cc3_rig):
src_bone = get_rl_edit_bone(cc3_rig, cc3_name)
if src_bone:
# cc3 rig is usually scaled by 0.01, so calculate the world positions.
head_pos = cc3_rig.matrix_world @ src_bone.head
tail_pos = cc3_rig.matrix_world @ src_bone.tail
roll = src_bone.roll
if utils.edit_mode_to(dst_rig):
# meta and rigify rigs are at 1.0 scale so all bones are in world space (at the origin)
dst_bone = dst_rig.data.edit_bones.new(dst_name)
dst_bone.head = head_pos
dst_bone.tail = head_pos + (tail_pos - head_pos) * scale
dst_bone.roll = roll
if dst_parent_name != "":
parent_bone = get_edit_bone(dst_rig, dst_parent_name)
if parent_bone:
dst_bone.parent = parent_bone
else:
utils.log_error(f"Could not find parent bone: {dst_parent_name} in target Rig!")
return dst_bone
else:
utils.log_error(f"Unable to edit target rig!")
else:
utils.log_error(f"Could not find bone: {cc3_name} in CC3 Rig!")
else:
utils.log_error(f"Unable to edit CC3 rig!")
return None
def copy_pose(rig):
pose = {}
bone: bpy.types.PoseBone
for bone in rig.pose.bones:
pose[bone.name] = (bone.rotation_mode,
bone.rotation_quaternion.copy(),
bone.rotation_euler.copy(),
bone.rotation_axis_angle,
bone.location.copy(),
bone.scale.copy())
return pose
def paste_pose(rig: bpy.types.Object, pose):
bone: bpy.types.PoseBone
for bone in rig.pose.bones:
bone.rotation_mode, bone.rotation_quaternion, bone.rotation_euler, bone.rotation_axis_angle, bone.location, bone.scale = pose[bone.name]
def copy_rig_bind_pose(rig_from, rig_to):
rig_def = {}
utils.set_only_active_object(rig_from)
if utils.edit_mode_to(rig_from):
for edit_bone in rig_from.data.edit_bones:
rig_def[edit_bone.name] = {
"head": edit_bone.head.copy(),
"tail": edit_bone.tail.copy(),
"roll": edit_bone.roll,
}
utils.set_only_active_object(rig_to)
if utils.edit_mode_to(rig_to):
for edit_bone in rig_to.data.edit_bones:
if edit_bone.name in rig_def:
bone_def = rig_def[edit_bone.name]
edit_bone.head = bone_def["head"].copy()
edit_bone.tail = bone_def["tail"].copy()
edit_bone.roll = bone_def["roll"]
def get_bone_children(bone, bone_list = None, include_root = False):
is_root = False
if bone_list is None:
is_root = True
bone_list = []
if include_root or not is_root:
bone_list.append(bone)
for child in bone.children:
get_bone_children(child, bone_list, include_root)
return bone_list
def get_edit_bone_subtree_defs(rig, bone : bpy.types.EditBone, tree = None):
if tree is None:
tree = []
# bone must have a parent for it to be a sub-tree
if utils.edit_mode_to(rig) and bone.parent:
bone_data = [bone.name,
rig.matrix_world @ bone.head,
rig.matrix_world @ bone.tail,
bone.head_radius,
bone.tail_radius,
bone.roll,
bone.parent.name]
tree.append(bone_data)
for child_bone in bone.children:
get_edit_bone_subtree_defs(rig, child_bone, tree)
return tree
def copy_rl_edit_bone_subtree(cc3_rig, dst_rig, cc3_name, dst_name, dst_parent_name, dst_prefix, collection, layer, vertex_group_map):
src_bone_defs = None
# copy the cc3 bone sub-tree to the destination rig
if utils.edit_mode_to(cc3_rig):
cc3_bone = get_edit_bone(cc3_rig, cc3_name)
src_bone_defs = get_edit_bone_subtree_defs(cc3_rig, cc3_bone)
if utils.edit_mode_to(dst_rig):
for bone_def in src_bone_defs:
src_name = bone_def[0]
if src_name == cc3_name:
name = dst_name
parent_name = dst_parent_name
else:
name = f"{dst_prefix}{bone_def[0]}"
src_parent_name = bone_def[6]
parent_name = vertex_group_map[src_parent_name]
head = bone_def[1]
tail = bone_def[2]
head_radius = bone_def[3]
tail_radius = bone_def[4]
roll = bone_def[5]
bone : bpy.types.EditBone = dst_rig.data.edit_bones.new(name)
bone.head = head
bone.tail = tail
bone.head_radius = head_radius
bone.tail_radius = tail_radius
bone.roll = roll
# store the name of the newly created bone (in case Blender has changed it)
vertex_group_map[src_name] = bone.name
bone_def.append(bone.name)
# set the edit bone layers
set_bone_collection(dst_rig, bone, collection, None, layer)
# set the bone parent
parent_bone = get_edit_bone(dst_rig, parent_name)
if parent_bone:
bone.parent = parent_bone
# set pose bone layers
if utils.object_mode():
for bone_def in src_bone_defs:
name = bone_def[7]
pose_bone = dst_rig.data.bones[name]
set_bone_collection(dst_rig, pose_bone, collection, None, layer)
return src_bone_defs
def add_copy_transforms_constraint(from_rig, to_rig, from_bone, to_bone, influence = 1.0, space="WORLD"):
try:
if utils.object_mode():
to_pose_bone : bpy.types.PoseBone = to_rig.pose.bones[to_bone]
c : bpy.types.CopyTransformsConstraint = to_pose_bone.constraints.new(type="COPY_TRANSFORMS")
c.target = from_rig
c.subtarget = from_bone
c.head_tail = 0
c.mix_mode = "REPLACE"
c.target_space = space
c.owner_space = space
c.influence = influence
return c
except Exception as e:
utils.log_error(f"Unable to add copy transforms constraint: {to_bone} {from_bone}", e)
return None
def add_copy_rotation_constraint(from_rig, to_rig, from_bone, to_bone, influence = 1.0, space="WORLD"):
try:
if utils.object_mode():
to_pose_bone : bpy.types.PoseBone = to_rig.pose.bones[to_bone]
c : bpy.types.CopyRotationConstraint = to_pose_bone.constraints.new(type="COPY_ROTATION")
c.target = from_rig
c.subtarget = from_bone
c.use_x = True
c.use_y = True
c.use_z = True
c.invert_x = False
c.invert_y = False
c.invert_z = False
c.mix_mode = "REPLACE"
c.target_space = space
if space == "LOCAL_OWNER_ORIENT":
space = "LOCAL"
c.owner_space = space
c.influence = influence
return c
except Exception as e:
utils.log_error(f"Unable to add copy rotation constraint: {to_bone} {from_bone}", e)
return None
def add_copy_scale_constraint(from_rig, to_rig, from_bone, to_bone, influence = 1.0, space="WORLD"):
try:
if utils.object_mode():
to_pose_bone : bpy.types.PoseBone = to_rig.pose.bones[to_bone]
c : bpy.types.CopyScaleConstraint = to_pose_bone.constraints.new(type="COPY_SCALE")
c.target = from_rig
c.subtarget = from_bone
c.use_x = True
c.use_y = True
c.use_z = True
c.target_space = space
if space == "LOCAL_OWNER_ORIENT":
space = "LOCAL"
c.owner_space = space
c.influence = influence
return c
except Exception as e:
utils.log_error(f"Unable to add copy scale constraint: {to_bone} {from_bone}", e)
return None
def add_copy_location_constraint(from_rig, to_rig, from_bone, to_bone, influence = 1.0, space="WORLD", axes=None):
try:
if utils.object_mode():
to_pose_bone : bpy.types.PoseBone = to_rig.pose.bones[to_bone]
c : bpy.types.CopyLocationConstraint = to_pose_bone.constraints.new(type="COPY_LOCATION")
c.target = from_rig
c.subtarget = from_bone
c.use_x = True
c.use_y = True
c.use_z = True
c.invert_x = False
c.invert_y = False
c.invert_z = False
c.target_space = space
if space == "LOCAL_OWNER_ORIENT":
space = "LOCAL"
c.owner_space = space
c.influence = influence
if axes:
c.use_x = "X" in axes
c.use_y = "Y" in axes
c.use_z = "Z" in axes
c.invert_x = "-X" in axes
c.invert_y = "-Y" in axes
c.invert_z = "-Z" in axes
return c
except Exception as e:
utils.log_error(f"Unable to add copy location constraint: {to_bone} {from_bone}", e)
return None
def add_stretch_to_constraint(from_rig, to_rig, from_bone, to_bone, influence = 1.0, head_tail = 0.0, space="WORLD"):
try:
if utils.object_mode():
to_pose_bone : bpy.types.PoseBone = to_rig.pose.bones[to_bone]
c : bpy.types.StretchToConstraint = to_pose_bone.constraints.new(type="STRETCH_TO")
c.target = from_rig
c.subtarget = from_bone
c.head_tail = head_tail
c.target_space = space
if space == "LOCAL_OWNER_ORIENT":
space = "LOCAL"
c.owner_space = space
c.influence = influence
return c
except Exception as e:
utils.log_error(f"Unable to add copy stretch to constraint: {to_bone} {from_bone}", e)
return None
def add_damped_track_constraint(rig, bone_name, target_name, influence):
try:
if utils.object_mode():
pose_bone : bpy.types.PoseBone = rig.pose.bones[bone_name]
c : bpy.types.DampedTrackConstraint = pose_bone.constraints.new(type="DAMPED_TRACK")
c.target = rig
c.subtarget = target_name
c.head_tail = 0
c.track_axis = "TRACK_Y"
c.influence = influence
return c
except Exception as e:
utils.log_error(f"Unable to add damped track constraint: {bone_name} {target_name}", e)
return None
def add_limit_distance_constraint(from_rig, to_rig, from_bone, to_bone, distance, influence = 1.0, space="WORLD"):
try:
if utils.object_mode():
to_pose_bone : bpy.types.PoseBone = to_rig.pose.bones[to_bone]
c : bpy.types.LimitDistanceConstraint = to_pose_bone.constraints.new(type="LIMIT_DISTANCE")
c.target = from_rig
c.subtarget = from_bone
c.distance = distance
c.limit_mode = "LIMITDIST_ONSURFACE"
c.target_space = space
c.owner_space = space
c.influence = influence
return c
except Exception as e:
utils.log_error(f"Unable to add limit distance constraint: {to_bone} {from_bone}", e)
return None
def add_child_of_constraint(parent_rig, child_rig, parent_bone, child_bone, influence = 1.0, space="WORLD"):
try:
if utils.object_mode():
to_pose_bone : bpy.types.PoseBone = child_rig.pose.bones[child_bone]
c : bpy.types.ChildOfConstraint = to_pose_bone.constraints.new(type="CHILD_OF")
c.target = parent_rig
c.subtarget = parent_bone
c.target_space = space
c.owner_space = space
c.influence = influence
return c
except Exception as e:
utils.log_error(f"Unable to add child of constraint: {child_bone} {parent_bone}", e)
return None
def add_inverse_kinematic_constraint(from_rig, to_rig, from_bone, to_bone, influence = 1.0, space="WORLD",
use_tail = True, use_stretch = True, use_rotation = True, use_location = True,
weight = 1.0, orient_weight = 0.0, chain_count = 1):
try:
if utils.object_mode():
to_pose_bone : bpy.types.PoseBone = to_rig.pose.bones[to_bone]
c : bpy.types.KinematicConstraint = to_pose_bone.constraints.new(type="IK")
c.target = from_rig
c.subtarget = from_bone
c.use_tail = use_tail
c.use_stretch = use_stretch
c.use_rotation = use_rotation
c.use_location = use_location
c.weight = weight
c.chain_count = chain_count
c.orient_weight = orient_weight
c.target_space = space
c.owner_space = space
c.influence = influence
return c
except Exception as e:
utils.log_error(f"Unable to add inverse kinematic constraint: {to_bone} {from_bone}", e)
return None
def set_pose_bone_lock(pose_bone : bpy.types.PoseBone,
lock_ik = [0, 0, 0],
lock_location = [0, 0, 0],
lock_rotation = [0, 0, 0, 0],
lock_scale = [0, 0, 0],):
for i, lock in enumerate(lock_location):
pose_bone.lock_location[i] = lock > 0
for i, lock in enumerate(lock_rotation):
if i == 3:
pose_bone.lock_rotation_w = lock > 0
else:
pose_bone.lock_rotation[i] = lock > 0
pose_bone.lock_ik_x = lock_ik[0] > 0
pose_bone.lock_ik_y = lock_ik[1] > 0
pose_bone.lock_ik_z = lock_ik[2] > 0
for i, lock in enumerate(lock_scale):
pose_bone.lock_scale[i] = lock > 0
def set_edit_bone_flags(edit_bone, flags, deform):
edit_bone.use_connect = True if "C" in flags else False
edit_bone.use_local_location = True if "L" in flags else False
edit_bone.use_inherit_rotation = True if "R" in flags else False
edit_bone.use_deform = deform
def store_armature_settings(rig, include_pose=False):
if not rig: return None
collections = {}
layers = []
if utils.B400():
for collection in rig.data.collections:
collections[collection.name] = collection.is_visible
else:
for i in range(0, 32):
layers.append(rig.data.layers[i])
visibility = { "layers": layers,
"collections": collections,
"show_in_front": rig.show_in_front,
"display_type": rig.display_type,
"pose_position": rig.data.pose_position,
"action": utils.safe_get_action(rig),
"location": rig.location }
if include_pose:
pose_data = {}
pose_bone: bpy.types.PoseBone
for pose_bone in rig.pose.bones:
pose_data[pose_bone.name] = [pose_bone.location, pose_bone.rotation_axis_angle, pose_bone.rotation_euler,
pose_bone.rotation_quaternion, pose_bone.scale, pose_bone.rotation_mode]
visibility["pose"] = pose_data
return visibility
def restore_armature_settings(rig, visibility, include_pose=False):
if not rig: return
if utils.B400():
collections = visibility["collections"]
for collection in collections:
rig.data.collections[collection].is_visible = collections[collection]
else:
layers = visibility["layers"]
for i in range(0, 32):
rig.data.layers[i] = layers[i]
rig.show_in_front = visibility["show_in_front"]
rig.display_type = visibility["display_type"]
rig.data.pose_position = visibility["pose_position"]
utils.safe_set_action(rig, visibility["action"])
rig.location = visibility["location"]
if include_pose:
pose_data = visibility["pose"]
for bone_name in pose_data:
rig.pose.bones[bone_name].rotation_mode = pose_data[bone_name][5]
rig.pose.bones[bone_name].location = pose_data[bone_name][0]
rig.pose.bones[bone_name].rotation_axis_angle = pose_data[bone_name][1]
rig.pose.bones[bone_name].rotation_euler = pose_data[bone_name][2]
rig.pose.bones[bone_name].rotation_quaternion = pose_data[bone_name][3]
rig.pose.bones[bone_name].scale = pose_data[bone_name][4]
def set_rig_bind_pose(rig):
rig.data.pose_position = "POSE"
utils.safe_set_action(rig, None)
clear_pose(rig)
def copy_position(rig, bone, copy_bones, offset):
if utils.edit_mode_to(rig):
if bone in rig.data.edit_bones:
edit_bone = rig.data.edit_bones[bone]
head_position = mathutils.Vector((0,0,0))
tail_position = mathutils.Vector((0,0,0))
num = 0
for copy_name in copy_bones:
if copy_name in rig.data.edit_bones:
copy_bone = rig.data.edit_bones[copy_name]
dir = (copy_bone.tail - copy_bone.head).normalized()
head_position += copy_bone.head + dir * offset
tail_position += copy_bone.tail + dir * offset
num += 1
head_position /= num
tail_position /= num
edit_bone.head = head_position
edit_bone.tail = tail_position
return edit_bone
else:
utils.log_error(f"Cannot find bone {bone} in rig!")
return None
def is_bone_in_collections(rig, bone: bpy.types.Bone, collections=None, groups=None, layers=None):
if utils.B400():
if collections:
for collection in collections:
if collection in rig.data.collections:
if bone.name in rig.data.collections[collection].bones:
return True
else:
if groups:
if bone.name in rig.pose.bones:
pose_bone: bpy.types.PoseBone = rig.pose.bones[bone.name]
if pose_bone.bone_group and pose_bone.bone_group.name in groups:
return True
if layers:
for layer in layers:
if not bone.layers[layer]:
return False
return True
return False
def set_bone_collection(rig, bone, collection=None, group=None, layer=None, color=None):
"""Sets the bone collection (Any) (Blender 4.0+),
or group (PoseBone only) or layer (Bone or EditBone) (< Blender 4.0)"""
if utils.B400():
if collection:
if not collection in rig.data.collections:
rig.data.collections.new(collection)
bone_collection = rig.data.collections[collection]
bone_collection.assign(bone)
if color is not None:
set_bone_color(bone, color)
else:
if group:
if group not in rig.pose.bone_groups:
rig.pose.bone_groups.new(name=group)
group = rig.pose.bone_groups[group]
if type(bone) is not bpy.types.PoseBone and bone.name in rig.pose.bones:
pose_bone = rig.pose.bones[bone.name]
pose_bone.bone_group = group
elif type(bone) is bpy.types.PoseBone:
bone.bone_group = group
if layer:
if type(bone) is bpy.types.PoseBone:
bone = bone.bone
bone.layers[layer] = True
for i, l in enumerate(bone.layers):
bone.layers[i] = i == layer
CUSTOM_COLORS = {
"Active": (0.7686275243759155, 1.0, 1.0),
"Select": (0.5960784554481506, 0.8980392813682556, 1.0),
"IK": (0.8000000715255737, 0.0, 0.0),
"FK": (0.3764706254005432, 0.7803922295570374, 0.20784315466880798),
"SPECIAL": (0.9803922176361084, 0.9019608497619629, 0.2392157018184662),
"SIM": (0.98, 0.24, 0.9),
"TWEAK": (0.2196078598499298, 0.49803924560546875, 0.7843137979507446),
"TWEAK_DISABLED": (0.270588, 0.396078, 0.521569),
"ROOT": (0.6901960968971252, 0.46666669845581055, 0.6784313917160034),
"DETAIL": (0.9843137860298157, 0.5372549295425415, 0.33725491166114807),
"DEFAULT": (0.3764706254005432, 0.7803922295570374, 0.20784315466880798),
"SKIN": (0.647059, 0.780392, 0.588235),
"PIVOT": (0.9803922176361084, 0.9019608497619629, 0.2392157018184662),
"MESH": (0.9803922176361084, 0.9019608497619629, 0.2392157018184662),
}
def set_bone_color(bone, color_code):
if utils.B400():
bone.color.palette = "CUSTOM"
bone.color.custom.normal = CUSTOM_COLORS[color_code]
bone.color.custom.active = CUSTOM_COLORS["Active"]
bone.color.custom.select = CUSTOM_COLORS["Select"]
def set_bone_collection_visibility(rig, collection, layer, visible, only=False):
if utils.B400():
if only:
for coll in rig.data.collections:
coll.is_visible = False
if collection in rig.data.collections:
rig.data.collections[collection].is_visible = visible
else:
rig.data.layers[layer] = visible
if only:
for i in range(0, 32):
if i != layer:
rig.data.layers[i] = not visible
def make_bones_visible(arm, protected=False, collections=None, layers=None):
bone : bpy.types.Bone
pose_bone : bpy.types.PoseBone
for bone in arm.data.bones:
pose_bone = get_pose_bone(arm, bone.name)
# make all active bone layers visible so they can be unhidden and selectable
if utils.B400():
for collection in arm.data.collections:
if collections:
collection.is_visible = collection.name in collections
else:
collection.is_visible = True
#if protected:
# collection.is_editable = True
else:
for i, l in enumerate(bone.layers):
if l:
if layers:
arm.data.layers[i] = i in layers
else:
arm.data.layers[i] = True
if protected:
arm.data.layers_protected[i] = False
# show and select bone
bone.hide = False
bone.hide_select = False
def is_bone_collection_visible(arm, collection=None, layer=None):
if utils.B400():
if collection in arm.data.collections:
return arm.data.collections[collection].is_visible
return False
else:
return arm.data.layers[layer]
def add_bone_collection(rig, collection):
if collection not in rig.data.collections:
rig.data.collections.new(collection)
return rig.data.collections[collection]
def assign_rl_base_collections(rig):
if utils.B400():
if "Deform" not in rig.data.collections:
deform = add_bone_collection(rig, "Deform")
none = add_bone_collection(rig, "Non-Deform")
twist = add_bone_collection(rig, "Twist")
share = add_bone_collection(rig, "Share")
none_deform_bones = [
"CC_Base_R_Upperarm",
"CC_Base_L_Upperarm",
"CC_Base_R_Forearm",
"CC_Base_L_Forearm",
"CC_Base_R_Thigh",
"CC_Base_L_Thigh",
"CC_Base_R_Calf",
"CC_Base_L_Calf",
"CC_Base_FacialBone",
"CC_Base_BoneRoot",
"RL_BoneRoot",
]
for bone in rig.data.bones:
if "Twist" in bone.name:
twist.assign(bone)
elif "ShareBone" in bone.name:
share.assign(bone)
if bone.name in none_deform_bones:
none.assign(bone)
else:
deform.assign(bone)
def get_distance_between(rig, bone_a_name, bone_b_name):
if utils.edit_mode_to(rig):
if bone_a_name in rig.data.edit_bones and bone_b_name in rig.data.edit_bones:
bone_a = rig.data.edit_bones[bone_a_name]
bone_b = rig.data.edit_bones[bone_b_name]
delta : mathutils.Vector = bone_b.head - bone_a.head
return delta.length
else:
utils.log_error(f"Could not find all bones: {bone_a_name} and {bone_b_name} in Rig!")
else:
utils.log_error(f"Unable to edit rig!")
return 0
def generate_eye_widget(rig, bone_name, bones, distance, scale):
wgt : bpy.types.Object = None
if utils.object_mode():
if len(bones) == 1:
bpy.ops.mesh.primitive_circle_add(vertices=16, radius=1, rotation=[0,0,11.25])
bpy.ops.object.transform_apply(rotation=True)
wgt = utils.get_active_object()
else:
bpy.ops.mesh.primitive_circle_add(vertices=16, radius=1.35, rotation=[0,0,11.25])
bpy.ops.object.transform_apply(rotation=True)
wgt = utils.get_active_object()
mesh : bpy.types.Mesh = wgt.data
vert: bpy.types.MeshVertex
for vert in mesh.vertices:
if vert.co.x < -0.01:
vert.co.x -= 0.5 * distance / scale
elif vert.co.x > 0.01:
vert.co.x += 0.5 * distance / scale
if wgt:
collection : bpy.types.Collection
for collection in bpy.data.collections:
if collection.name.startswith("WGTS_rig"):
collection.objects.link(wgt)
elif wgt.name in collection.objects:
collection.objects.unlink(wgt)
if bone_name in rig.pose.bones:
pose_bone : bpy.types.PoseBone
pose_bone = rig.pose.bones[bone_name]
pose_bone.custom_shape = wgt
wgt.name = "WGT-rig_" + bone_name
return wgt
def make_widget_collection(collection_name) -> bpy.types.Collection:
wgt_collection: bpy.types.Collection = None
for collection in bpy.data.collections:
if collection.name.startswith(collection_name):
wgt_collection = collection
if not wgt_collection:
wgt_collection = bpy.data.collections.new(collection_name)
bpy.context.scene.collection.children.link(wgt_collection)
wgt_collection.hide_render = True
layer_collections = utils.get_view_layer_collections(search=collection_name)
for collection in layer_collections:
collection.exclude = True
collection.hide_viewport = True