Delanimation

Delano Athias - Animator

Segmenting Joints with Python in Maya [Update]

In this mini course, we'll learn how to create a simple Python tool to segment selected joint chains. We'll also learn how to use an object's DAG path to avoid name-clash errors.

from maya import cmds

'''
Joint Segment Tool
Instructions: select root(s) of joint chain(s) and run script...
'''
#Store selected root joints in variable "sel_jt"
sel_jt = cmds.ls(sl=True, type='joint')
#Var for segment length
count = 9
#Var for joint radius offset value
rad_offset = 0.5
#Iterate through each root joint in selection and do the following...
for each_jt in sel_jt:
    #Print each selected joint
    print(each_jt)
    #Find the end joint of the selected root(s)
    end_jt = cmds.listRelatives(each_jt, c=True)[0]
    #Find path to end joint
    end_jt_path = each_jt + '|' + end_jt
    #Store length of chain
    end_offset = cmds.getAttr(end_jt_path + '.tx')
    print(end_offset)
    #Var used for the new radius val of each new joint
    rad = cmds.getAttr(each_jt + '.radius') - rad_offset
    #Force selection of each root joint to create the root of segmented chain
    cmds.select(each_jt, r=True)
    #Create root of segmented chain
    seg_root = cmds.joint(radius=rad)
    #Force selection of segment root to append segmented chain
    cmds.select(seg_root, r=True)
    #Add joints to segment tool based on value stored in "count" var
    for i in range(count):
        #Create a hierarchy of new segment joints
        seg_jt = cmds.joint(radius=rad)
        #Offset segment joints evenly along chain length
        cmds.move((end_offset/count), 0, 0, ls=True)