Wednesday, October 2, 2013

Joint Labeling Function [Maya Python]

This is a Python function that I have created to help me do joint labeling in Maya.

[UPDATES:]
Oct 6th, 2013 - Add help docs. To access this type, 'acJntLabel.__doc__' or 'help(acJntLabel)'
Oct 8th, 2013 - Add functionality for user to pass joints through a selection or a list.

Video Demo:


Source Code:

 """  
 Title: AC_jointLabel.py  
 Author: Andrew Kin Fun Chan  
 Email: AndrewChan1985@gmail.com  
 Date: Sept 2013  
 Version: 1.0  
 Compatibility: Maya 2011+ (It will probably work in older versions as well. Contact me if you have issues.)  
 """  
 import maya.cmds as mc  
 def toList(objects):  
   """  
   This function takes in data and returns it as a list.  
   """  
   if isinstance(objects, str):  
     return [objects]  
   elif isinstance(objects, tuple):  
     return list(objects)  
   return objects  
 #acceptable side keys to use.  
 sideDict = {  
       'Center':0,   
       'Left':1,   
       'Right':2,  
       'None':3  
       }  
 #acceptable type keys to use.  
 typeDict = {  
       'None':0,  
       'Root':1,  
       'Hip':2,  
       'Knee':3,  
       'Foot':4,  
       'Toe':5,  
       'Spine':6,   
       'Neck':7,   
       'Head':8,   
       'Collar':9,   
       'Shoulder':10,  
       'Elbow':11,  
       'Hand':12,  
       'Finger':13,  
       'Thumb':14,  
       'PropA':15,  
       'PropB':16,  
       'PropC':17,  
       'Other':18,  
       'Index Finger':19,  
       'Middle Finger':20,  
       'Ring Finger':21,  
       'Pinky Finger':22,  
       'Extra Finger':23,  
       'Big Toe':24,  
       'Index Toe':25,  
       'Middle Toe':26,  
       'Ring Toe':27,  
       'Pinky Toe':28,  
       'Extra Toe':29  
       }  
 acJntLabel ('Left', 'Other',None, 'oi')  
 def acJntLabel (side, type, joints = None, *args):  
   """  
   [Description:] This is a function that joint labels using the user input or selected joints.  
   [Example:]  
     If the selected joint names were...  
       lf_joint1_bnd, lf_joint2_bnd, lf_joint3_bnd  
     I would want to assign the side to Left, type to Other,   
     and remove 'lf_' & '_bnd' from the joint label name.   
     acJntLabel ('Left', 'Other', None, 'lf_','_bnd')  
   @param side: Name of joint label side.  
   @type side: *str*  
     Acceptable side parameters:  
       'Center', 'Left', 'Right', 'None'  
   @param type: Name of joint label type.  
   @type side: *str*  
     Acceptable type parameters:  
       'None', 'Root', 'Hip', 'Knee', 'Foot', 'Toe', 'Spine', 'Neck', 'Head', 'Collar', 'Shoulder',   
       'Elbow', 'Hand', 'Finger', 'Thumb', 'PropA', 'PropB', 'PropC', 'Other', 'Index Finger', 'Middle Finger',  
       'Ring Finger', 'Pinky Finger', 'Extra Finger', 'Big Toe', 'Index Toe', 'Middle Toe', 'Ring Toe',  
       'Pinky Toe', 'Extra Toe'  
   @param joints: List of joint names to label. If param is set to None, then get joints based off user selection.  
   @type side: *str*, *tuple*, *list*   
   @param *args: Name of strings you would like to remove from the joint name.  
   @type *args: *str*  
   """  
   joints = toList(joints)  
   if not joints:  
      joints = mc.ls (sl = True)  
      if not joints:  
        raise RuntimeError('Must select or pass in joints.')  
   if side not in sideDict.keys():  
     raise RuntimeError('Side name,%s, must be one of the following: %s' % (side, sideDict.keys()))    
   if type not in typeDict.keys():  
     raise RuntimeError('Type name, %s, must be one of the following: %s' % (type, typeDict.keys()))  
   for jnt in joints:  
     print jnt  
     if not mc.objExists(jnt):  
       print '%s joint does not exist... Continuing to next joint.' % (jnt)  
       continue  
     #assigning the side and type to all selected joints.  
     mc.setAttr ((jnt + '.side'), sideDict[side])  
     mc.setAttr ((jnt + '.type'), typeDict[type])  
     jntNewName = jnt  
     #looping through the args to remove them from the name.  
     for i, arg in enumerate(args):  
       jntNewName = jntNewName.replace(arg, '')  
     #assign the name of the joint label with the new name.  
     mc.setAttr ((jnt+'.otherType'), jntNewName, type ='string' )  

No comments: