后缀if语句



我正在寻找一种在Maya中添加jointchain后缀的方法。jointchain具有特定的命名,因此我创建了一个包含所需名称的列表。第一个链以"_1"作为后缀,result:

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"

代码,在我眼里应该是可行的。谁能给我指一下正确的方向吗?

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

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

  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列表太棒了!学会爱他们。

您永远不会重新绑定 suffix:

else:
    suffix + 1

这是一个no-op。表达式返回2,然后将其完全忽略。suffix本身引用的值在这里不受影响。

您想在那里重新分配给suffix:

else:
    suffix = suffix + 1

也可以使用增广赋值:

else:
    suffix += 1

最新更新