The best thing to do is approach it in a way that you’d approach writing any modular program… break it down into the steps that you’re going to be taking.
For example, first you need to figure out how you want to build the rig… do you want it based on the skeleton, or do you want to do it based on locators that define certain regions of the creature? The important thing is that you need a base to start with. So let’s take the locator method as an example…
First, you would load in the scene that has the geometry and create locators based on where you want things to be. Because you know that you’re going to be building a skeletal structure based on the locators, you want to ensure that the naming is consistent and correct. So what you would do is place locators where you want, for example, the up arm, elbow, and wrist to be.
So you name the locators l_up_arm_loc, l_elbow_loc, l_wrist_loc.
Then, you’re going to make a program that does the following:
- Determine whether or not you’re building a right or left sided arm (l_ or r_).
- Find the appropriate locators (l_up_arm_loc, l_elbow_loc, l_wrist_loc).
- Build a joint structure that moves through them.
- Build an ikHandle
- Create the control structure you want
- Set the correct attributes keyable/unkeyable/locked
- Hide things that you don’t need to see.
So start breaking this down… into mel. You know that you’re going to be making this a “procedure” or individual program that can be called from a larger program. And you also know that you need to pass it either a left or right side, so it knows what to do. So you would define the procedure and the variables that you’re going to pass it:
global proc buildIkArm (string $side)
{
// Take an argument ("l_" or "r_") and build
// an ik arm.
}
This is a simple procedure which that simply will take an argument and does nothign with it at this point… but it will do something eventually.
Now you want to add the second part… where it finds the locators that it needs. You can do that using the $side variable, and the fact that we KNOW that the things will be labled up_arm, elbow and wrist:
global proc buildIkArm (string $side)
{
// Take an argument ("l_" or "r_") and build
// an ik arm.
// get the names of the locators
$up_arm = ($side + "up_arm_loc");
$elbow = ($side + "elbow_loc");
$wrist = ($side + "wrist_loc");
}
now that we know those, we’re going to start building joints through them. To do that you need the location of those locators in space, and you need to know the joint command. To find the location in space of an object, you can either use the xform command (xform -q -worldSpace -rotatePivot) or use the getAttr command if the locators aren’t a child of anything else. I prefer the xform command 'cuz I know it’ll work…
global proc buildIkArm (string $side)
{
// Take an argument ("l_" or "r_") and build
// an ik arm.
// get the names of the locators
$up_arm = ($side + "up_arm_loc");
$elbow = ($side + "elbow_loc");
$wrist = ($side + "wrist_loc");
// find the world space of the locators, and
// create joints that are in the right place
$uap = `xform -q -ws -rp $up_arm`;
$ep = `xform -q -ws -rp $elbow`;
$wp = `xform -q -ws -rp $wrist`;
$up_arm_joint = ($side + "up_arm");
$elbow_joint = ($side + "elbow");
$wrist_joint = ($side + "wrist");
// now each of the pos variables are arrays.
// we can use them to create the joints.
select -d;
joint -p $uap[0] $uap[1] $uap[2] -n $up_arm_joint;
joint -p $ep[0] $ep[1] $ep[2] -n $elbow_joint;
joint -p $wp[0] $wp[1] $wp[2] -n $wrist_joint;
// now orient them correctly
joint -e -oj xyz $up_arm_joint;
joint -e -oj xyz $elbow_joint;
}
see how this is progressing? basically, you see what it is you need to do, then search the mel docs for the commands required. The next step was to make the ikHandle. Just guessing you can assume that it requires the ikHandle command… type
help -doc ikHandle
and you’ll find that there is example code to help you out.
global proc buildIkArm (string $side)
{
// Take an argument ("l_" or "r_") and build
// an ik arm.
// get the names of the locators
$up_arm = ($side + "up_arm_loc");
$elbow = ($side + "elbow_loc");
$wrist = ($side + "wrist_loc");
// find the world space of the locators, and
// create joints that are in the right place
$uap = `xform -q -ws -rp $up_arm`;
$ep = `xform -q -ws -rp $elbow`;
$wp = `xform -q -ws -rp $wrist`;
$up_arm_joint = ($side + "up_arm");
$elbow_joint = ($side + "elbow");
$wrist_joint = ($side + "wrist");
// now each of the pos variables are arrays.
// we can use them to create the joints.
select -d;
joint -p $uap[0] $uap[1] $uap[2] -n $up_arm_joint;
joint -p $ep[0] $ep[1] $ep[2] -n $elbow_joint;
joint -p $wp[0] $wp[1] $wp[2] -n $wrist_joint;
// now orient them correctly
joint -e -oj xyz $up_arm_joint;
joint -e -oj xyz $elbow_joint;
// add an ikHandle
$handleItems = `ikHandle -sj $up_arm_joint -ee $wrist_joint -n ($side + "_arm_ikHandle")`;
}
see how it works?
the best thing to do is look at the script editor as you’re trying stuff & see what commands get echoed… then read the docs on those commands… and discover how to use them to your advantage.
hope this helps some & gets you going on the right track!
-jason