# ------------------------------------------------------------------------------ # gbCreateFK.py - Maya Python Script # ------------------------------------------------------------------------------ # # DESCRIPTION: # This script will create FK controls to drive the joints you have selected. # It uses the names of the joints to rename adjs, ctrls, shapes and constrs. # Works with jnt names without suffix or with .Jnt, .jnt, .Joint or .joint. # # OTHER SCRIPTS REQUIREMENTS: # Requires gbZeroOut.py. # # USAGE: # (PYTHON) import gbCreateFK; gbCreateFK.createFK() # (MEL) python("import gbCreateFK;gbCreateFK.createFK()") # # AUTHOR: # Giorgio Bertolone - mail@giorgiobertolone.com - www.giorgiobertolone.com # Copyright (C)2010 Giorgio Bertolone - All Rights Reserved. # # VERSIONS: # 1.00 - Dec 25, 2010 - Initial Release # # ------------------------------------------------------------------------------ import maya.cmds as mc import maya.mel as mel import gbZeroOut import sys def createFK(): # check for selection errors sel = mc.ls( sl=True ) for i in sel: id = mc.nodeType( i ) if len(sel) < 1: mc.confirmDialog( title="Selection Error", message="Nothing is selected. Please select one or more joints.", button=["Ok"], defaultButton="Ok" ) sys.exit() if id != "joint": mc.confirmDialog( title="Selection Error", message="Something in your selection is not a joint. Please select only joints.", button=["Ok"], defaultButton="Ok" ) sys.exit() # if one or more joints are selected go on if id == "joint" and len(sel) >= 1: for i in sel: # get the parent joint and store it upJnt = mc.listRelatives(i, ap = 1) # check the end name of each joint and use it for constraint and control names if "_Jnt" in i: i_Jnt = i.replace('_Jnt', '', 1) ctrlname = "%s%s"%( i_Jnt, "_Ctrl") constrname = ( i_Jnt + "_Jnt_ParentConstraint" ) adjname = "%s%s"%( i_Jnt, "_Ctrl_Adj") adjconstr = "%s%s"%( i_Jnt, "_Ctrl_Adj_ParentConstraint") elif "_jnt" in i: i_jnt = i.replace('_jnt', '', 1) ctrlname = "%s%s"%( i_jnt, "_Ctrl") constrname = ( i_jnt + "_Jnt_ParentConstraint" ) adjname = "%s%s"%( i_jnt, "_Ctrl_Adj") adjconstr = "%s%s"%( i_jnt, "_Ctrl_Adj_ParentConstraint") elif "_Joint" in i: i_Joint = i.replace('_Joint', '', 1) ctrlname = "%s%s"%( i_Joint, "_Ctrl") constrname = ( i_Joint + "_Jnt_ParentConstraint" ) adjname = "%s%s"%( i_Joint, "_Ctrl_Adj") adjconstr = "%s%s"%( i_Joint, "_Ctrl_Adj_ParentConstraint") elif "_joint" in i: i_joint = i.replace('_joint', '', 1) ctrlname = "%s%s"%( i_joint, "_Ctrl") constrname = ( i_joint + "_Jnt_ParentConstraint" ) adjname = "%s%s"%( i_joint, "_Ctrl_Adj") adjconstr = "%s%s"%( i_joint, "_Ctrl_Adj_ParentConstraint") shapename = "%s%s"%( i_joint, '') else: ctrlname = "%s%s"%( i, "_Ctrl") constrname = ( i + "_Jnt_ParentConstraint" ) adjname = "%s%s"%( i, "_Ctrl_Adj") adjconstr = "%s%s"%( i, "_Ctrl_Adj_ParentConstraint") # for each joint create a circle ctrl with radius 4, rename it properly and change the shape color to a light blue ctrl = mc.circle( nr=(1, 0, 0), c=(0, 0, 0), n=ctrlname, r=4 ) # mc.setAttr( ctrl[0] + ".overrideEnabled", 1 ) # mc.setAttr( ctrl[0] + ".overrideColor", 18 ) mc.setAttr( ctrl[0]+"Shape" + ".overrideEnabled", 1 ) mc.setAttr( ctrl[0]+"Shape" + ".overrideColor", 18 ) # match position and orientation of each ctrl with the relative joint and zero out using gbZeroOut.py mc.parentConstraint( i, ctrl, w=1, n="tempParentConstraint" ) mc.delete( "tempParentConstraint" ) gbZeroOut.ZeroOut() # create and rename each constraint to the joints mc.parentConstraint( ctrl, i, w=1, mo=True, n=constrname ) # get rid of the history on the nurbs shape of each ctrl mc.select( ctrl ) mel.eval("DeleteHistory;") # if there is a parent jnt use it to parent constraint the Adj of the Ctrl if upJnt != None: mc.parentConstraint( upJnt, adjname, w=1, mo=True, n=adjconstr ) # lock and hide scale attributes of each cntrl mc.setAttr( '%s.sx' % ctrlname, lock=True, keyable=False ) mc.setAttr( '%s.sy' % ctrlname, lock=True, keyable=False ) mc.setAttr( '%s.sz' % ctrlname, lock=True, keyable=False )