I think your ‘ctrl’ variable is causing the issue. Since you’re creating it in a for loop, by the time you exit the loop, Maya thinks you are only referring to the last controller when you try and append your list to ‘CK’. Instead of adding all your controls within one list, you are adding lists within a larger list.
Example : you are doing this “CK = [[ctrl], [ctrl], [ctrl], [ctrl], etc.]” rather than “CK = [ctrl, ctrl, ctrl, ctrl, etc.]”
The solution would be to simply add ‘[0]’ to ‘ctrl’ when you append everything to ‘CK’.
Another problem is you’re trying to toss your joints under your controller’s shape nodes (or at least that’s what you say you are trying to do, right?). The issue with this is you are using ‘mc. parent’ which only works with transform nodes. I’m assuming you are trying to “hide” your joints within your controllers so they don’t show up in the outliner—otherwise, I’m not sure why this would be desirable.
I haven’t tried seeing if there is another way to do parent your joints under your shape nodes but if you want to try the code below, maybe that will give you what you’re after. Everything works at least in terms of function and hierarchy but like I explained above, you can still see your joints despite them being parented under your control’s shape nodes.
import maya.cmds as mc
#create variable for the selected joints
selected = mc.ls(sl=True)
#clears the selection
mc.select(cl=True)
#create empty list for the new controls
CK = []
offsetList = []
#for the joints in selection, run the following code
for s in selected:
#create variable for the position/rotation of the joints
pos = mc.xform(s, q=True, t=True, ws=True)
rot = mc.xform(s, q=True, rotation=True, ws=True)
#create 3lvl controls and position them on top of joints
ctrl = mc.circle(n=str(s + '_ctrl'), radius=0.6,nr=(1,0,0), ch=False)
mc.group(n=str(s + '_sdk'))
offset = mc.group(n=str(s + '_offset'))
#snap the controls to the position of the joint
mc.xform(offset, translation=pos, ws=True)
mc.xform(offset, rotation=rot, ws=True)
#append controls and offsets to list
CK.append(ctrl[0]) # add '[0]' otherwise you will nest lists within list
offsetList.append(offset)
#list all the shape nodes within a list
for s in CK:
derp = mc.listRelatives(CK, s=True) # list shape for all controls
#store length of shape nodes
shapeCount = 0
#parent all the joints within the list under the shape nodes from Derp variable
for each in selected:
mc.select(selected[shapeCount], derp[shapeCount], replace=True, noExpand=True)
mc.parent()
shapeCount += 1
#store length of shape nodes
shapeCount = 1
#parent 'offset' groups to joints
for each in selected:
mc.select(offsetList[shapeCount], each, replace=True)
mc.parent()
shapeCount += 1
# add one to 'shapeCount' until it reaches last 'offset' group
if shapeCount > len(offsetList) -1:
#delselect all. generate warning
mc.select(offsetList[0], replace=True) # select top 'offset'
mc.warning("All joints have been parented.")
break
One last thing to keep in mind is your control’s orientation depends on the orientation of your joints. That said, if you are finding any of the controls are misaligned or not oriented the right way, it’s due to your joints. In that case I would simply orient things like you want and then your controls will be oriented accordingly. Hope that helps.
-Bryan