Skip to content

Commit

Permalink
ExtractMetarig: now uses mapped metarig bone names;
Browse files Browse the repository at this point in the history
  • Loading branch information
mechanic committed Mar 26, 2024
1 parent e974639 commit d857e87
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
19 changes: 10 additions & 9 deletions bone_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,25 @@ def copy_bone_constraints(bone_a, bone_b):
continue


def copy_bone_to_arm(src_ob, trg_ob, bone_name, suffix='CP'):
"""Create a new bone in trg_ob with the same head/tail as bone with the given name"""
def copy_bone_to_arm(src_ob, trg_ob, src_bone_name, trg_bone_name="", suffix='CP'):
"""Create a new bone (or align existing) in trg_ob with the same head/tail as bone with the given name"""
try:
src_bone = src_ob.data.bones[bone_name]
src_bone = src_ob.data.bones[src_bone_name]
except KeyError:
return

new_name = '_'.join((bone_name, suffix)) if suffix else bone_name
trg_bone_name = src_bone_name if not trg_bone_name else trg_bone_name
trg_bone_name = '_'.join((trg_bone_name, suffix)) if suffix else trg_bone_name

try:
new_bone = trg_ob.data.edit_bones[new_name]
trg_bone = trg_ob.data.edit_bones[trg_bone_name]
except KeyError:
new_bone = trg_ob.data.edit_bones.new(new_name)
trg_bone = trg_ob.data.edit_bones.new(trg_bone_name)

new_bone.head = src_bone.head_local
new_bone.tail = src_bone.tail_local
trg_bone.head = src_bone.head_local
trg_bone.tail = src_bone.tail_local

return new_bone.name
return trg_bone.name


def copy_bone(ob, bone_name, assign_name='', constraints=False, deform_bone='SAME'):
Expand Down
25 changes: 18 additions & 7 deletions operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,8 @@ def execute(self, context):

met_skeleton = bone_mapping.RigifyMeta()

src_to_met_map = src_skeleton.conversion_map(met_skeleton)

if self.rigify_names:
# check if doesn't contain rigify deform bones already
bones_needed = met_skeleton.spine.hips, met_skeleton.spine.spine
Expand Down Expand Up @@ -947,7 +949,12 @@ def match_meta_fingers(met_bone_group, src_bone_group, bone_attr):
met_armature.edit_bones.remove(face_bone)

for src_name, src_attr in additional_bones:
new_bone_name = bone_utils.copy_bone_to_arm(src_object, metarig, src_name, suffix="")
# find target bone name by src_name
met_bone_name = src_to_met_map.get(src_name)
if not met_bone_name:
continue

new_bone_name = bone_utils.copy_bone_to_arm(src_object, metarig, src_name, trg_bone_name=met_bone_name, suffix="")

if 'chain' in src_attr: # TODO: also fingers
# working around weird bug: sometimes src_armature.bones causes KeyError even if the bone is there
Expand All @@ -961,7 +968,7 @@ def match_meta_fingers(met_bone_group, src_bone_group, bone_attr):
except IndexError:
break

child_bone_name = bone_utils.copy_bone_to_arm(src_object, metarig, bone.name, suffix="")
child_bone_name = bone_utils.copy_bone_to_arm(src_object, metarig, bone.name, trg_bone_name=src_to_met_map.get(bone.name), suffix="")
child_bone = met_armature.edit_bones[child_bone_name]
child_bone.parent = met_armature.edit_bones[new_parent_name]
child_bone.use_connect = True
Expand All @@ -974,9 +981,11 @@ def match_meta_fingers(met_bone_group, src_bone_group, bone_attr):

if bone:
if bone.parent:
# FIXME: should use mapping to get parent bone name
# DONE: should use mapping to get parent bone name
parent_name = bone.parent.name.replace('DEF-', '')
met_armature.edit_bones[new_bone_name].parent = met_armature.edit_bones[parent_name]
met_parent_name = src_to_met_map.get(parent_name)
if met_parent_name:
met_armature.edit_bones[new_bone_name].parent = met_armature.edit_bones[met_parent_name]
if ".raw_" in src_attr:
met_armature.edit_bones[new_bone_name].use_deform = bone.use_deform
elif bone.name.startswith('DEF-'):
Expand All @@ -985,15 +994,17 @@ def match_meta_fingers(met_bone_group, src_bone_group, bone_attr):
else:
bone.name = "DEF-{}".format(bone.name)
except KeyError:
self.report({'WARNING'}, "bones not found in target, perhaps wrong preset?")
self.report({'WARNING'}, "parent bone [{}] not found in target, perhaps wrong preset?".format(parent_name))
continue

bpy.ops.object.mode_set(mode='POSE')
# now we can copy the stored rigify attrs
for src_name, src_attr in additional_bones:
src_meta = src_name[4:] if src_name.startswith('DEF-') else src_name
metarig.pose.bones[src_meta].rigify_type = src_attr
# TODO: should copy rigify options of specific types as well
src_meta = src_to_met_map.get(src_meta)
if src_meta:
metarig.pose.bones[src_meta].rigify_type = src_attr
# TODO: should copy rigify options of specific types as well

if current_settings.left_leg.upleg_twist_02 or current_settings.left_leg.leg_twist_02:
metarig.pose.bones['thigh.L']['rigify_parameters']['segments'] = 3
Expand Down
7 changes: 5 additions & 2 deletions rig_mapping/bone_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ def conversion_map(self, target_skeleton, skip_ik=False):
bone_map = dict()

def bone_mapping(attr, limb, bone_name):
if not bone_name:
return

target_limbs = getattr(target_skeleton, attr, None)
if not target_limbs:
return
Expand Down Expand Up @@ -318,9 +321,9 @@ def fingers_mapping(src_fingers, trg_fingers):
for finger, bone_names in src_fingers.items():
trg_bone_names = trg_fingers[finger]

assert len(bone_names) == len(trg_bone_names)
for bone, trg_bone in zip(bone_names, trg_bone_names):
bone_map[bone] = trg_bone
if bone and trg_bone:
bone_map[bone] = trg_bone

trg_fingers = target_skeleton.left_fingers
fingers_mapping(self.left_fingers, trg_fingers)
Expand Down

0 comments on commit d857e87

Please sign in to comment.