-
Notifications
You must be signed in to change notification settings - Fork 32
/
operators.py
2990 lines (2388 loc) · 117 KB
/
operators.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
from math import pi
import os
import bpy
from bpy.props import BoolProperty
from bpy.props import EnumProperty
from bpy.props import FloatProperty
from bpy.props import IntProperty
from bpy.props import StringProperty
from bpy.props import CollectionProperty
from bpy.props import FloatVectorProperty
from bpy_extras.io_utils import ImportHelper
from itertools import chain
from .rig_mapping import bone_mapping
from . import preset_handler
from . import bone_utils
from . import fbx_helper
from .version_compatibility import make_annotations, matmul, get_preferences, layout_split
from mathutils import Vector
from mathutils import Matrix
CONSTR_STATUS = (
('enable', "Enable", "Enable All Constraints"),
('disable', "Disable", "Disable All Constraints"),
('remove', "Remove", "Remove All Constraints")
)
CONSTR_TYPES = bpy.types.PoseBoneConstraints.bl_rna.functions['new'].parameters['type'].enum_items.keys()
CONSTR_TYPES.append('ALL_TYPES')
@make_annotations
class ConstraintStatus(bpy.types.Operator):
"""Disable/Enable bone constraints."""
bl_idname = "object.expykit_set_constraints_status"
bl_label = "Enable/disable constraints"
bl_options = {'REGISTER', 'UNDO'}
set_status = EnumProperty(items=CONSTR_STATUS,
name="Status",
default='enable')
selected_only = BoolProperty(name="Only Selected",
default=False)
constr_type = EnumProperty(items=[(ct, ct.replace('_', ' ').title(), ct) for ct in CONSTR_TYPES],
name="Constraint Type",
default='ALL_TYPES')
@classmethod
def poll(cls, context):
if not context.object:
return False
if context.mode != 'POSE':
return False
if context.object.type != 'ARMATURE':
return False
return True
def execute(self, context):
bones = context.selected_pose_bones if self.selected_only else context.object.pose.bones
if self.set_status == 'remove':
for bone in bones:
for constr in reversed(bone.constraints):
if self.constr_type != 'ALL_TYPES' and constr.type != self.constr_type:
continue
bone.constraints.remove(constr)
else:
for bone in bones:
for constr in bone.constraints:
if self.constr_type != 'ALL_TYPES' and constr.type != self.constr_type:
continue
constr.mute = self.set_status == 'disable'
return {'FINISHED'}
@make_annotations
class SelectConstrainedControls(bpy.types.Operator):
bl_idname = "armature.expykit_select_constrained_ctrls"
bl_label = "Select constrained controls"
bl_description = "Select bone controls with constraints or animations"
bl_options = {'REGISTER', 'UNDO'}
select_type = EnumProperty(items=[
('constr', "Constrained", "Select constrained controls"),
('anim', "Animated", "Select animated controls"),
],
name="Select if",
default='constr')
skip_deform = BoolProperty(name="Skip Deform Bones", default=True)
has_shape = BoolProperty(name="Only Control shapes", default=True)
@classmethod
def poll(cls, context):
if not context.object:
return False
if context.mode != 'POSE':
return False
if context.object.type != 'ARMATURE':
return False
return True
def execute(self, context):
ob = context.object
if self.select_type == 'constr':
for pb in bone_utils.get_constrained_controls(ob, unselect=True, use_deform=not self.skip_deform):
pb.bone.select = bool(pb.custom_shape) if self.has_shape else True
elif self.select_type == 'anim':
if not ob.animation_data:
return {'FINISHED'}
if not ob.animation_data.action:
return {'FINISHED'}
for fc in ob.animation_data.action.fcurves:
bone_name = crv_bone_name(fc)
if not bone_name:
continue
try:
bone = ob.data.bones[bone_name]
except KeyError:
continue
bone.select = True
return {'FINISHED'}
@make_annotations
class RevertDotBoneNames(bpy.types.Operator):
"""Reverts dots in bones that have renamed by Unreal Engine"""
bl_idname = "object.expykit_dot_bone_names"
bl_label = "Revert dots in Names (from UE4 renaming)"
bl_options = {'REGISTER', 'UNDO'}
sideletters_only = BoolProperty(name="Only Side Letters",
description="i.e. '_L' to '.L'",
default=True)
selected_only = BoolProperty(name="Only Selected",
default=False)
@classmethod
def poll(cls, context):
if not context.object:
return False
if context.mode != 'POSE':
return False
return context.object.type == 'ARMATURE'
def execute(self, context):
bones = context.selected_pose_bones if self.selected_only else context.object.pose.bones
if self.sideletters_only:
for bone in bones:
for side in ("L", "R"):
if bone.name[:-1].endswith("_{0}_00".format(side)):
bone.name = bone.name.replace("_{0}_00".format(side), ".{0}.00".format(side))
elif bone.name.endswith("_{0}".format(side)):
bone.name = bone.name[:-2] + ".{0}".format(side)
else:
for bone in bones:
bone.name = bone.name.replace('_', '.')
return {'FINISHED'}
@make_annotations
class ConvertBoneNaming(bpy.types.Operator):
"""Convert Bone Names between Naming Convention"""
bl_idname = "object.expykit_convert_bone_names"
bl_label = "Convert Bone Names"
bl_options = {'REGISTER', 'UNDO'}
src_preset = EnumProperty(items=preset_handler.iterate_presets_with_current,
name="Source Preset",
)
trg_preset = EnumProperty(items=preset_handler.iterate_presets,
name="Target Preset",
)
strip_prefix = BoolProperty(
name="Strip Prefix",
description="Remove prefix when found",
default=True
)
anim_tracks = BoolProperty(
name="Convert Animations",
description="Convert Animation Tracks",
default=True
)
replace_existing = BoolProperty(
name="Take Over Existing Names",
description='Bones already named after Target Preset will get ".001" suffix',
default=True
)
prefix_separator = StringProperty(
name="Prefix Separator",
description="Separator between prefix and name, i.e: MyCharacter:head",
default=":"
)
@classmethod
def poll(cls, context):
if not context.object:
return False
if context.mode != 'POSE':
return False
if context.object.type != 'ARMATURE':
return False
return True
@staticmethod
def convert_presets(src_settings, target_settings):
src_skeleton = preset_handler.get_preset_skel(src_settings)
trg_skeleton = preset_handler.get_preset_skel(target_settings)
return src_skeleton, trg_skeleton
@staticmethod
def convert_settings(current_settings, target_settings, validate=True):
src_settings = preset_handler.PresetSkeleton()
src_settings.copy(current_settings)
src_skeleton = preset_handler.get_settings_skel(src_settings)
trg_skeleton = preset_handler.set_preset_skel(target_settings, validate)
return src_skeleton, trg_skeleton
@staticmethod
def rename_bones(context, src_skeleton, trg_skeleton, separator="", replace_existing=False, skip_ik=False):
# FIXME: separator should not be necessary anymore, as it is handled at preset validation
bone_names_map = src_skeleton.conversion_map(trg_skeleton, skip_ik=skip_ik)
if separator:
for bone in context.object.data.bones:
if separator not in bone.name:
continue
bone.name = bone.name.rsplit(separator, 1)[1]
additional_bones = {}
for src_name, trg_name in bone_names_map.items():
if not trg_name:
continue
if not src_name:
continue
try:
src_bone = context.object.data.bones.get(src_name, None)
except SystemError:
continue
if not src_bone:
continue
if replace_existing:
pre_existing_bone = context.object.data.bones.get(trg_name, None)
if pre_existing_bone:
pre_existing_name = pre_existing_bone.name
pre_existing_bone.name = "{}.001".format(trg_name)
additional_bones[pre_existing_name] = pre_existing_bone.name
src_bone.name = trg_name
bone_names_map.update(additional_bones)
return bone_names_map
def execute(self, context):
if self.src_preset == "--Current--":
current_settings = context.object.data.expykit_retarget
trg_settings = preset_handler.PresetSkeleton()
trg_settings.copy(current_settings)
src_skeleton, trg_skeleton = self.convert_settings(trg_settings, self.trg_preset, validate=False)
set_preset = False
else:
src_skeleton, trg_skeleton = self.convert_presets(self.src_preset, self.trg_preset)
set_preset = True
if all((src_skeleton, trg_skeleton, src_skeleton != trg_skeleton)):
if self.anim_tracks:
actions = [action for action in bpy.data.actions if validate_action(action, context.object.path_resolve)]
else:
actions = []
bone_names_map = self.rename_bones(context, src_skeleton, trg_skeleton,
self.prefix_separator if self.strip_prefix else "",
self.replace_existing)
if context.object.animation_data and context.object.data.animation_data:
for driver in chain(context.object.animation_data.drivers, context.object.data.animation_data.drivers):
try:
driver_bone = driver.data_path.split('"')[1]
except IndexError:
continue
try:
trg_name = bone_names_map[driver_bone]
except KeyError:
continue
driver.data_path = driver.data_path.replace('bones["{0}"'.format(driver_bone),
'bones["{0}"'.format(trg_name))
for action in actions:
for fc in action.fcurves:
try:
track_bone = fc.data_path.split('"')[1]
except IndexError:
continue
if self.strip_prefix and self.prefix_separator in track_bone:
stripped_bone = track_bone.rsplit(self.prefix_separator, 1)[1]
else:
stripped_bone = track_bone
try:
trg_name = bone_names_map[stripped_bone]
except KeyError:
continue
fc.data_path = fc.data_path.replace('bones["{0}"'.format(track_bone),
'bones["{0}"'.format(trg_name))
if set_preset:
preset_handler.set_preset_skel(self.trg_preset)
else:
preset_handler.validate_preset(bpy.context.active_object.data, separator=self.prefix_separator)
if bpy.app.version[0] > 2:
# blender 3.0 objects do not immediately update renamed vertex groups
for ob in bone_utils.iterate_rigged_obs(context.object):
ob.data.update()
return {'FINISHED'}
@make_annotations
class CreateTransformOffset(bpy.types.Operator):
"""Scale the Character and setup an Empty to preserve final transform"""
bl_idname = "object.expykit_create_offset"
bl_label = "Create Scale Offset"
bl_options = {'REGISTER', 'UNDO'}
container_name = StringProperty(name="Name", description="Name of the transform container", default="EMP-Offset")
container_scale = FloatProperty(name="Scale", description="Scale of the transform container", default=0.01)
fix_animations = BoolProperty(name="Fix Animations", description="Apply Offset to character animations", default=True)
fix_constraints = BoolProperty(name="Fix Constraints", description="Apply Offset to character constraints", default=True)
do_parent = BoolProperty(name="Execute and Exit", description="Parent to the new offset and exit",
default=False, options={'SKIP_SAVE'})
_allowed_modes = ['OBJECT', 'POSE']
@classmethod
def poll(cls, context):
if not context.object:
return False
if context.object.parent:
return False
if context.object.type != 'ARMATURE':
return False
if context.mode not in cls._allowed_modes:
return False
return True
def draw(self, context):
layout = self.layout
column = layout.column()
row = layout_split(column, factor=0.2, align=True)
row.label(text="Name")
row.prop(self, 'container_name', text="")
row = layout_split(column, factor=0.2, align=True)
row.label(text="Scale")
row.prop(self, "container_scale", text="")
row = layout_split(column, factor=0.2, align=True)
row.label(text="")
row.prop(self, "fix_animations")
row = layout_split(column, factor=0.2, align=True)
row.label(text="")
row.prop(self, "fix_constraints")
row = layout_split(column, factor=0.2, align=True)
row.label(text="")
row.prop(self, "do_parent", toggle=True)
def execute(self, context):
arm_ob = context.object
emp_ob = bpy.data.objects.new(self.container_name, None)
context.collection.objects.link(emp_ob)
transform = Matrix().to_3x3() * self.container_scale
emp_ob.matrix_world = transform.to_4x4()
if self.do_parent:
arm_ob.parent = emp_ob
inverted = emp_ob.matrix_world.inverted()
arm_ob.data.transform(inverted)
arm_ob.update_tag()
# bring in metarig if found
try:
metarig = next(ob for ob in bpy.data.objects if ob.type == 'ARMATURE' and ob.data.rigify_target_rig == arm_ob)
except (StopIteration, AttributeError): # Attribute Error if Rigify is not loaded
pass
else:
if self.do_parent:
metarig.parent = emp_ob
metarig.data.transform(inverted)
metarig.update_tag()
if self.fix_constraints:
# fix constraints rest lenghts
for pbone in arm_ob.pose.bones:
for constr in pbone.constraints:
if constr.type == 'STRETCH_TO':
constr.rest_length /= self.container_scale
elif constr.type == 'LIMIT_DISTANCE':
constr.distance /= self.container_scale
elif constr.type == 'ACTION':
if constr.target == arm_ob and constr.transform_channel.startswith('LOCATION'):
if constr.target_space != 'WORLD':
constr.min /= self.container_scale
constr.max /= self.container_scale
elif constr.type == 'LIMIT_LOCATION' and constr.owner_space != 'WORLD':
constr.min_x /= self.container_scale
constr.min_y /= self.container_scale
constr.min_z /= self.container_scale
constr.max_x /= self.container_scale
constr.max_y /= self.container_scale
constr.max_z /= self.container_scale
# scale rigged meshes as well
rigged = (ob for ob in bpy.data.objects if
next((mod for mod in ob.modifiers if mod.type == 'ARMATURE' and mod.object == context.object),
None))
for ob in rigged:
if ob.data.shape_keys:
# cannot transform objects with shape keys
ob.scale /= self.container_scale
else:
ob.data.transform(inverted)
# fix scale dependent attrs in modifiers
for mod in ob.modifiers:
if mod.type == 'DISPLACE':
mod.strength /= self.container_scale
elif mod.type == 'SOLIDIFY':
mod.thickness /= self.container_scale
if self.fix_animations:
path_resolve = arm_ob.path_resolve
for action in bpy.data.actions:
if not validate_action(action, path_resolve):
continue
for fc in action.fcurves:
data_path = fc.data_path
if not data_path.endswith('location'):
continue
for kf in fc.keyframe_points:
kf.co[1] /= self.container_scale
return {'FINISHED'}
@make_annotations
class ExtractMetarig(bpy.types.Operator):
"""Create Metarig from current object"""
bl_idname = "object.expykit_extract_metarig"
bl_label = "Extract Metarig"
bl_description = "Create Metarig from current object"
bl_options = {'REGISTER', 'UNDO'}
rig_preset = EnumProperty(items=preset_handler.iterate_presets_with_current,
name="Rig Type",
)
offset_knee = FloatProperty(name='Offset Knee',
default=0.0)
offset_elbow = FloatProperty(name='Offset Elbow',
default=0.0)
offset_fingers = FloatVectorProperty(name='Offset Fingers')
no_face = BoolProperty(name='No face bones',
default=True)
rigify_names = BoolProperty(name='Use rigify names',
default=True,
description="Rename source rig bones to match Rigify Deform preset")
assign_metarig = BoolProperty(name='Assign metarig',
default=True,
description='Rigify will generate to the active object')
forward_spine_roll = BoolProperty(name='Align spine frontally', default=True,
description='Spine Z will face the Y axis')
apply_transforms = BoolProperty(name='Apply Transform', default=True,
description='Apply current source transforms before extraction')
def draw(self, context):
layout = self.layout
column = layout.column()
# if not context.active_object.data.expykit_retarget.has_settings():
row = column.row()
row.prop(self, 'rig_preset', text="Rig Type")
row = layout_split(column, factor=0.5, align=True)
row.label(text="Offset Knee")
row.prop(self, 'offset_knee', text='')
row = layout_split(column, factor=0.5, align=True)
row.label(text="Offset Elbow")
row.prop(self, 'offset_elbow', text='')
row = layout_split(column, factor=0.5, align=True)
row.label(text="Offset Fingers")
row.prop(self, 'offset_fingers', text='')
row = layout_split(column, factor=0.5, align=True)
row.label(text="No Face Bones")
row.prop(self, 'no_face', text='')
row = layout_split(column, factor=0.5, align=True)
row.label(text="Use Rigify Names")
row.prop(self, 'rigify_names', text='')
row = layout_split(column, factor=0.5, align=True)
row.label(text="Assign Metarig")
row.prop(self, 'assign_metarig', text='')
row = layout_split(column, factor=0.5, align=True)
row.label(text="Align spine frontally")
row.prop(self, 'forward_spine_roll', text='')
row = layout_split(column, factor=0.5, align=True)
row.label(text="Apply Transform")
row.prop(self, 'apply_transforms', text='')
@classmethod
def poll(cls, context):
if not context.object:
return False
if 'rigify' not in get_preferences(context).addons:
return False
if context.mode != 'POSE':
return False
if context.object.type != 'ARMATURE':
return False
return True
def execute(self, context):
src_object = context.object
src_armature = context.object.data
if not bone_mapping.get_rigify_version():
self.report({'WARNING'}, 'Cannot detect Rigify version')
return {'CANCELLED'}
if self.rig_preset == "--Current--":
current_settings = context.object.data.expykit_retarget
if current_settings.deform_preset and current_settings.deform_preset != '--':
deform_preset = current_settings.deform_preset
src_skeleton = preset_handler.set_preset_skel(deform_preset)
current_settings = src_skeleton
else:
src_settings = preset_handler.PresetSkeleton()
src_settings.copy(current_settings)
src_skeleton = preset_handler.get_settings_skel(src_settings)
elif self.rig_preset == "--":
src_skeleton = None
else:
src_skeleton = preset_handler.set_preset_skel(self.rig_preset)
current_settings = context.object.data.expykit_retarget
if not src_skeleton:
return {'FINISHED'}
# TODO: remove action, bring to rest pose
if self.apply_transforms:
rigged = (ob for ob in bpy.data.objects if
next((mod for mod in ob.modifiers if mod.type == 'ARMATURE' and mod.object == src_object),
None))
for ob in rigged:
ob.data.transform(src_object.matrix_local)
src_armature.transform(src_object.matrix_local)
src_object.matrix_local = Matrix()
met_skeleton = bone_mapping.RigifyMeta()
if self.rigify_names:
# check if doesn't contain rigify deform bones already
bones_needed = met_skeleton.spine.hips, met_skeleton.spine.spine, met_skeleton.spine.spine1
if not [b for b in bones_needed if b in src_armature.bones]:
# Converted settings should not be validated yet, as bones have not been renamed
if bone_mapping.rigify_version < (0, 5):
_preset_name = 'Rigify_Deform_0_4.py'
else:
_preset_name = 'Rigify_Deform.py'
src_skeleton, trg_skeleton = ConvertBoneNaming.convert_settings(current_settings, _preset_name, validate=False)
ConvertBoneNaming.rename_bones(context, src_skeleton, trg_skeleton, skip_ik=True)
src_skeleton = bone_mapping.RigifySkeleton()
for name_attr in ('left_eye', 'right_eye'):
bone_name = getattr(src_skeleton.face, name_attr)
if bone_name not in src_armature.bones and bone_name[4:] in src_armature.bones:
# fix eye bones lacking "DEF-" prefix on b3.2
setattr(src_skeleton.face, name_attr, bone_name[4:])
if src_skeleton.face.super_copy:
# supercopy def bones start with DEF-
bone_name = getattr(src_skeleton.face, name_attr)
if bone_name and not bone_name.startswith('DEF-'):
new_name = "DEF-{}".format(bone_name)
try:
context.object.data.bones[bone_name].name = new_name
except KeyError:
pass
else:
setattr(src_skeleton.face, name_attr, new_name)
src_to_met_map = src_skeleton.conversion_map(met_skeleton)
# bones that have rigify attr will be copied when the metarig is in edit mode
additional_bones = [(b.name, b.rigify_type) for b in src_object.pose.bones if b.rigify_type]
# look if there is a metarig for this rig already
metarig = None
for ob in bpy.data.objects:
if ob.type == 'ARMATURE':
# rigify from (0, 6, 1) has it as object ref
if hasattr(ob.data, "rigify_target_rig") and ob.data.rigify_target_rig:
if ob.data.rigify_target_rig == src_object:
metarig = ob
break
continue
# some versions from 0.6.1 have it
if hasattr(ob.data, "rigify_rig_basename") and ob.data.rigify_rig_basename:
if ob.data.rigify_rig_basename == src_object.name:
metarig = ob
break
continue
# in rigify 0.4, 0.5 it's partially implemented, but we set it ourselves (rigify only reads it, never writes)
if ob.get("rig_object_name") and ob["rig_object_name"] == src_object.name:
metarig = ob
break
if not metarig:
create_metarig = True
met_armature = bpy.data.armatures.new('metarig')
metarig = bpy.data.objects.new("metarig", met_armature)
if bpy.app.version < (2, 80):
context.scene.objects.link(metarig)
else:
context.collection.objects.link(metarig)
else:
met_armature = metarig.data
create_metarig = False
# getting real z_axes for src rest pose
src_z_axes = bone_utils.get_rest_z_axes(src_object, context)
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
if bpy.app.version < (2, 80):
metarig.select = True
context.scene.objects.active = metarig
else:
metarig.select_set(True)
context.view_layer.objects.active = metarig
# in local view it will fail prior 2.80 but in 2.80 it won't add it to the view as well
if context.space_data.local_view is not None:
if bpy.app.version < (2, 80):
met_obj_base = next((base for base in context.scene.object_bases if base.object == metarig))
if met_obj_base:
met_obj_base.layers_from_view(context.space_data)
else:
bpy.ops.view3d.localview(frame_selected=False)
src_object.select_set(True)
bpy.ops.view3d.localview(frame_selected=False)
src_object.select_set(False)
bpy.ops.object.mode_set(mode='EDIT')
if create_metarig:
from rigify.metarigs import human
human.create(metarig)
# measure some original meta proportions for later use
def get_body_proportions():
props = {}
bone_pairs = []
for attr in ("hips", "head", "neck", "spine2"):
try:
met_bone = met_armature.edit_bones[getattr(met_skeleton.spine, attr)]
src_bone = src_armature.bones[getattr(src_skeleton.spine, attr)]
except:
continue
if met_bone and src_bone:
bone_pairs.append((attr, met_bone, src_bone))
if len(bone_pairs) > 1:
break
if len(bone_pairs) < 2 or bone_pairs[0][0] != "hips":
return None
met_body_vector = bone_pairs[1][1].head - bone_pairs[0][1].head
src_body_vector = bone_pairs[1][2].head_local - bone_pairs[0][2].head_local
props["body_scale"] = src_body_vector.length / met_body_vector.length
props["hips_head"] = bone_pairs[0][1].head.copy()
return props
body_proportions = get_body_proportions()
def match_meta_spine(met_bone_group, src_bone_group, bone_attrs, axis=None):
# find existing bones
ms_bones = []
for bone_attr in bone_attrs:
met_bone_name = getattr(met_bone_group, bone_attr, None)
met_bone = met_armature.edit_bones.get(met_bone_name, None) if met_bone_name else None
src_bone_name = getattr(src_bone_group, bone_attr, None)
src_bone = src_armature.bones.get(src_bone_name, None) if src_bone_name else None
ms_bones.append((met_bone, src_bone))
# terminators must exist anyway
if not (ms_bones[0][0] and ms_bones[0][1] and ms_bones[-1][0] and ms_bones[-1][1]):
self.report({'ERROR'}, "First and last bone in the chain ({}..{}) must exist".format(bone_attrs[0], bone_attrs[-1]))
return
met_bones_to_kill = {ms[0] for ms in ms_bones if ms[0] and not ms[1]}
# place matched bones and set their rolls
for met_bone, src_bone in ((ms[0], ms[1]) for ms in ms_bones if ms[0] and ms[1]):
met_bone.head = src_bone.head_local
met_bone.tail = src_bone.tail_local
if axis:
met_bone.roll = bone_utils.ebone_roll_to_vector(met_bone, axis)
else:
src_z_axis = src_z_axes[src_bone.name]
met_bone.align_roll(src_z_axis)
for met_bone in met_bones_to_kill:
met_bone.length = 0.0
def match_meta_bone(met_bone_group, src_bone_group, bone_attr, axis=None):
try:
met_bone = met_armature.edit_bones[getattr(met_bone_group, bone_attr)]
src_bone_name = getattr(src_bone_group, bone_attr)
src_bone = src_armature.bones.get(src_bone_name, None)
except KeyError:
return
if not src_bone:
self.report({'WARNING'}, "{}, {} not found in {}".format(bone_attr, src_bone_name, src_armature))
return
met_bone.head = src_bone.head_local
met_bone.tail = src_bone.tail_local
if met_bone.parent and met_bone.use_connect:
bone_dir = met_bone.vector.normalized()
parent_dir = met_bone.parent.vector.normalized()
if bone_dir.dot(parent_dir) < -0.6:
self.report({'WARNING'}, "{} is not aligned with its parent".format(met_bone.name))
# TODO
if axis:
met_bone.roll = bone_utils.ebone_roll_to_vector(met_bone, axis)
else:
src_z_axis = src_z_axes[src_bone.name]
met_bone.align_roll(src_z_axis)
return met_bone
if self.forward_spine_roll:
align = Vector((0.0, -1.0, 0.0))
else:
align = None
match_meta_spine(met_skeleton.spine, src_skeleton.spine,
('hips', 'spine', 'spine1', 'spine2', 'neck', 'head'),
axis=align)
for bone_attr in ['shoulder', 'arm', 'forearm', 'hand']:
match_meta_bone(met_skeleton.right_arm, src_skeleton.right_arm, bone_attr)
match_meta_bone(met_skeleton.left_arm, src_skeleton.left_arm, bone_attr)
for bone_attr in ['upleg', 'leg', 'foot', 'toe']:
match_meta_bone(met_skeleton.right_leg, src_skeleton.right_leg, bone_attr)
match_meta_bone(met_skeleton.left_leg, src_skeleton.left_leg, bone_attr)
rigify_face_bones = set(bone_mapping.rigify_face_bones)
for bone_attr in ['left_eye', 'right_eye', 'jaw']:
met_bone = match_meta_bone(met_skeleton.face, src_skeleton.face, bone_attr)
if met_bone:
try:
rigify_face_bones.remove(met_skeleton.face[bone_attr])
except:
pass
if src_skeleton.face.super_copy:
metarig.pose.bones[met_bone.name].rigify_type = "basic.super_copy"
# FIXME: sometimes eye bone group is not renamed accordingly
# TODO: then maybe change jaw shape to box
try:
right_leg = met_armature.edit_bones[met_skeleton.right_leg.leg]
left_leg = met_armature.edit_bones[met_skeleton.left_leg.leg]
except KeyError:
pass
else:
offset = Vector((0.0, self.offset_knee, 0.0))
for bone in right_leg, left_leg:
bone.head += offset
try:
right_knee = met_armature.edit_bones[met_skeleton.right_arm.forearm]
left_knee = met_armature.edit_bones[met_skeleton.left_arm.forearm]
except KeyError:
pass
else:
offset = Vector((0.0, self.offset_elbow, 0.0))
for bone in right_knee, left_knee:
bone.head += offset
def match_meta_fingers(met_bone_group, src_bone_group, bone_attr):
met_bone_names = getattr(met_bone_group, bone_attr)
src_bone_names = getattr(src_bone_group, bone_attr)
if not src_bone_names:
print(bone_attr, "not found in", src_armature)
return
if not met_bone_names:
print(bone_attr, "not found in", src_armature)
return
# handle palm bones
if 'thumb' not in bone_attr:
# check if it's already mapped in 'meta' limb
try:
met_bone = met_armature.edit_bones[met_bone_names[3]]
src_bone = src_armature.bones.get(src_bone_names[3])
except Exception:
pass
if not met_bone or not src_bone:
try:
met_bone = met_armature.edit_bones[met_bone_names[0]]
src_bone = src_armature.bones.get(src_bone_names[0], None)
except KeyError:
pass
else:
if src_bone:
palm_bone = met_bone.parent
palm_bone.tail = src_bone.head_local
hand_bone = palm_bone.parent
palm_bone.head = hand_bone.head * 0.75 + src_bone.head_local * 0.25
# not a big deal to match palm's roll with the proximal's
src_z_axis = src_z_axes[src_bone.name]
palm_bone.align_roll(src_z_axis)
for i, (met_bone_name, src_bone_name) in enumerate(zip(met_bone_names, src_bone_names)):
if not src_bone_name:
continue
try:
met_bone = met_armature.edit_bones[met_bone_name]
src_bone = src_armature.bones[src_bone_name]
except KeyError:
print("source bone not found", src_bone_name)
continue
met_bone.head = src_bone.head_local
try:
met_bone.tail = src_bone.children[0].head_local
except IndexError:
try:
if i < 2:
src_bone_next = src_armature.bones[src_bone_names[i + 1]]
elif i == 3: # palm
src_bone_next = src_armature.bones[src_bone_names[0]]
else:
raise KeyError()
except KeyError:
bone_utils.align_to_closer_axis(src_bone, met_bone)
else:
met_bone.tail = src_bone_next.head_local
src_z_axis = src_z_axes[src_bone.name]
met_bone.align_roll(src_z_axis)
offset_fingers = matmul(Vector(self.offset_fingers), src_bone.matrix_local.to_3x3())
if met_bone.head.x < 0: # Right side
offset_fingers /= -100
else:
offset_fingers /= 100
if met_bone.parent.name in met_bone_names and met_bone.children:
met_bone.translate(offset_fingers)
for bone_attr in ['thumb', 'index', 'middle', 'ring', 'pinky']:
match_meta_fingers(met_skeleton.right_fingers, src_skeleton.right_fingers, bone_attr)
match_meta_fingers(met_skeleton.left_fingers, src_skeleton.left_fingers, bone_attr)
try:
met_armature.edit_bones['spine.003'].tail = met_armature.edit_bones['spine.004'].head
met_armature.edit_bones['spine.005'].head = (met_armature.edit_bones['spine.004'].head + met_armature.edit_bones['spine.006'].head) / 2
except KeyError:
pass
# find foot vertices
foot_verts = {}
foot_ob = None
# pick object with most foot verts
for ob in bone_utils.iterate_rigged_obs(src_object):
if src_skeleton.left_leg.foot not in ob.vertex_groups:
continue
grouped_verts = bone_utils.get_group_verts(ob, src_skeleton.left_leg.foot, threshold=0.8)
if len(grouped_verts) > len(foot_verts):
foot_verts = grouped_verts
foot_ob = ob
if foot_verts:
# find rear verts (heel)
rearest_y = max([foot_ob.data.vertices[v].co[1] for v in foot_verts])
leftmost_x = max([foot_ob.data.vertices[v].co[0] for v in foot_verts]) # FIXME: we should counter rotate verts for more accuracy
rightmost_x = min([foot_ob.data.vertices[v].co[0] for v in foot_verts])
for side in 'L', 'R':
# invert left/right vertices when we switch sides
leftmost_x, rightmost_x = rightmost_x, leftmost_x
heel_bone = met_armature.edit_bones['heel.02.' + side]
heel_bone.head.y = rearest_y
heel_bone.tail.y = rearest_y
if heel_bone.head.x > 0:
heel_head = leftmost_x
heel_tail = rightmost_x
else:
heel_head = rightmost_x * -1
heel_tail = leftmost_x * -1
heel_bone.head.x = heel_head
heel_bone.tail.x = heel_tail
try:
spine_bone = met_armature.edit_bones[met_skeleton.spine.hips]
pelvis_bone = met_armature.edit_bones['pelvis.' + side]
except KeyError:
pass
else:
offset = spine_bone.head - pelvis_bone.head
pelvis_bone.translate(offset)
if body_proportions:
pelvis_bone.length *= body_proportions["body_scale"]
try:
if body_proportions:
spine_bone = met_armature.edit_bones[met_skeleton.spine.hips]