In the previous movie, we created controls for our FK leg
and added the ability to control the length of the thigh and shin joints.
In this movie, we’ll start work on the IK leg.
Open the file “13-0-Character_Rigging_part13_start.mb”
or use your own file from the previous lesson.
Since we’ll be working in IK, set the IK/FK switch value to 1 to fully enable IK mode.
Hide the FK leg layer as well so as not to confuse us in the workspace...
Previously, we'd already added the geometry to the geo_LYR.
Now let's add the result joints to the result_skeleton_LYR
so we don’t accidentally select those either.
We're ready to create the basic IK handle for our leg.
Open the Skeleton > IK Handle Tool Settings.
We’re going to set the "Current Solver" to "ikRPSolver".
This stands for “Rotate Plane” solver,
and will allow us to create a Pole Vector to aim our knee later.
Click the IK thigh joint and then Ctrl + click the IK foot joint in the Outliner.
Maya creates an IK handle. If you use the Move Tool,
you can see that the IK leg chain moves along when you translate the handle.
Rename the handle “leftLeg_HDL” and the effector "leftLeg_EFF".
Let’s create a control curve for this to make it easier to select.
Starting from a NURBS circle, create a curve that outlines the base of the foot.
Make sure to set its pivot to the foot joint
and rename it “leftFoot_CTRL”.
Freeze its transformations, then add it to a new layer named “legs_IK_LYR”
colored yellow to match the other IK layers we have so far.
As a control, we definitely want to set its rotation order. In this case, ZXY.
This will allow the animator to rotate the foot around Y first, then bend it up and down.
Parent the IK handle under it.
Now you can use the control to move the entire leg.
However, notice that the foot remains straight and thus dips below the control.
This is because the IK handle only affects the leg as far as the foot.
Create a new IK handle from the foot joint to the ball joint,
and another from the ball joint to the toe joint.
Rename these “leftBall_HDL” and “leftToe_HDL” and their effectors accordingly.
Parent these IK handles under the foot control as well.
Now the foot stays flat as you move its control around.
But you can also control each part of the foot by translating their respective IK handles.
We'll create controls for those in an upcoming movie when we start working on our foot.
For now, we'd like our IK leg to be able to stretch just like our FK leg.
Your first thought might be to use the same “curve info” node method
we used for the torso and head rigs.
However, because we’re using IK handles instead of an IK spline, there is no curve!
We're going to need to use a different trick to get the length of the leg.
We can do this using the Distance Tool, which measures the distance between two locators.
To create one, go to the Create > Measure Tools > Distance Tool command.
Hold the ‘V’ key and click-drag to point snap the first locator to your thigh joint.
Point snap the second locator to the foot joint.
Maya creates two locators as well as a “distanceDimension” node
that displays the distance between them.
Rename the nodes “leftLeg_IK_lengthStart_LOC”, “leftLeg_IK_lengthEnd_LOC”,
and “leftLeg_IK_length” respectively.
You’ll also need to parent the end locator under the leftFoot_CTRL
so it follows the leg as you move it.
Now, unlike the spine and head rigs, in which we scaled each joint based on their length value,
here there’s an additional complication.
If we simply scale the leg alongside the leftLeg_IK_length measurement,
the leg will never bend, it will always stretch or squash.
What we need to do is set a driven key
such that when the distance between our start and end locators
is lower than the leg's maximum length, the leg will bend…
…but when it’s longer, the leg will stretch.
To accomplish this, we’ll set one driven key when the leg is perfectly straight,
representing the maximum “regular” length of the thigh and shin.
Then set another driven key when the leg and shin are say, two times their normal length.
Then we’ll just use the "Post Infinity" command in the Graph Editor to extrapolate that forever,
just like we did with the FK joint lengths.
Now unfortunately, a weakness of IK is going to get in the way of this.
Positioning the IK handle at just the right spot
when the length of the leg is at its exact maximum value can be quite tricky.
Luckily, what we can’t do in the workspace, we can do using MEL commands.
Open the Script Editor. Rather than using the Set Driven Key window,
we’re going to use the Set Driven Key command.
But first we’ll make our job a little easier by storing some variables.
First, let’s set our driver value, which is the measured distance between the thigh and foot.
Since the distance value is actually on the distance node’s shape,
we’ll assign it to a variable using that shape node.
Next, create a variable named “$leftThighLength” and assign it the length of the thigh,
which as you remember is the "Translate X" value of the shin joint.
Likewise, create a variable named “$leftShinLength”
and assign it the length of the shin by using the "Translate X" value of the foot joint.
We’ll also create a variable for the sum of their lengths named “$sumLength”.
$sumLength now represents the farthest the leg can possibly extend without stretching.
With those variables stored,
we’ll start setting our driven keys using the “setDrivenKeyframe” command.
The first key we want to set is at the leg’s fully extended position.
Add the –currentDriver flag set to our $driver (the distance value).
Then add the –driverValue flag set to $sumLength so this $driver value
is equal to the leg’s maximum extension when the leg is totally straight.
Add the –attribute and –value flags to set the outcome value of this connection,
which is as we remember, is the “Translate X” value of the shin.
Lastly, we must identify we're setting a keyframe on the leftShin_IK_JNT.
Click Execute All to run the script.
If you check the Translate X value of the shin,
you should see it’s highlighted red to indicate there’s a key on it now.
With the maximum length set, let’s next focus on the two times length.
Make a copy of your current script,
then multiply all the length values by two, and run the script again.
Make sure to encapsulate these new values in brackets or the script will break.
Now if you pull on the leftFoot_CTRL, the thigh stretches.
Make a copy of both setDrivenKeyFrame commands, replacing "thigh" with "shin",
and "shin" with "foot".
Now if you pull the leg IK handle,
the whole leg stretches up to two times its normal length and then stops.
Just like the FK leg, we can extrapolate this movement to infinity
by selecting both the shin and foot joints and opening the Graph Editor.
Set the animation curves to "Linear", as well as the "Post Infinity" as "Linear".
Now you can stretch the leg infinitely!
Hide the leg's IK handles along with all the nodes related to the leg length measurement.
You may notice as you manipulate your rig that while we can move and rotate the foot,
the knee doesn’t always follow it the way we want.
In the next movie, we’ll start work on a control for our knee.