Skip to content

Instantly share code, notes, and snippets.

@rBrenick
Last active June 8, 2023 08:10
Show Gist options
  • Save rBrenick/da7df752a5dae52648cb56f608e30e89 to your computer and use it in GitHub Desktop.
Save rBrenick/da7df752a5dae52648cb56f608e30e89 to your computer and use it in GitHub Desktop.
Maya script that connects up a skeleton to IKRig
import pymel.core as pm
# This is a script that connects one of the rigs from Maya's Content Browser to the IKRig from https://github.com/chadmv/cmt
base_ik_rig_list = [
"IKRig_Root",
"IKRig_Hips",
"IKRig_Chest",
"IKRig_Neck",
"IKRig_Head",
"IKRig_LeftClavicle",
"IKRig_LeftShoulder",
"IKRig_LeftElbow",
"IKRig_LeftHand",
"IKRig_LeftUpLeg",
"IKRig_LeftLoLeg",
"IKRig_LeftFoot",
"IKRig_RightClavicle",
"IKRig_RightShoulder",
"IKRig_RightElbow",
"IKRig_RightHand",
"IKRig_RightUpLeg",
"IKRig_RightLoLeg",
"IKRig_RightFoot",
"IKRig_LeftThumb01",
"IKRig_LeftThumb02",
"IKRig_LeftThumb03",
"IKRig_LeftIndex01",
"IKRig_LeftIndex02",
"IKRig_LeftIndex03",
"IKRig_LeftMiddle01",
"IKRig_LeftMiddle02",
"IKRig_LeftMiddle03",
"IKRig_LeftRing01",
"IKRig_LeftRing02",
"IKRig_LeftRing03",
"IKRig_LeftPinky01",
"IKRig_LeftPinky02",
"IKRig_LeftPinky03",
"IKRig_RightThumb01",
"IKRig_RightThumb02",
"IKRig_RightThumb03",
"IKRig_RightIndex01",
"IKRig_RightIndex02",
"IKRig_RightIndex03",
"IKRig_RightMiddle01",
"IKRig_RightMiddle02",
"IKRig_RightMiddle03",
"IKRig_RightRing01",
"IKRig_RightRing02",
"IKRig_RightRing03",
"IKRig_RightPinky01",
"IKRig_RightPinky02",
"IKRig_RightPinky03",
]
# example skeleton mapping
character_ik_rig_map = {
"IKRig_Root": "root",
"IKRig_Hips": "hip",
"IKRig_Chest": "spine_02",
"IKRig_Neck": "neck",
"IKRig_Head": "head",
"IKRig_LeftClavicle": "shoulder_l",
"IKRig_LeftShoulder": "upperarm_l",
"IKRig_LeftElbow": "lowerarm_l",
"IKRig_LeftHand": "hand_l",
"IKRig_LeftUpLeg": "upperleg_l",
"IKRig_LeftLoLeg": "lowerleg_l",
"IKRig_LeftFoot": "foot_l",
"IKRig_RightClavicle": "shoulder_r",
"IKRig_RightShoulder": "upperarm_r",
"IKRig_RightElbow": "lowerarm_r",
"IKRig_RightHand": "hand_r",
"IKRig_RightUpLeg": "upperleg_r",
"IKRig_RightLoLeg": "lowerleg_r",
"IKRig_RightFoot": "foot_r",
}
ik_rig_node = pm.PyNode("ikRig1") # You'll need to create this node yourself
#
# In the scene I have 4 skeletons
# char_0: - retarget source skeleton
# char_0_rest: - retarget source skeleton base t-pose
#
# char_1: - retarget output skeleton
# char_1_rest: - retarget output skeleton base t-pose
#
# output skeleton needed zeroed out jointOrients
#
# The "output skeleton base t-pose" needed a tiny rotation in the elbow bend axis.
# otherwise the solver had a completetely straight arm
#
for index, map_name in enumerate(base_ik_rig_list):
joint_name = character_ik_rig_map.get(map_name)
if not joint_name:
continue
print index, joint_name
# source base t-pose
pm.setAttr("{}.inRestMatrix[{}]".format(ik_rig_node, index), pm.getAttr("char_0_rest:{}.worldMatrix[0]".format(joint_name)))
# output base t-pose
pm.setAttr("{}.targetRestMatrix[{}]".format(ik_rig_node, index), pm.getAttr("char_1_rest:{}.worldMatrix[0]".format(joint_name)))
# source animation
pm.connectAttr("char_0:" + joint_name + ".worldMatrix[0]", "{}.inMatrix[{}]".format(ik_rig_node, index), force=True)
# you can either turn off inheritsTransform or output to some transforms without parent transforms
pm.setAttr("char_1:{}.inheritsTransform".format(joint_name), False)
# connect output
pm.connectAttr("{}.outputTranslate[{}]".format(ik_rig_node, index), "char_1:{}.translate".format(joint_name), force=True)
pm.connectAttr("{}.outputRotate[{}]".format(ik_rig_node, index), "char_1:{}.rotate".format(joint_name), force=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment