Suffix if statement

1.4k Views Asked by At

I was looking for a way to add a suffix to jointchain in Maya. The jointchain has specific naming so I create a list with the names they need to be. The first chain has "_1" as suffix, result:

R_Clavicle_1|R_UpperArm_1|R_UnderArm_1|R_Wrist_1

When I create the second this is the result:

R_Clavicle_2|R_UpperArm_1|R_UnderArm_1|R_Wrist_1

The code:

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

I tried adding +1 in jntname which resulted in a good second chain but the third chain had "_2" after R_Clavicle_3

The code, in my eyes should work. Can anybody point me in the correct direction :)

2

There are 2 best solutions below

2
On BEST ANSWER

It looks like your code will also be running on every joint in the scene, which may not really be what you want.

Here are a few basic techniques that will help you with this problems.

  1. Use lists for your loops - you don't need to 'while true', looping through the list will go through every item in your list. So you can hit every joint in the scene with:

    all_joint  = cmds.ls(type='joint')
    for each_joint in all_joints:
        do_something(each_joint)
    
  2. python has an extremely handy feature known as list comprehensions This allows you to make new lists out of old ones very simply. For example:

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

    which is the same as yours but a lot less typing (and you can do left arms too!)

  3. Python has a handy function called zip, which will make a new list out of matching sets of items from other lists:

        names = ['a', 'b', 'c']
        values = [10, 20, 30]
        name_vals  = zip(names, values)
        # [('a', 10), ('b':20), ('c', 30)]
    
  4. Python is very clever about looping, so if you loop over a zipped list you can get the pieces like so:

      for letter, number in named_vals:
          print letter, number
      #   a  10
      #   b  20
      #   c  30
    
  5. It helps to break your functions down into smaller ones so that it's clear what's going on

Putting all that together, you might get something like:

   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')

This is a bit more planning on the front end, but much easier to read and work with over the long haul. Python lists are awesome! lean to love 'em.

4
On

You are never rebinding suffix:

else:
    suffix + 1

This is a no-op. The expression returns 2, which is then ignored altogether. The value referenced by suffix itself is never affected here.

You want to re-assign to suffix there:

else:
    suffix = suffix + 1

You can use an augmented assignment there too:

else:
    suffix += 1