Rigify: refactor leg to make modifying the foot roll mechanism easier.

This commit is contained in:
Alexander Gavrilov 2022-01-10 21:09:03 +03:00
parent ed42feaed5
commit 4cd7c8fc05
3 changed files with 24 additions and 11 deletions

View File

@ -130,6 +130,11 @@ def convert_pose_matrix_via_rest_delta(mat, from_bone, to_bone):
return mat @ from_bone.bone.matrix_local.inverted() @ to_bone.bone.matrix_local
def convert_pose_matrix_via_pose_delta(mat, from_bone, to_bone):
"""Convert pose of one bone to another bone, preserving the current pose difference between them."""
return mat @ from_bone.matrix.inverted() @ to_bone.matrix
def get_local_pose_matrix(pose_bone):
""" Returns the local transform matrix of the given pose bone.
"""

View File

@ -232,7 +232,7 @@ class Rig(BaseLimbRig):
@stage.parent_bones
def parent_ik_toe_control(self):
if self.use_ik_toe:
self.set_bone_parent(self.bones.ctrl.ik_toe, self.bones.mch.heel[2])
self.set_bone_parent(self.bones.ctrl.ik_toe, self.get_mch_heel_toe_output())
@stage.configure_bones
def configure_ik_toe_control(self):
@ -247,6 +247,9 @@ class Rig(BaseLimbRig):
####################################################
# Heel roll MCH
def get_mch_heel_toe_output(self):
return self.bones.mch.heel[-3]
@stage.generate_bones
def make_roll_mch_chain(self):
orgs = self.bones.org.main
@ -322,7 +325,7 @@ class Rig(BaseLimbRig):
def parent_fk_parent_bone(self, i, parent_mch, prev_ctrl, org, prev_org):
if i == 3:
if not self.use_ik_toe:
align_bone_orientation(self.obj, parent_mch, self.bones.mch.heel[2])
align_bone_orientation(self.obj, parent_mch, self.get_mch_heel_toe_output())
self.set_bone_parent(parent_mch, prev_org, use_connect=True)
else:
@ -334,7 +337,7 @@ class Rig(BaseLimbRig):
def rig_fk_parent_bone(self, i, parent_mch, org):
if i == 3:
if not self.use_ik_toe:
con = self.make_constraint(parent_mch, 'COPY_TRANSFORMS', self.bones.mch.heel[2])
con = self.make_constraint(parent_mch, 'COPY_TRANSFORMS', self.get_mch_heel_toe_output())
self.make_driver(con, 'influence', variables=[(self.prop_bone, 'IK_FK')], polynomial=[1.0, -1.0])

View File

@ -1026,32 +1026,37 @@ class RigifyLimbIk2FkBase:
mat = convert_pose_matrix_via_rest_delta(mat, ik, ctrl)
set_transform_from_matrix(obj, ctrl.name, mat, keyflags=keyflags)
def assign_extra_controls(self, context, obj, all_matrices, ik_bones, ctrl_bones):
for extra in self.extra_ctrl_list:
set_transform_from_matrix(
obj, extra, Matrix.Identity(4), space='LOCAL', keyflags=self.keyflags
)
def apply_frame_state(self, context, obj, all_matrices):
ik_bones = [ obj.pose.bones[k] for k in self.ik_bone_list ]
ctrl_bones = [ obj.pose.bones[k] for k in self.ctrl_bone_list ]
tail_bones = [ obj.pose.bones[k] for k in self.tail_bone_list ]
assert len(all_matrices) == len(ik_bones) + len(tail_bones)
assert len(all_matrices) >= len(ik_bones) + len(tail_bones)
matrices = all_matrices[0:len(ik_bones)]
tail_matrices = all_matrices[len(ik_bones):]
use_pole = self.get_use_pole(obj)
# Remove foot heel transform, if present
self.assign_extra_controls(context, obj, all_matrices, ik_bones, ctrl_bones)
context.view_layer.update()
# Set the end control position
endmat = convert_pose_matrix_via_rest_delta(matrices[-1], ik_bones[-1], ctrl_bones[-1])
endmat = convert_pose_matrix_via_pose_delta(matrices[-1], ik_bones[-1], ctrl_bones[-1])
set_transform_from_matrix(
obj, self.ctrl_bone_list[-1], endmat, keyflags=self.keyflags,
undo_copy_scale=True,
)
# Remove foot heel transform, if present
for extra in self.extra_ctrl_list:
set_transform_from_matrix(
obj, extra, Matrix.Identity(4), space='LOCAL', keyflags=self.keyflags
)
# Set the base bone position
ctrl_bones[0].matrix_basis = Matrix.Identity(4)