繁体   English   中英

为什么按代码复制关节不起作用?

[英]why is duplicating joints by code not working?

我在 Maya 中通过代码创建多个关节,这就是我想要做的。 像这样创建并养育他们......

L_Arm_00IK L_Arm01IK 父级 L_Arm02IK 父级

L_Arm_00FK L_Arm01FK 的父级 L_Arm02FK 的父级

L_Arm_00IKDriver L_Arm01IKDriver 父 L_Arm02IKDriver 父

L_Arm_00Blend L_Arm01Blend 父 L_Arm02Blend

但是当我运行我的代码时,创建了第一个关节 L_Arm_00IK、L_Arm_00FK、L_Arm_00IKDriver、L_Arm_00Blend

但他们的孩子没有被创造。 我究竟做错了什么? 看起来 def duplicate_chain 没有通过。

def ikfkchain(name, side, parent, joints, fktemplate, iktemplate, pvtemplate, fkcolor, ikcolor):



fk_joints = duplicate_chain(joints, None, "_JNT", "Fk_JNT")
ik_joints = duplicate_chain(joints, None, "_JNT", "Ik_JNT")
ik_driver_joints = duplicate_chain(joints, None, "_JNT", "IkDriver_JNT")
blend_joints = duplicate_chain(joints, None, "_JNT", "Blend_JNT")





def duplicate_chain(joints, parent, replace_what, replace_with):
new_joints = []
new_joints_parent = parent

for jnt in joints:
    new_jnt = cmds.duplicate(jnt, n=jnt.replace(replace_what, replace_with), po=True)[0]
    if new_joints_parent:
        cmds.parent(new_jnt, new_joints_parent)


    new_joints_parent = new_jnt 
    new_joints.append(new_jnt)

    return new_joints

看起来你只是在你的循环中返回,这意味着你的 function 通过循环一次,并且没有进一步循环就返回了。

您只需像这样更改代码即可修复它:

def duplicate_chain(joints, parent, replace_what, replace_with):
    new_joints = []
    new_joints_parent = parent
    
    for jnt in joints:
        print(jnt)
        new_jnt = cmds.duplicate(jnt, n=jnt.replace(replace_what, replace_with), po=True)[0]
        print(new_jnt)
        if new_joints_parent:
            cmds.parent(new_jnt, new_joints_parent)
    
    
        new_joints_parent = new_jnt 
        new_joints.append(new_jnt)
    
    return new_joints

也就是说,在 Maya 中复制对象时,默认情况下 Maya 也会复制该 object 的整个后代层次结构。

你可以做一些像这样简单的事情:

from maya import cmds

top_joint = cmds.ls(selection=True)

duplicated_top_joint = cmds.duplicate(top_joint)

这将复制您的整个层次结构。 这在复制关节链时通常就足够了,因为无论如何我们通常都需要整个链。

然后你可以从层次结构的底部开始重命名它们:

def rename_jnts(top_jnt, replace_what, replace_with):
    new_joints = cmds.listRelatives(top_jnt, allDescendents=True, fullPath=True) # we use fullPath so that each of the names contain the full hierarchy, in order to rename properly
    new_joints.append(top_jnt)
    new_joints.sort(key=len) # we sort by length to get the joints in hierarchical order
    new_joints.reverse() # we reverse the list to get the longer names first, since these are long names that means that the joints at the bottom of the hierarchy will be first in that list, and the first ones in the hierarchy will be last, this avoids renaming the top joints first and having Maya not find the other ones because their long names will have changed.
    for jnt in new_joints:
        short_name = jnt.split("|")[-1] # first we split the long name by the pipe (|) character and keep the last token to get only the short name 
        new_name = short_name.replace(replace_what, replace_with) # We build the new name by replacing the proper token
        cmds.rename(jnt, new_name) # rename

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM