0

我正在寻找一种在 Maya 中为关节链添加后缀的方法。关节链具有特定的命名,因此我创建了一个包含它们需要的名称的列表。第一个链以“_1”为后缀,结果:

R_Clavicle_1|R_UpperArm_1|R_UnderArm_1|R_Wrist_1

当我创建第二个时,结果如下:

R_Clavicle_2|R_UpperArm_1|R_UnderArm_1|R_Wrist_1

编码:

DRClavPos = cmds.xform ('DRClavicle', q=True, ws=True, t=True)
DRUpArmPos = cmds.xform ('DRUpperArm', q=True, ws=True, t=True) 
DRUnArmPos = cmds.xform ('DRUnderArm', q=True, ws=True, t=True) 
DRWristPos = cmds.xform ('DRWrist', q=True, ws=True, t=True), cmds.xform('DRWrist', q=True, os=True, ro=True)

suffix = 1
jntsA = cmds.ls(type="joint", long=True)
while True:
    jntname = ["R_Clavicle_"+str(suffix),"R_UpperArm_"+str(suffix),"R_UnderArm_"+str(suffix),"R_Wrist_"+str(suffix)]
    if jntname not in jntsA:
        cmds.select (d=True)  
        cmds.joint ( p=(DRClavPos))
        cmds.joint ( p=(DRUpArmPos))
        cmds.joint ( 'joint1', e=True, zso=True, oj='xyz', radius=0.5, n=jntname[0])
        cmds.joint ( p=(DRUnArmPos))
        cmds.joint ( 'joint2', e=True, zso=True, oj='xyz', radius=0.5,  n=jntname[1])
        cmds.joint ( p=(DRWristPos[0]))
        cmds.joint ( 'joint3', e=True, zso=True, oj='xyz', radius=0.5,  n=jntname[2])
        cmds.rename ('joint4', jntname[3])
        cmds.select ( cl=True)
        break
    else:
        suffix + 1

我尝试在 jntname 中添加 +1,这导致第二条链很好,但第三条链在 R_Clavicle_3 之后有“_2”

在我看来,代码应该可以工作。谁能指出我正确的方向:)

4

2 回答 2

0

你永远不会重新绑定 suffix

else:
    suffix + 1

这是一个无操作。表达式返回2,然后完全忽略。自身引用的值suffix在这里永远不会受到影响。

您想重新分配到suffix那里:

else:
    suffix = suffix + 1

您也可以在那里使用增强的作业:

else:
    suffix += 1
于 2014-06-08T21:22:58.657 回答
0

看起来您的代码也将在场景中的每个关节上运行,这可能不是您真正想要的。

这里有一些基本的技巧可以帮助你解决这个问题。

  1. 为你的循环使用列表——你不需要'while true',循环遍历列表将遍历列表中的每个项目。因此,您可以使用以下方法击中场景中的每个关节:

    all_joint  = cmds.ls(type='joint')
    for each_joint in all_joints:
        do_something(each_joint)
    
  2. python 有一个非常方便的功能,称为列表推导 式,它允许您非常简单地从旧列表中创建新列表。例如:

    def suffixed_names (side, suffix)
        joint_names = ["_Clavicle_", "_UpperArm_", "_UnderArm_", "_Wrist_"]
        return  [side.upper() + jn + str(suffix) for jn in joint_names]
    

    这和你的一样,但打字少了很多(你也可以做左臂!)

  3. Python 有一个名为 zip 的便捷函数,它可以从其他列表中匹配的项目集合中创建一个新列表:

        names = ['a', 'b', 'c']
        values = [10, 20, 30]
        name_vals  = zip(names, values)
        # [('a', 10), ('b':20), ('c', 30)]
    
  4. Python 在循环方面非常聪明,所以如果你循环一个压缩列表,你可以得到这样的片段:

      for letter, number in named_vals:
          print letter, number
      #   a  10
      #   b  20
      #   c  30
    
  5. 它有助于将您的功能分解为更小的功能,以便清楚发生了什么

把所有这些放在一起,你可能会得到类似的东西:

   def get_original_positions(clav, upper, under, wrist):
       # get all the underlying positions in order
       results = []
       for each_bone in [clav, upper, under, wrist]:
           results.append(cmd.xform (each_bone, q=True, t=True, ws=True))
       return results

    def create_named_arm(side, suffix, clav, under, upper, wrist):
        original_pos_list = get_original_positions(clav, upper, under, wrist)
        suffixed_name_list = suffixed_names (side, suffix)

        cmds.select(cl=True) # clear before starting

        created_joints = []
        for pos, name in zip (original_pos_list, suffixed_name_list):
            created_joints.append( cmds.joint(name, p = pos))

        # now loop back and orient the joints...
        for each_new_joint in created_joints:
            cmds.joint(each_new_joint, zso = True, oj= 'xyz')

        return created_joints

   # you do need to supply the names of the original joints:
   create_named_arm ('r', 1, 'DRClavicle', 'DRUpperArm', 'DRUnderArm','DRWrist')

   # a second chain:
   create_named_arm ('r', 2, 'DRClavicle', 'DRUpperArm', 'DRUnderArm','DRWrist')

这在前端有更多的计划,但从长远来看更容易阅读和使用。Python 列表很棒!爱他们。

于 2014-06-09T03:40:25.060 回答