Maya Python: rename duplicated joint children

Dasteel :

So as I continue building my auto rigging script I've run into the second out of three problems I anticipated would be difficult to solve. This is probably a really simple answer: but the script itself is very easy to use, first hit generate proxy locators, then hit build spine joints: but the problem comes in at the Build IK controls button, It duplicates the bound joints well enough and renames the first one: but I cant get it to properly number increment like the bound joints are: if you expand it, it just gives you children named "spine__IK" any help is always appreciated:

'''
import DS_hybrid_spineOmatic_V1
reload (DS_hybrid_spineOmatic_V1)
DS_hybrid_spineOmatic_V1.gui()
'''

import re
import maya.cmds as cmds
import maya.mel as mel

if cmds.window("spineWin", exists =True):
    cmds.deleteUI("spineWin", window = True)

myWindow = cmds.window("spineWin",t='DS_hybrid_spineOmatic_V1',w=200, h=500, toolbox=True)
column = cmds.columnLayout(adj=True)

'''
To DO:
    -You're going to have a series of scrips splitting into an IKFK spine and a ribon spine: this script will build the IKFK spine
    -make build spine joints orient joint chain
'''

def gui():

    cmds.button( label="Generate Spine Proxy Locators", c = buildProxies)
    cmds.separator( w=200, h=3)
    cmds.button( label="Build Spine Joints", c = buildJointChain)
    cmds.separator( w=200, h=3)
    cmds.button( label="Build IK Controls", c = createIKcontrols)
    cmds.separator( w=200, h=9)

    cmds.setParent('..')
    cmds.showWindow(myWindow)

def buildProxies(*args):
    locAmount = 2
    for i in range(locAmount):
        countLoc = i+1
        spaceLoc = cmds.spaceLocator(n = 'spineLoc_{}_PRX'.format(countLoc), p = [0,i*2.5,0])
        cmds.makeIdentity(spaceLoc, a=1, t=1)
        mel.eval('CenterPivot;')

    cmds.parent('spineLoc_2_PRX','spineLoc_1_PRX')

def buildJointChain(*args):

    cmds.select(cl=True) #this line clears your selection

    #this For in range loop creates equidistant joints between the 2 proxy locators
    #//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    countJnt = 7

    startLoc = "spineLoc_1_PRX" #this is where the joint chain starts
    endLoc = "spineLoc_2_PRX" #this is where the joint chain ends

    jntPosSteps = 1.0/(countJnt-1) # Will use count to calculate how much it should increase percentage by each iteration. Need to do -1 so the joints reach both start and end points.
    jntPerc = 0  # This will always go between a range of 0.0 - 1.0, and we'll use this in the constraint's weights.

    for jNum in range(countJnt):
        jntInc = jNum
        jnt = cmds.joint(n = 'spineJnt_{}_Bound'.format(jntInc))
        cmds.setAttr(jnt + ".displayLocalAxis", True) #display the local rotation axis of the joint

        jntConstraint = cmds.pointConstraint(startLoc, jnt, weight=1.0 - jntPerc)[0] # Apply 1st constraint, with inverse of current percentage.
        cmds.pointConstraint(endLoc, jnt, weight=jntPerc)
        cmds.delete(jntConstraint) #constraint is now no longer neccisary

        jntPerc += jntPosSteps #Increase percentage for next iteration
    #//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    #this will orient the joint chain(build later)
    #//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    #//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    cmds.select(cl=True)
    cmds.group(n='skeletonGrp',empty=True,world=True)
    cmds.parent('spineJnt_0_Bound','skeletonGrp')
    cmds.delete('spineLoc_1_PRX')

def createIKcontrols(*args):

    #create spine control curves
    #//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    #//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    #create duplicate joint chain
    #//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ikName = 'spineJnt_*_IK'
    ikChain = cmds.duplicate('spineJnt_0_Bound', n='spineJnt_0_IK')[0]
    cmds.parent(ikChain,world=True)
    ikList = cmds.listRelatives(ikChain,ad=True,pa=True)
    for name in ikList:
        cmds.rename(name, ikName)
        print(ikList)
    #//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    #create IK spline handle for joint chain

I just need the script to rename the duplicated joint chains properly and sequentially

haggi krey :

You cannot use a wildcard for renaming cmds.rename(name, ikName) where ikName is 'spineJnt_*_IK' will result in 'spineJnt___IK' because in maya a * is in invalid character and will be replaced by an underscore. But you can do this:

for idx, name in enumerate(ikList):
    cmds.rename(name, 'spineJnt_{0}_IK'.format(idx))
    print(ikList)

What results in a more or less proper naming. The problem here is that the listRelatives give you the leaf nodes first before the parent ones. So your numbering will be inverse. This can be fixed by inverting the id:

cmds.rename(name, 'spineJnt_{0}_IK'.format(len(ikList) - idx))

This should give you a better result.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=26590&siteId=1