[Maya MEL] How do I constraint joints and controls automatically through name?


#1

Hi,

I am trying to constraint several joints to its corresponding controllers but automatically through name.
I was thinking of putting all joints in List/Array A while all controls in List/Array B.

The problem is I don’t know how match them up.
So basically,
match head_jnt
to head_grp

and perform parentConstraint.

Apologies if this has been asked before but I can’t see any relevant sources.

Thank you for looking at my problem.


#2

if the node names match except for the suffix, you do not need to make 2 separate lists. You can just build a list of joints, and build the control names in a for loop.

//untested...
string $joints[]; //empty array to hold all joints
string $joint_name; //empty variable to hold current joint
string $prefix; //empty variable to hold current joint prefix (without "_jnt")
string $control_name; //empty variable to hold current group

$joints =`ls "*_jnt"`; //populate the list of all joints

// now loop through the list of joints
for  ($joint_name in $joints) {
	$prefix =  `substitute "_jnt"  $joint_name ""`;  //strip "_jnt" from joint name
	$control_name =  $prefix+"_grp"; // add the control suffix
	parentConstraint  $control_name $joint_name;
}

That said, almost nobody does such things in MEL these days.
Python & pymel is almost always a simpler choice.

# also untested...
import pymel.core as pm
for joint in pm.ls("*_jnt"):
    ctrl_name=joint.name().replace("_jnt","_grp")
    ctrl=pm.PyNode(ctrl_name)
    pm.parentConstraint(ctrl,joint)

#3

We were talking about this in Slack. Maya LT only supports MEL. No Python or SDK. So that is one valid reason someone might choose MEL. If you are not limited to LT, then you should really not be using MEL in this day and age.


#4

Thanks for the reply @Mambo4 and @clesage

It works as expected. I just modified some lines to make it work.

From: $joints =ls "*_jnt" to $joints =ls “*_jnt”`

and

From: for $joint_name in $joints to for ($joint_name in $joints)

Thanks again!