Sunday, October 26, 2014

Create a Nurbs Circle Shaped Control Script.

 So, this is usually my first method of getting folks' feet wet with scripting while they are learning rigging, then by the time they get to scripting they are at least familiar with what the Python language looks like, examples of how it works, and some of the logic that can be used to accomplish a small scripting tool.  This example is used to create a nurbs circle control with a few various useful options for the tool.  I typically throw this in just after they have rigged their first character, nothing is more frustrating than creating 30 FK controls for fingers by hand every time you need a rig.  This code is using Python in Maya running with the PyMel libraries.  The code is below...



import pymel.core as pm


def create_circle_control( i_sJoint='', 
                            i_vNormal=[1,0,0], 
                            i_bOrientToJoint=True, 
                            i_sRigConstraintType='parent', 
                            i_bCreateAnimGrp=False,
                            i_iColor=17 ):
                                
    """
    Function to create a simple nurbs shaped control
    
    Args:
        i_sJoint (string) : name of joint to create the control at
        i_vNormal (vector) : direction of the circle to draw
        i_bOrientToJoint (bool) : determine if the control should be oriented to joint
        i_sRigConstraintType (sting) : type of rig constraint to create if any
        i_bCreateAnimGrp (bool) : determine if an anim/sdk group needs to be created
        i_iColor (int) : color value for the circle shape override color attribute    
    
    """
    
    # create the circle control
    control = pm.circle( name=i_sJoint + "_Ctrl",
                        radius=0.25,
                        normal=i_vNormal, 
                        constructionHistory=False )
                        
    # create the pad group
    pad = pm.group( name=i_sJoint + "_Pad",
                    empty=True )
    
    # parent the control to the pad
    pm.parent( control, pad )
    
    # If createAnim, create the anim group and parent
    if i_bCreateAnimGrp:
        anim = pm.group( name=i_sJoint + "_Anim", empty=True )
        pm.parent( control, anim )
        pm.parent( anim, pad )
    
    # if you want the control oriented to the joint, use parentConstraint
    if i_bOrientToJoint:
        pm.delete( pm.parentConstraint( i_sJoint,
                                        pad,
                                        maintainOffset=False, 
                                        weight=1.0 ) )
    else:
        pm.delete( pm.pointConstraint( i_sJoint, 
                                       pad, 
                                       maintainOffset=False, 
                                       weight=1.0 ) )
    
    # if rigConstraint is a parent constraint
    if i_sRigConstraintType == 'parent':
        pm.parentConstraint( control, 
                            i_sJoint, 
                            maintainOffset=True, 
                            weight=1.0 )
               
    # if rigConstraint is a point constraint             
    elif i_sRigConstraintType == 'point':
        pm.pointConstraint( control, 
                            i_sJoint, 
                            maintainOffset=True, 
                            weight=1.0 )

    # if rigConstraint is a orient constraint  
    elif i_sRigConstraintType == 'orient':
        pm.orientConstraint( control, 
                            i_sJoint, 
                            maintainOffset=True, 
                            weight=1.0 )
    
    # Set override color
    control[0].getShape().setAttr( 'overrideEnabled', True )
    control[0].getShape().setAttr( 'overrideColor', i_iColor )
    
    # Get the joint parent
    sJointParent = i_sJoint.getParent()
    
    # if the joint parent's ctrl exists, parent this new pad to it
    if sJointParent:
        if pm.objExists( sJointParent + "_Ctrl" ):
            pm.parent( pad, sJointParent + "_Ctrl" )


# Loop through the selection and create controls
for item in pm.ls( selection=True ):
    create_circle_control( item )\


No comments:

Post a Comment