Maya Auto rig tool help

Hi, I am fairly new to python coding, and after following a number of tutorials I wanted to make some Auto rig tools to speed up the making of a uni rigg I have.

for now, I am only trying to automate the low part of the rig e.g legs in order to get my head around the code however I am coming across a problem when trying to parent constraint joints to locators and making there higher arches

so the basis of the tool is to create a number of proxy locators then position them in the world and then create a joint hierarchy base on their position. Unfortunately, I am stuck at the joints

Here is the code I am using for the locators and this works fine

def create_Loc():

	amount = 6

	for i in range(amount):

		count = i+1

		loc = cmds.spaceLocator (n = 'locator_{}_PRX'.format (count), p = [0,i*2.5,0])
		cmds.makeIdentity (loc, a = 1, t = 1)

		mel.eval ('CenterPivot;') ('*_PRX')

	lyr = cmds.createDisplayLayer (n = 'locators', num = 1)
	cmds.setAttr ('locators.color', 22)

however, when going on to create the joints I would like them to be based on the locator’s positioning with specific naming conventions here is the code I have right now witch takes the names of a list using a for loop and creates the joint for each name in the list

 # Make joints 
names  = ['C_Pelvis_jnt','l_leg_jnt','l_knee_jnt','l_ankle_jnt','l_toe_jnt','l_foot_ball_jnt'] 
	for name in names: (cl = 1)
		jnt = cmds.joint (n = name , sc = 0 )

however, I am struggling with the next part of the code which is to parent constraint each joint to the right locator I have tried to use something within the lines of

# Constrain joints to Locators
	Loc = ('*_PRX')
	if Loc:	
		con1 = cmds.parentConstraint(Loc[0],names[5])
		con2 = cmds.parentConstraint(Loc[1],names[4])
		con3 = cmds.parentConstraint(Loc[2],names[3])
		con4 = cmds.parentConstraint(Loc[3],names[2])
		con5 = cmds.parentConstraint(Loc[4],names[1])
		con6 = cmds.parentConstraint(Loc[5],names[0])

		cmds.delete(con1,con2,con3,con4,con5,con6) #delete Constraints

# create joint hierarchy		
	cmds.parent (names[1],names[0])
	cmds.parent (names[2],names[1])
	cmds.parent (names[3],names[2])
	cmds.parent (names[4],names[3])
	cmds.parent (names[5],names[4])

But I was thinking there must be a way to make this code shorter and more accurate as to which constraints you are deleting. I also tried to use a for loop in order to parent the joints into a hierarchy.

for n in names:
	cmds.parent (n)

however, the for loop did not parent them in the order of the list from 0 to 5, so I was wondering if anyone can give me some advice on how to do this better and more compact.

If you are sure your list is sorted, try with enumerate(list):

names = ['a', 'b', 'c', 'd', 'e']
for i, n in enumerate(names):
    if i == len(names)-1:
    print(names[i+1], n)

('b', 'a')
('c', 'b')
('d', 'c')
('e', 'd')

1 Like

Thanks so much for this example, i just started doing this 2 months ago and i did not know about this function however reading about it i can see how powerfull it can be.

For what it’s worth, iterating through hierarchies is always a tiny bit annoying even with experience.
In general try to imagine it as a moving window, where each iteration you look up the main transform, and then either the parent or child relative to it.

To be even more compact, you can use a slice on list to only iterate over all but the last transform (for example in joint chains, you usually don’t need to orient / skin the end joint). Eg

for i, name in enumerate( names[ :-1 ] ) : # iterate over all but the last item
    parent = name
    child = names[ i+1 ]  # always valid, no need for if-check on index

If you’re new I recommend reading up on slices - this Google guide helped me most, it’s geared to strings but the slice notation works on all sequences in Python.