因此,当我继续构建我的自动索具脚本时,我遇到了我预期难以解决的三个问题中的第二个。这可能是一个非常简单的答案:但脚本本身非常易于使用,首先点击生成代理定位器,然后点击构建脊椎关节:但问题出现在构建 IK 控件按钮上,它足够好地复制绑定(bind)关节并且重命名第一个:但我无法像绑定(bind)关节那样正确地增加数字:如果你扩展它,它只会给你名为“spine__IK”的 child 任何帮助总是很感激:

'''
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

我只需要脚本正确并按顺序重命名重复的关节链

最佳答案

您不能使用通配符来重命名 cmds.rename(name, ikName),其中 ikName 为 'spineJnt_*_IK' 将导致 'spineJnt__IK',因为在 Maya 中 * 是无效字符,将被下划线替换。但是你可以这样做:

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

什么导致或多或少正确的命名。这里的问题是 listRelatives 在父节点之前首先为您提供叶节点。所以你的编号将是相反的。这可以通过反转 id 来解决:
cmds.rename(name, 'spineJnt_{0}_IK'.format(len(ikList) - idx))

这应该会给你一个更好的结果。

关于python - 玛雅 Python : rename duplicated joint children,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60543702/

10-13 08:31