Auto Attaching Motion Capture Data to a Rig
- dashamakarishcheva
- Feb 9, 2023
- 2 min read
Updated: Mar 12, 2023
The video demonstrates the execution of my script, which is customized to attached to the Maverick rig, provided by Ahmed Elmatarawi
In early 2023 I as tasked with attaching motion capture data (.fbx out of Shogun Post) to a rig in Maya. When shown the repeating process of retargeting, connecting, and constraining locators, joints, and controls I figured that it would save a significant amount of time to automate the process.
This is the code that I made. The top section declares the joints on the motion capture .fbx and then the controls associated with them. The motion capture joints out of Shogun Post will always export with the same names, so the first Joints in the list can be left alone.
To use this code for your own rig, just replace the names of the relevant control associated with the specific joint.
"""
Dasha Makarishcheva
February 2022
"""
import maya.cmds as cmds;
import math;
# Modify this
# Partner the name of each joint of the Motion Capture Data to its parallel Rig Control
AllData = [
# Arms
("RightHand", "R_maverick_wrist_0_bind_FK_0_CTRL"),
("LeftHand", "L_maverick_wrist_0_bind_FK_0_CTRL"),
("LeftForeArm", "L_maverick_elbow_0_bind_FK_0_CTRL"),
("RightForeArm", "R_maverick_elbow_0_bind_FK_0_CTRL"),
("RightArm", "R_maverick_shoulder_0_bind_FK_0_CTRL"),
("LeftArm", "L_maverick_shoulder_0_bind_FK_0_CTRL"),
("RightFoot", "R_maverick_ankle_0_bind_FK_0_CTRL"),
("LeftFoot", "L_maverick_ankle_0_bind_FK_0_CTRL"),
("RightLeg", "R_maverick_knee_0_bind_FK_0_CTRL"),
("LeftLeg", "L_maverick_knee_0_bind_FK_0_CTRL"),
("RightUpLeg", "R_maverick_hip_0_bind_FK_0_CTRL"),
("LeftUpLeg", "L_maverick_hip_0_bind_FK_0_CTRL"),
("Neck", "C_maverick_head_CTRL"),
#("Spine3", "C_maverick_spine_04_bind2_0_CTRL"),
("Spine2", "C_maverick_spine_03_bind2_0_CTRL"),
("Spine1", "C_maverick_spine_02_bind2_0_CTRL"),
("Spine", "C_maverick_spine_01_bind2_0_CTRL"),
("Hips_ncl1_1", "C_maverick_COG_CTRL"),
#Toes (Breaks/Is Funky)
#("RightToeBase", "R_maverick_ball_0_bind_FK_0_CTRL"),
#("LeftToeBase", "L_maverick_ball_0_bind_FK_0_CTRL"),
]
def AttachRigToMocap(AllData):
num = 1
for i in AllData:
#Creates locator per joint
cmds.spaceLocator(n = "Locator_%s"%(str(num)))
#Fetches XYZ position of joint
JointPos = cmds.xform(str(i[0]), query=True, translation=True, worldSpace=True)
#Prints XYZ position of joint
print("Joint" + str(num) + " pos: " + str(JointPos[0]) + ", " + str(JointPos[1]) + ", " + str(JointPos[2]))
#Moves locator to Joint position
cmds.parent("Locator_%s"%(str(num)),str(i[0]))
#Makes translation and rotation of locator 0
cmds.select("Locator_%s"%(str(num)), af = True)
cmds.move(JointPos[0],JointPos[1],JointPos[2], worldSpace = True)
cmds.setAttr("Locator_%s.rx"%(str(num)),0)
cmds.setAttr("Locator_%s.ry"%(str(num)),0)
cmds.setAttr("Locator_%s.rz"%(str(num)),0)
#clear selection
cmds.select(cl=True)
#Matches rotation of Locator to control
cmds.matchTransform("Locator_%s"%(str(num)),str(i[1]), pos=False,rot=True,scale=False)
if str(i[0]) == 'Hips_ncl1_1':
#Creates parent constraint if attaching waist
cmds.parentConstraint("Locator_%s"%(str(num)),str(i[1]))
else:
#Creates an orient constraint between the control and the locator
cmds.orientConstraint("Locator_%s"%(str(num)),str(i[1]))
#Adds to num for new unique locator in loop
num += 1
AttachRigToMocap(AllData)