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

why is duplicating joints by code not working?(为什么按代码复制关节不起作用?)

本文介绍了为什么按代码复制关节不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Maya中通过代码创建多个关节,这就是我想要做的。创建它们,并像这样将它们设置为父对象...

L_Arm01IK父级的L_ARM_00IK父级

L_Arm01FK父级的L_ARM_00FK父级

L_ARM_00IKDriver父级L_Arm01IKDriver父级L_Arm02IKDriver

L_Arm01的L_ARM_00混合父项L_Arm02的混合父项

但当我运行代码时,会创建第一个关节L_ARM_00IK、L_ARM_00FK、L_ARM_00IKDriver、L_ARM_00Blend

但它们的子代并不是被创建的。我做错了什么?似乎没有通过定义DIPLICATE_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

推荐答案

看起来您只是在循环中返回,这意味着您的函数只执行一次循环,然后返回,不会进一步循环。

只需如下更改代码即可修复:

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也会复制该对象的整个子体层次结构。

您可以只做这样简单的事情:

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

这篇关于为什么按代码复制关节不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么按代码复制关节不起作用?