【问题标题】:Maya Python: rename duplicated joint childrenMaya Python:重命名重复的联合子项
【发布时间】:2020-06-17 23:41:28
【问题描述】:

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

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

我只需要脚本来正确且顺序地重命名重复的关节链

【问题讨论】:

    标签: python maya autodesk


    【解决方案1】:

    您不能使用通配符重命名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))
    

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

    【讨论】:

    • 哈吉克雷做到了,非常感谢。如果你不介意我问:什么是叶节点?
    • 形状在变换下分组,变换可以组合在一起并构建一个也称为树的层次结构。层次结构末端的节点是树的叶子,在这种情况下是形状节点。
    猜你喜欢
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 2019-04-27
    • 2020-06-07
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    相关资源
    最近更新 更多