Maya:使用父约束连接两个关节链

问题描述 投票:0回答:1

所以这是我一直在研究的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

python maya
1个回答
0
投票

这是一个示例,它将从头开始创建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'。

© www.soinside.com 2019 - 2024. All rights reserved.