Preface to Squash and Stretch Part 2

We locate the variable $distance equal to the production of our distance node. It represents the coldness from the shoulder to LtArm.

The worth of $initialDis in my scene was 5.72655. Yours determination be different. It should be what the initial return value of the distanceDimension node is when the arm is in the locked position. You’ll see why this number in significant in a minute.

We’re departing to use the variable $factor to level the joints in the arm for the stretch effect. Distance divided by initial distance give us the value we need. at what time the arm is in the locked position, distance over initial distance = 1. That number gets better and smaller as you move LtArm in and out, giving us the appropriate scalar worth to use for the stretch effect.

Ok, we include our variables. at the present let’s use them. The next lines of your expression should be:

LtShoulder.scaleX = $factor ;

LtElbow.scaleX = $factor ;

Click “create” to create the expression. at the present when you move LtArm around the scene, the arm should stretch to meet it:

Introduction to Squash and Stretch 6

Though, you’ll notice a potential problem as fine. The arm never bends anymore. It just gets shorter plus shorter as the control approaches the shoulder:

Introduction to Squash and Stretch 7

Though there are times at what time you might want this effect, the majority of the time you’re going to want the arm to bend. So how do we fix it?

We require a clamp. The shortening occurs when $factor < 1 and the scale gets multiplied by a fraction. So we need to clamp the value.

LtShoulder.scaleX = clamp(1,10,$factor) ;

LtElbow.scaleX = clamp(1,10,$factor) ;

At the present the arm should bend when the control gets close and stretch when it’s far away:

Introduction to Squash and Stretch 8

The stretch result works, and it’s pretty cool so far, other than there’s more we can do with it. It might be useful to have an envelope attribute that controls the amount of stretchiness.

We’ll make by adding a custom attribute to LtArm. (Right click in the Channel Box plus select “Add Attribute”). Call it “Stretch.” Make sure it is set to “float” data type. We be supposed to also set min=0, max=1, and default=1.

(UPDATE: In newer versions of Maya, you can’t just right click in the channel box to “Add Attribute.” For whatever basis, that menu option is now just accessible from the “Edit” drop-down box.)

Now we require a little bit of math to figure out how to write our expression. The easiest way to do it is to believe of the control as a blender between two options. at what time LtArm.Stretch = 1, the arm stretches to wherever the control object is. When LtArm.Stretch = 0, it doesn’t stretch at all. It behaves as a normal IK arm would. Values between 0 and 1 should interpolate.

To do this, we’re leaving to have to modify our expression to have two different terms.

If we required blending between terms P and Q, where x is the blender, our look would look like this:

y = (Q *(1-x)) + (P * x)

In so as to equation, if x=0, then y=Q. And if x=1, y=P. Plug the numbers in and see for yourself.

In our expression, P = clamp (1,10,$factor) and Q = 1. (Q = 1 because when the stretchiness is off, we want LtShoulder.scaleX and LtElbow.scaleX to equal 1).

X needs to equal our custom attribute, so let’s declare a latest variable.

$envelope = LtArm.Stretch ;

So, by our equation as a guide, plus plugging in the terms, our expression comes out like this:

LtShoulder.scaleX = 1*(1-$envelope) + (clamp(1,10,$factor)*$envelope) ;

plus of course we be able to remove the “1*” from the first term to simplify to:

LtShoulder.scaleX = (1-$envelope) + (clamp(1,10,$factor)*$envelope) ;

Phew! I hope that was clear enough for everyone to follow along. Create the same amendment to LtElbow.scaleX plus you should have a working control:

LtArm.Stretch = 1

Introduction to Squash and Stretch 06
LtArm.Stretch = 0

Introduction to Squash and Stretch 04

One previous task: There might be occasions at what time the animator wants to use so as to arm shortening effect that we had earlier. Let’s make a switch to go back and forth between arms that bends and an arm that shortens.
Create another custom attribute on LtArm. This time call it ShortArm, and make it a “boolean” data type.
Now in the expression, we require to declare another variable.
$switch = (1-LtArm.ShortArm) ;

So when LtArm.ShortArm is off, $switch = 1, and at what time LtArm.ShortArm is on, $switch = 0.
Now, modify the clamp portion of the expression to convert:
clamp($switch,10,$factor)

Chaging LtArm.ShortArm modifies the clamp function that we required earlier to remove the shortening. Now instead of removing it entirely, you can turn it on plus off.
LtArm.ShortArm = on

Preface to Squash and Stretch 07

LtArm.ShortArm = off

Introduction to Squash and Stretch 03

That’s it!

You be able to download the last control here: squash and stretch


I hope so as to you enjoyed the lesson. Feel free to email me by means of any questions or corrections. Or make sure out my weblog: Set Driven Key, for more tutorials and tips.