domenica 23 marzo 2014

pymel: FK Chain from Joints Hierarchy



Hi!

This is a procedure written in Pymel.
It helps to create a system FK selecting the root joint. All controls are aligned along the X axis of the joints.

The process uses a recursive algorithm. The function hierarchy(), in fact, calls itself and can give information about the joint hierarchy.
It works on any type of joint hierarchy.
Bye!


import pymel.core as pm

def hierarchy( root, parent ):
    for joint in root:
        child = joint.getChildren( type = "joint" )
        if child != []:
            anchor, ctrl = createCircleOnJoint( joint )
            pm.parentConstraint( ctrl, joint, mo = 1 )
            if parent != "":
                pm.parentConstraint( parent, anchor, mo = 1 )
            hierarchy( child, ctrl )
    
def createCircleOnJoint( joint ):
    circle = pm.circle( nr = (1,0,0) )[0]
    offset = pm.group( circle, n = "GRP_offset_%s"%circle )
    anchor = pm.group( offset, n = "GRP_anchor_%s"%circle )
    constraint = pm.parentConstraint( joint, anchor, mo = 0 )
    pm.delete( constraint )
    return anchor, circle
    
def makeFK():      
    joints = pm.ls( selection = 1, type = "joint" )
    hierarchy( joints, "" )
    
# RUN makeFK() on selected Joint