首页 > 解决方案 > Maya:使用父约束连接两个关节链

问题描述

这是我一直在研究的 IK 脊柱构建器的片段。我已经弄清楚如何制作列表以将绑定复制到 IK 链中,但我一直坚持的是我想要我的列表和 for 循环到父约束绑定层次结构中的每个关节到它在 ik 中的相应关节等级制度:

    import maya.cmds as cmds

def linkJointChain(lookFor='joint'):
    namePref = 'ct_'
    limbPref = 'spine'
    ctlName = namePref + limbPref

    #list selection to get the joint and their children
    root = cmds.ls(sl=True)[0] # adding a zero bracket makes sure it counts the head of the herarchy too
    child = cmds.listRelatives(root,ad=1,type='joint')
    child.append(root)
    child.reverse()
    limbJnt = child
    print(child)

    #list all joints in chain, this list will be refrenced by all the commands beneath it
    root = cmds.ls(sl=True)[0]
    child = cmds.listRelatives(root,ad=1,f=True,children=True,type='joint')

    #rename the joints
    for j, name in enumerate(child):
        cmds.rename(name,namePref + limbPref + 'AJ{0}_BIND_JNT'.format(len(child)-j))
        print(child)

    #rename beggining and end joints to start and end respectivly
    root = cmds.ls(sl=True)
    child = cmds.listRelatives(root,ad=1,f=True,children=True,type='joint')
    cmds.rename(child[0],ctlName +'AJ_BIND_END_JNT')
    cmds.rename(root,ctlName + 'AJ_BIND_START_JNT')


    #duplicate bound chain for ik spine
    root = cmds.ls(sl=True)
    IKChain = cmds.duplicate(root,n=ctlName + 'AJ_IK_START_JNT')
    IKList = cmds.listRelatives(ctlName + 'AJ_IK_START_JNT', ad=True,pa=True)
    for IKn, name in enumerate(IKList):
        cmds.rename(name, ctlName +'AJ{0}_IK_JNT'.format(len(IKList)-IKn))
        print(IKList)

        #select IK chain, then,set joints size for easy grabbing on IK chain
        cmds.select(ctlName +'AJ_IK_START_JNT')

        IKRoot = cmds.ls(sl=True)[0] 
        IKChild = cmds.listRelatives(ctlName +'AJ_IK_START_JNT', ad=True,pa=True)
        IKChild.append(IKRoot)

        for r in IKChild:
            cmds.setAttr(r + '.radius', 1.5)


    #parent constrain bound spine to ik spine
    ikJntChain=cmds.listRelatives(ctlName +'AJ_IK_START_JNT',ad=1,type='joint')
    ikJntChain.append(ctlName +'AJ_IK_START_JNT') #try appending your other joint chain to create a double list with which to append
    ikJntChain.reverse()
    ikLimbJnt = ikJntChain

    boundJntChain=cmds.listRelatives(ctlName +'AJ_BIND_START_JNT',ad=1,type='joint')
    boundJntChain.append(ctlName +'AJ_BIND_START_JNT') #try appending your other joint chain to create a double list with which to append
    boundJntChain.reverse()
    boundLimbJnt = boundJntChain

    limbJnt = ikJntChain+boundJntChain

    print(limbJnt)

    for j in limbJnt:
        spineCons = cmds.parentConstraint(ikJntChain[0],boundJntChain[0])
        #ikParChain = cmds.parentConstraint(j,ikJntChain)

linkJointChain()

该脚本具有 listRelatives 的硬编码名称,因为在重命名列表中的第一个和最后一个关节后,完整的脚本会读取关节链并将控件放置在开始和结束关节处,我知道它与 cmds.parentConstraint 中的括号有关

标签: pythonmaya

解决方案


这是一个示例,它将从头开始创建 2 个单独的关节链,然后将父约束应用于每个关节,以便一个链驱动另一个链:

import maya.cmds as cmds

joint_count = 10

# Create 1st joint chain 'a'.
chain_a = [
    cmds.joint(position=[0, i * -2 + ((joint_count - 1) * 2), 0], name="a#")
    for i in range(joint_count)]

cmds.select(clear=True)  # Need to clear selection so the next chain doesn't accidentally parent to chain a.

# Create 2nd joint chain 'b'.
chain_b = [
    cmds.joint(position=[0, i * -2 + ((joint_count - 1) * 2), -10], name="b#")
    for i in range(joint_count)]

# Use `zip` to iterate through both lists at the same time.
for jnt_a, jnt_b in zip(chain_a, chain_b):
    cmds.parentConstraint(jnt_a, jnt_b, maintainOffset=True)  # Constraint b->a

主要思想是你得到 2 个列表,每个列表都有自己的关节。然后将这两个列表传递给zip,这样当您遍历它时,它将首先通过两个第一个关节,然后是两个第二个关节,依此类推。

要使其正常工作,您必须确保两个列表具有相同的长度,并且都使用相同的联合顺序。这样您就不必对任何东西进行硬编码,而是可以按程序进行(例如,您可以更改joint_count为任何数字,它仍然可以工作)。

您实际上甚至不需要使用zip并且可以通过像这样替换结尾来实现相同的目的:

for i in range(len(chain_a)):
    cmds.parentConstraint(chain_a[i], chain_b[i], maintainOffset=True)  # Constraint b->a

虽然使用zip感觉更“pythonic”。


推荐阅读