# Setting up animation controls in Maya

In this tutorial you will learn:

1. How to use the Bend Deformer.
2. How to make one attribute depend on another.
3. How to use math in expressions to create animation.

## Scenario

A plank is hit by a stone and starts to shake from the impact. The plank is stuck at the ground and gets hit at the top so the only thing that actually swings is the top while the bottom becomes the pivot for the
rotation.

1 - First you create the scene. In my case I created a plank from a pCube and a stone from a pSphere.
Rename the plank to pPlank and the stone to pStone. When working with expressions naming is very important. The objects doesn't matter really, but for the scenario stated above it does and I'll use it as an example further throughout the tutorial

2 - We want the plank to stay stuck at the bottom. A regular rotate would rotate the plank in a linear movement. Add a bend-deformer to the plank by selecting it and clicking on "Deform - Create Nonlinear - Bend" This is under the animation-menu. Click "F2" to bring it up. Rename it plankBendDeformer.
After you have applied the deformer you should fool around with it to check out what the attributes do.

Envelope: decides how much the plank will be affected. 0 = none, 1 = max. Curvature: The actual bendingValue of the deformer

Low Bound: Define how much of the mesh that shall bend above the deformers pivot. High Bound: Define how much of the mesh that shall bend above the deformers pivot

Keep Envelope at 1, don't worry about the curvature, set Low Bound to -1.5 and High Bound to 0. You can move the deformer a bit down in the Y-axis to get it to bend more of the geometry. Play with it to learn how it works. Always do this when using new stuff.

3 - Create a nurbsCircle and place it half way up the plank. This is our animationControl. Rename it plankControl.

Since we shall not manipulate the plank, it doesn't need any attributes in the channelControl. Nor does the
plankControl. To get rid of the attributes go to "Window - General Editors - Channel Control". Select all its keyable controls except visibility and click move. Do this with the pPlank and the plankControl.

Ball Material: The ball, in contrast to the wall will be shiny. As a highly polished object, it will be used to study the effects of bouncing light. The ball will be a dark grey object with a Diffuse Value of 0.2 (with Hue & Saturation set to 0.0). To give the ball slightly softened highlights, Glossy is set to 1.0. The rest of the reflections will come from the Specular with a setting of 0.7. Shiny is left at 50, just to blur the highlights a little.

Now you want to add an attribute to the plankControl. This is the value that will determine the vibration. Select plankControl and go to "Modify > Add Attribute"

Attribute Name: vibrateValue (Note: Maya changes this in the channelBox for readability).

Data type: float. If you don't know what a float is look it up in maya's helpfile and make sure you read about the other types too.

Minimum: 0 , Maximum 1 and Default 0 ( I'll explain why in a second).

f(x) = sin(x). Sounds familiar? Good. If not, let's take a minute to explain the sine function. Sine, Cosine and
T angent are used much in trigonometry. The unitcircle is a very good tool when it comes to angles and these
mathematical functions. Let's take a look at the unitcircle.

Looks confusing? Well, let's strip it down. Notice the triangle at 30 degrees.We use sine to determine the value of the opposite line. (.sA) and cosine to determine the nearby line.
(.cA) sin30 = 0.5, cos30 = 0.866.

Look at the two axis in the circle. You have X from "-1" to "1" and Y from "-1" to "1". The sine value is read from the Y-axis (see dotted line) I'm going to take a trip all the way around the circle and list the values I get:

The values in paranthesis are the distance traveled on the circle. You don't have to understand this to get the point at the end.

If we take a look at this graph we see that the sinecurve is a neverending curve with the same pattern all over. If we go two rounds around the unitcircle (720 degrees) we would end up at the y-value 0 and 4 pi.
How is this usefull for us? If you are an animator you have already told yourself that this graph represents a repeating wobbleanimation or it could be an elevator with smooth translateY etc. etc.

We want the curvature on the plankBendDeformer to go from "-1" to "1". The sinecurve is perfect for this so therefore we write an expression where the value of the attribute "curvature" is controlled by a sinecurve

3 - Let's try sine out. Grab the pStone and right-holdclick the translate Y attribute. Go to expressions... and set select filter to by expression name.

Call our new expression "stoneTranslateYSine" and press enter. Now write this into the expression - textfield.

pStone.translateY = sin(90); and click Create.

Maya returns the rather odd result 0.894 (read the translateY attribute, now marked purple/lilac because an expression is linked to it ) I'm not quite sure why this is, but I recon maya reads radians since sin(deg_to_rad(90)); returns 1

Fear not, we can use sind (sinusdegrees). sind(90) will return "1".

If we use sind(frame) instead of a set value maya will fill the current frame in. e.g. at frame 10; sin(10), at frame 90; sin(90) Try this one out and hit play.

pStone.translateY = sin(frame);

If you have the timerange set from 0 to 90 you can see that it takes 90 frames to get the translateY to "1" If you've been paying attention you should be able to guess this result before you saw it in Maya. We are getting somewhere now.

We want a higher frequence. Taking 90 frames to get half a sinecurve isn't quite usable. I tried a couple of values and this is what I am looking for:

pStone.translateY = sin(frame*56):

It's rather quick, but I'll use the vibrateValue to ease it in and out later. Now I want you do one last thing with the stone before we go to the plankBendDeformer.Go to "Edit - Keys - Bake Simulation/optionBox".

Reset the settings (Edit - Reset Settings) and change Channels to "From Channel Box". Set Time Range to "Start/End" and set Start Time to 0 and End Time to 90. Select pStone.translateY in the channelBox and hit Bake.

Maya will now bake the animation by reading the value at each frame and set a key. Let her finish and open
the graphEditor (Window - Animation Editors - Graph Editor...) And... We're in sine-land:

Now we're going to do what we intended to do in the first place. I just wanted you to fully understand the "sind- command". Rightclick-hold pStone.translateY and choose "Break Connections" . Now we deleted the keys. If we hadn't baked it we would have broken the expression by doing this. Set translateY to "0" and leave the stone alone.

We're going to use what we have just learned to create the wobbleEffect for our plank. Let's directly trasfer the lines of code we found usefull earlier. Select plankBend and click plankBendDeformer under INPUTS. Rightclick-hold Curvature and release over Expressions. Now write this into the expression-textField:

plankBendDeformer.curvature = sind(frame*58);

By looking at the line above you should be able to imagine the plank go crazy. Click create to find out that you're absolutely right :) "Ahrg, it's crazy, we have to stop it!!". No problem.

plankBendDeformer.curvature = (sind(frame*58))*(plankControl.vibrateValue);

Let's try to animate it then. Grab plankControl and set a key at frame 0, vibrateValue "0", a key at frame 10, vibrateValue "1" and at frame 57 set vibrateValue back to "0".

Now it starts at impact and eases out over about 40 frames... I had to tweek my expression to get it to look better. Remember that you can edit expressions to fit your need. Here's what I got:

plankBendDeformer.curvature = (sind(frame*78))*(plankControl.vibrateValue/3);

Now, all you have to do is animate the stone and tweek the vibrateValue to match the animation.

Here's the relationship between the vibrateValue and the curvature after modification of the expression:

Feel free to download the file here and play around with it (Maya 7.0):

Tip: It's smart to get your way around the Maya help files. Look up words like "frame", "time", "noise", "rand", "sin", "cos" "radians" "degrees" "if-else" and the list goes on. It's important to get familiar with commands so you know what to use when you're in a situation where you consider expressions. And if you use commands you've never used before by reading a tutorial or if someone on MSN tells you to, always look it up.

I did not write this tutorial because I mean this is the best way to do this task. I wrote it to show you that possibilities with expressions are endless. Good luck creating nifty expressions