Hi Liee,
In your code example it looks like you're looping over your joints, and for every loop you're creating a controller. However, you are not changing the name of the controller, which makes that maya tries to create a controller with the same name every time, except that by default, maya does not accept multiple objects with the same name, which means it is adding a digit at the end of the new controller's name, which is probably what you are expecting.
The problem arises when you are trying to create the parent constraint, because you are not changing your controller's name here either, which means for every loop, you are constraining the first controller created (the only one that is actually named 'ctrl_bone') to the current joint.
I would suggest either iterating over your list with the enumerate method, or with an incremented number, which would allow you to create custom names, or storing the name of the controller created in a variable in order to reuse it later (or a combination of both).
Incremented method :
targetJoints = cmds.ls( sl=1 , typ='joint' )
ctrl_number = 1
for targetJoint in targetJoints :
ctrl_name = 'ctrl_bone' + str(ctrl_number)
cmds.circle( c=(0, offsetY, 0) , nr=nr , sw=360 , r=radius , ch=0 , n=ctrl_name);
# parent shape
grp_ctrl_name = 'grp_' + ctrl_name
cmds.group ( n= grp_ctrl_name , em=1)
cmds.parent (ctrl_name , grp_ctrl_name , r=1 )
cmds.parent ( grp_ctrl_name , targetJoint , r=1 )
cmds.parent ( grp_ctrl_name , w=1 , r=0 )
# constraint
cmds.parentConstraint ( ctrl_name , targetJoint, mo=1)
enumerate method:
targetJoints = cmds.ls( sl=1 , typ='joint' )
for i, targetJoint in enumerate(targetJoints) :
ctrl_number = i+1
ctrl_name = 'ctrl_bone' + str(ctrl_number)
cmds.circle( c=(0, offsetY, 0) , nr=nr , sw=360 , r=radius , ch=0 , n=ctrl_name);
# parent shape
grp_ctrl_name = 'grp_' + ctrl_name
cmds.group ( n= grp_ctrl_name , em=1)
cmds.parent (ctrl_name , grp_ctrl_name , r=1 )
cmds.parent ( grp_ctrl_name , targetJoint , r=1 )
cmds.parent ( grp_ctrl_name , w=1 , r=0 )
# constraint
cmds.parentConstraint ( ctrl_name , targetJoint, mo=1)
storing name in variables:
targetJoints = cmds.ls( sl=1 , typ='joint' )
for targetJoint in targetJoints :
ctrl_name = cmds.circle( c=(0, offsetY, 0) , nr=nr , sw=360 , r=radius , ch=0 , n= "ctrl_bone");
# parent shape
grp_name = cmds.group ( n= "grp_ctrl_bone" , em=1)
cmds.parent ( ctrl_name , grp_name , r=1 )
cmds.parent ( grp_name, targetJoint , r=1 )
cmds.parent ( grp_name , w=1 , r=0 )
# constraint
cmds.parentConstraint ( ctrl_name , targetJoint, mo=1)
# rename group
grp_ctrl_bone = cmds.rename(grp_name , 'grp_' + targetJoint)
cmds.rename( ctrl_name , 'ctrl_' + targetJoint )
In your case, the cleanest would probably be :
targetJoints = cmds.ls( sl=True , typ='joint' )
for targetJoint in targetJoints :
ctrl_name = 'ctrl_' + targetJoint
grp_name = 'grp_' + targetJoint
# storing the actual name in a variable, in case there is already something named like that in the scene
ctrl_name = cmds.circle( c=(0, offsetY, 0) , nr=nr , sw=360 , r=radius , ch=False , n= ctrl_name);
# parent shape
cmds.group ( n=grp_name, r=True) # without the em flag turned on, it will parent the circle you just created in the newly created group, removing one step
cmds.parent ( grp_name , targetJoint , r=True )
cmds.parent ( grp_name , w=True , r=False )
# constraint
cmds.parentConstraint ( ctrl_name , targetJoint, mo=True)
It is generally good practice to use the python keywords True and False for booleans instead of numbers, as it makes more obvious what is an actual number against what is a bool.
It is also usually good to use long names for flags as it makes it more obvious for the next person who will read your code (probably you, 6 months down the line, and you will thank yourself for using long names ^^)