Joint Creation based on Locators

Hi, I am writing a code that uses locators to create a joint hierarchy. So far I’ve got a function that creates locators based on the amount I input.

def create_Loc():

	# a = print input('set number of locators')
	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;')		
	cmds.select ('*_PRX')

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

Then I have a function that uses the locators to create a join hierarchy based on said locators.

def create_JNT():
 
 # Make joints 
	
	names  = ['C_Pelvis_jnt','l_leg_jnt','l_knee_jnt','l_ankle_jnt','l_toe_jnt','l_foot_ball_jnt'] # Name of joints 
	sc = 0 
	for name in names:

		cmds.select (cl = 1)
		jnt = cmds.joint (n = name , sc = sc )
	  
# Constrain joints to Locators
	Loc = cmds.ls ('*_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

	for i, n in enumerate(names):
	    if i ==len(names)-1:
	        continue
	    cmds.parent (names[i+1], n)	 

	jnt = cmds.select('C_Pelvis_jnt', hi = 1)    
	if jnt:
		cmds.makeIdentity (jnt, a = 1, t =1, r = 1, s = 1)

	cmds.mirrorJoint ('l_leg_jnt', mb = True, myz = True, sr = ('l_', 'r_') )

	cmds.select(cl = 1)

Right now to position the joints I use a command to list all my locators then go in one by one through the list constraining them based on the joint list I have created.

	Loc = cmds.ls ('*_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

I am new to coding and when I started this code I was following Frigging Awesome Studios coding tutorials to help me out. In the tutorials, he uses the locators naming conventions to create and parent the joints using ``cmds.listRelatives` as a way to create a hierarchy.

however, as I learn and write this code I would like to make it more flexible and create a UI around it that allows users to change the number of locators and input the joint names they want. As the code is right now I do not think it will be possible to do this based on the way I am positioning the joints.

What I am asking is if there is a better way to do this that allows me, in the same way, to parent the joints based on a locator hierarchy but also use a list of names to do that within with that are determined by the user. I am thinking it may work with a enumerate for loom but I am not sure how they work or how to approach them.

P.s. Sorry if I sound like a noob to everyone

Hey there BStorm59

You need to ask yourself those questions to know how to architecture your code:

:diamonds: What do you want to do?

  • You want to create some locators and place them
  • You want to create a joint for each locator, with the base and end of the joint on the locators positions

:diamonds: What do you know before launching the function?

  • You know how many joints you want to create,
  • You know their names,

So to structure your code, start from the data you have:

# names of all the joints
names  = ['C_Pelvis_jnt','l_leg_jnt','l_knee_jnt','l_ankle_jnt','l_toe_jnt','l_foot_ball_jnt']

:diamonds: From this, you know how many locators you need to create with len(names), so no need to set the number of locators to create in create_LOC()

create_LOC(amount = len(names))

:diamonds: You have an enumerate exemple in this previous topic (on the same code apparently)

In your case:

for i, name in enumerate( names[ :-1 ] ) : # iterate over all but the last item
    cmds.parentConstraint(Loc[ i ], names[ len(names) - i ])