forked from soupday/cc_blender_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbones.py
734 lines (622 loc) · 26.1 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
# 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 utils, vars
from rna_prop_ui import rna_idprop_ui_create
def cmp_rl_bone_names(name, bone_name):
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):
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):
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):
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):
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):
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 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_warn(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):
if utils.edit_mode_to(rig):
if bone_name not in rig.data.edit_bones:
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_warn(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 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, layer):
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:
name = bone_def[0]
if name == cc3_name:
name = dst_name
head = bone_def[1]
tail = bone_def[2]
head_radius = bone_def[3]
tail_radius = bone_def[4]
roll = bone_def[5]
parent_name = bone_def[6]
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)
bone_def.append(bone.name)
# set the edit bone layers
for l in range(0, 32):
bone.layers[l] = l == layer
# set the bone parent
parent_bone = get_edit_bone(dst_rig, parent_name)
if parent_bone:
bone.parent = parent_bone
# set the tree parent
dst_bone = get_edit_bone(dst_rig, dst_name)
dst_parent_bone = get_edit_bone(dst_rig, dst_parent_name)
if dst_bone and dst_parent_bone:
dst_bone.parent = dst_parent_bone
# finally set pose bone layers
if utils.set_mode("OBJECT"):
for bone_def in src_bone_defs:
name = bone_def[7]
pose_bone = dst_rig.data.bones[name]
for l in range(0, 32):
pose_bone.layers[l] = l == 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.set_mode("OBJECT"):
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:
utils.log_error(f"Unable to add copy transforms constraint: {to_bone} {from_bone}")
return None
def add_copy_rotation_constraint(from_rig, to_rig, from_bone, to_bone, influence = 1.0, space="WORLD"):
try:
if utils.set_mode("OBJECT"):
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:
utils.log_error(f"Unable to add copy transforms constraint: {to_bone} {from_bone}")
return None
def add_copy_location_constraint(from_rig, to_rig, from_bone, to_bone, influence = 1.0, space="WORLD"):
try:
if utils.set_mode("OBJECT"):
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
return c
except:
utils.log_error(f"Unable to add copy transforms constraint: {to_bone} {from_bone}")
return None
def add_damped_track_constraint(rig, bone_name, target_name, influence):
try:
if utils.set_mode("OBJECT"):
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:
utils.log_error(f"Unable to add damped track constraint: {bone_name} {target_name}")
return None
def add_limit_distance_constraint(from_rig, to_rig, from_bone, to_bone, distance, influence = 1.0, space="WORLD"):
try:
if utils.set_mode("OBJECT"):
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:
utils.log_error(f"Unable to add limit distance constraint: {to_bone} {from_bone}")
return None
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 set_bone_layer(rig, bone_name, layer):
if utils.edit_mode_to(rig):
set_edit_bone_layer(rig, bone_name, layer)
if utils.set_mode("OBJECT"):
set_pose_bone_layer(rig, bone_name, layer)
def set_edit_bone_layer(rig, bone_name, layer):
edit_bone = rig.data.edit_bones[bone_name]
for l in range(0, 32):
edit_bone.layers[l] = l == layer
def set_pose_bone_layer(rig, bone_name, layer):
pose_bone = rig.data.bones[bone_name]
for l in range(0, 32):
pose_bone.layers[l] = l == layer
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 set_bone_group(rig, bone, group):
if utils.set_mode("OBJECT"):
if bone in rig.pose.bones:
bone_group = rig.pose.bone_groups[group]
rig.pose.bones[bone].bone_group = bone_group
return True
else:
utils.log_error(f"Cannot find pose bone {bone} in rig!")
else:
utils.log_error("Unable to edit rig!")
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.set_mode("OBJECT"):
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 add_pose_bone_custom_property(rig, pose_bone_name, prop_name, prop_value):
if utils.set_mode("OBJECT"):
if pose_bone_name in rig.pose.bones:
pose_bone = rig.pose.bones[pose_bone_name]
rna_idprop_ui_create(pose_bone, prop_name, default=prop_value, overridable=True, min=0, max=1)
def add_constraint_scripted_influence_driver(rig, pose_bone_name, data_path, variable_name, constraint_type, expression = ""):
if utils.set_mode("OBJECT"):
if pose_bone_name in rig.pose.bones:
pose_bone = rig.pose.bones[pose_bone_name]
con : bpy.types.Constraint = None
for con in pose_bone.constraints:
if con.type == constraint_type:
if expression:
driver = make_driver(con, "influence", "SCRIPTED", expression)
else:
driver = make_driver(con, "influence", "SUM")
if driver:
var = make_driver_var(driver, "SINGLE_PROP", variable_name, rig, data_path = data_path)
def make_driver_var(driver, var_type, var_name, target, data_path = "", bone_target = "", transform_type = "", transform_space = ""):
"""
var_type = "SINGLE_PROP", "TRANSFORMS"
var_name = variable name
target = target object/bone
target_data_path = "..."
"""
var : bpy.types.DriverVariable = driver.variables.new()
var.name = var_name
if var_type == "SINGLE_PROP":
var.type = var_type
var.targets[0].id_type = "OBJECT"
var.targets[0].id = target.id_data
var.targets[0].data_path = data_path
elif var_type == "TRANSFORMS":
var.targets[0].id = target.id_data
var.targets[0].bone_target = bone_target
var.targets[0].rotation_mode = "AUTO"
var.targets[0].transform_type = transform_type
var.targets[0].transform_space = transform_space
return var
def make_driver(source, prop_name, driver_type, driver_expression = ""):
"""
prop_name = "value", "influence"
driver_type = "SUM", "SCRIPTED"
driver_expression = "..."
"""
driver = None
if source:
fcurve : bpy.types.FCurve
fcurve = source.driver_add(prop_name)
driver : bpy.types.Driver = fcurve.driver
if driver_type == "SUM":
driver.type = driver_type
elif driver_type == "SCRIPTED":
driver.type = driver_type
driver.expression = driver_expression
return driver
def get_data_path_pose_bone_property(pose_bone_name, variable_name):
data_path = f"pose.bones[\"{pose_bone_name}\"][\"{variable_name}\"]"
return data_path
def get_data_rigify_limb_property(limb_id, variable_name):
"""
limb_id = "LEFT_LEFT", "RIGHT_LEFT", "LEFT_ARM", "RIGHT_ARM", "TORSO", "JAW", "EYES"\n
variable_name = "IK_Stretch", "IK_FK", "neck_follow", "head_follow", "mouth_lock", "eyes_follow"
"""
if limb_id == "LEFT_LEG":
return get_data_path_pose_bone_property("thigh_parent.L", variable_name)
elif limb_id == "RIGHT_LEFT":
return get_data_path_pose_bone_property("thigh_parent.R", variable_name)
elif limb_id == "LEFT_ARM":
return get_data_path_pose_bone_property("upper_arm_parent.L", variable_name)
elif limb_id == "RIGHT_ARM":
return get_data_path_pose_bone_property("upper_arm_parent.R", variable_name)
elif limb_id == "TORSO":
return get_data_path_pose_bone_property("torso", variable_name)
elif limb_id == "JAW":
return get_data_path_pose_bone_property("jaw_master", variable_name)
elif limb_id == "EYES":
return get_data_path_pose_bone_property("eyes", variable_name)
return ""
def add_bone_prop_driver(rig, pose_bone_name, bone_data_path, bone_data_index, props, prop_name, variable_name):
if utils.set_mode("OBJECT"):
pose_bone : bpy.types.PoseBone
if pose_bone_name in rig.pose.bones:
pose_bone = rig.pose.bones[pose_bone_name]
fcurve : bpy.types.FCurve
fcurve = pose_bone.driver_add(bone_data_path, bone_data_index)
driver : bpy.types.Driver = fcurve.driver
driver.type = "SUM"
var : bpy.types.DriverVariable = driver.variables.new()
var.name = variable_name
var.type = "SINGLE_PROP"
var.targets[0].id_type = "SCENE"
var.targets[0].id = props.id_data
var.targets[0].data_path = props.path_from_id(prop_name)
def clear_constraints(rig, pose_bone_name):
if pose_bone_name:
if utils.set_mode("OBJECT"):
if pose_bone_name in rig.pose.bones:
pose_bone = rig.pose.bones[pose_bone_name]
constraints = []
for con in pose_bone.constraints:
constraints.append(con)
for con in constraints:
pose_bone.constraints.remove(con)
def clear_drivers(rig):
drivers = rig.animation_data.drivers
if drivers:
fcurves = []
for fc in drivers:
fcurves.append(fc)
for fc in fcurves:
drivers.remove(fc)
def get_bone_name_from_data_path(data_path):
if data_path.startswith("pose.bones[\""):
start = utils.safe_index_of(data_path, '"', 0) + 1
end = utils.safe_index_of(data_path, '"', start)
return data_path[start:end]
return None
def get_roll(bone):
mat = bone.matrix_local.to_3x3()
quat = mat.to_quaternion()
if abs(quat.w) < 1e-4:
roll = pi
else:
roll = 2*atan(quat.y/quat.w)
return roll
def clear_pose(arm):
# select all bones in pose mode
arm.data.pose_position = "POSE"
utils.object_mode_to(arm)
utils.set_mode("POSE")
bone : bpy.types.Bone
for bone in arm.data.bones:
bone.select = True
# unlock the bones
pose_bone : bpy.types.PoseBone
for pose_bone in arm.pose.bones:
pose_bone.lock_location = [False, False, False]
pose_bone.lock_rotation = [False, False, False]
pose_bone.lock_rotation_w = False
pose_bone.lock_scale = [False, False, False]
# clear pose
bpy.ops.pose.transforms_clear()
utils.object_mode_to(arm)
def reset_root_bone(arm):
if utils.edit_mode_to(arm):
root_bone = arm.data.edit_bones[0]
if "root" in root_bone.name.lower():
head = root_bone.head
length = root_bone.length
tail = head + mathutils.Vector((0,-1,0)) * length
root_bone.tail = tail
root_bone.align_roll(mathutils.Vector((0,0,1)))
utils.set_mode("OBJECT")
def bone_mapping_contains_bone(bone_mappings, bone_name):
for bone_mapping in bone_mappings:
if cmp_rl_bone_names(bone_mapping[1], bone_name):
return True
return False
def get_accessory_root_bone(bone_mappings, bone):
root = None
if not bone_mapping_contains_bone(bone_mappings, bone.name):
while bone.parent:
if not bone_mapping_contains_bone(bone_mappings, bone.parent.name):
root = bone.parent
bone = bone.parent
return root
def bone_parent_in_list(bone_list, bone):
if bone:
while bone.parent:
if bone.parent.name in bone_list:
return True
bone = bone.parent
return False
def find_accessory_bones(bone_mappings, rig):
accessory_bones = []
for bone in rig.data.bones:
bone_name = bone.name
if not bone_mapping_contains_bone(bone_mappings, bone_name):
if bone_name not in accessory_bones and not bone_parent_in_list(accessory_bones, bone):
utils.log_info(f"Accessory Bone: {bone_name}")
accessory_bones.append(bone_name)
return accessory_bones