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

2024-10-02 12:34:13 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我一直在研究的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中的括号有关


Tags: tochildtrueforrootstartadik
1条回答
网友
1楼 · 发布于 2024-10-02 12:34:13

以下示例将从头创建两个单独的关节链,然后对每个关节应用父约束,以便一个链驱动另一个链:

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

主要的想法是,您可以得到两个列表,每个列表都有自己的关节。然后将这两个列表传递给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”

相关问题 更多 >

    热门问题