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 :)
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.
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:
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:
which is the same as yours but a lot less typing (and you can do left arms too!)
Python has a handy function called zip, which will make a new list out of matching sets of items from other lists:
Python is very clever about looping, so if you loop over a zipped list you can get the pieces like so:
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:
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.