|

|
|
Once you figure out the basics of expressions
in Maya, you'll find yourself using them all the time, avoiding
manual keyframing as much as possible. Expressions are, in my
opinion, Maya's strongest feature. In order to explain the basic
idea of using them, this tutorial will show how to create a very
simple animation: a bug flapping its wings with a controllable
speed attribute. The model is very sketchy, since it's not the
focus of this tutorial.
|
| 
|
|
Create three Nurbs spheres,
and move and scale them so that you get a very rough representation
of a body with a couple of wings. Name the wings "rWing" and "lWing",
and name the body "bug" or whatever.
Throw a basic texture on
them if you wish.
|
| 
|
|
Move the pivot points of both wings so that
they can rotate around the proper axis. |
| 
|
|
Parent the wings to the main body so that
they move with it. |
 |
|
Open the Expression Editor
(Window > Animation Editors > Expression Editor...). You'll
find it useful to assign a hotkey to that (ALT+e is unassigned
by default and is a good choice).
On the left you'll see your currently selected object,
and on the right the list of its keyable attributes.
Expression are typed into the bottom box. It's important
to know that expressions do NOT relate to the currently selected
object. You can write expressions for anything, another object,
a shader, whatever you want. If have an object selected, though,
it's easier to know the exact name of an attribute you want to
control, since all keyable attributes at listed on the right.
|
| 

|
|
Type "wingFlap" into the
Expression Name box, and type the following into the Expression
box at the bottom of the dialog box:
lWing.rotateX = time * 10;
Click "Create". This creates the expression.
The syntax is very simple: you type the object name
(or, rather, the node name), followed by a period and the attribute
you want to control. In this case, we want to control the X rotation
of the left wing's transform node. The "time" attribute is a built-in
value in Maya, which returns the current time of the animation
in seconds. Its multiplied by ten so that the result is more immediately
visible (the flapping is faster). The semicolon simply marks the
end of every statement in Maya. Click "Play", and the you'll see
the wing rotate as time goes by. Check the Hypergraph, and you'll
see lWing controlled by the wingFlap expression. Also, in the
Channel Box, lWing's rotateX attribute turns orange to show that
it's controlled by something and that you can't change it directly.
|
 |
|
The simplest way to create
a back-and-forth motion is to use a sine function. Modify the
expression so that it looks like this:
lWing.rotateX = sin (time * 10) * 40;
If the expression has disappeared from the Expression
Editor, simply click Select Filter > By Expression Name and
click on wingFlap.
The result of the sine function is multiplied by forty
so that the motion describes a larger arc. Otherwise, the flapping
goes back and both between two very small values.
Click "Edit". This button, which replaced the "Create"
button, updates the expression to reflect the changes you've made.
If you get the message "Error: Expression invalid after edit",
check your syntax. If you play the animation after this error
without correcting it, Maya evaluates the last valid expression
that you entered.
|
| 
|
|
Play the animation. The wing flaps back
and forth. |
 |
|
To get both wings to flap,
copy and paste the expression into the line below, but change
lWing to rWing, so that the whole expression now reads:
lWing.rotateX = sin (time * 10) * 40;
rWing.rotateX = sin (time * 10) * 40;
You'll see a motion which isn't at the cutting-edge
of aerodynamics. This is because the X rotation of the opposite
wing should be reversed.
|
| 
|
|
Add a minus sign before the
sine function of the right wing, to read:
lWing.rotateX = sin (time * 10) * 40;
rWing.rotateX = -sin (time * 10) * 40;
Play the animation. The bug now has more chances of
survival. |
| 
|
|
Now suppose you want to control
the wings' flapping speed. Currently, the speed is determined
by the *10 part of the expression. Let's create an attribute for
wing speed that would allow us to change the speed dynamically,
and even keyframe it. In the Attribute Editor, select the bug's
transform node (the one simply called "bug"), and choose Attributes
> Add Attributes.
Type WingSpeed into the Attribute Name field, make
sure Float is selected, and set 0 as Minimum value, 10 as Maximum
and 5 as the Default value. Make sure that "Make Attribute Keyable"
is checked (so that is shows up in the Channel Box), and click
on OK.
The Add button allows you to add several attributes
without closing the window every time. |
| 
|
|
In the Extra Attributes folder of the bug's
transform node, you now have a fresh new attribute called wingspeed,
nicely displayed with a space (Wing Speed) because of the capital
S. It's set at the default of 5, and doesn't do anything at the
moment. |
 |
|
Back in the Expression Editor,
change the 10 value to bug.WingSpeed in both lines. They should
now read:
lWing.rotateX = sin (time * bug.WingSpeed) *
40;
rWing.rotateX = -sin (time * bug.WingSpeed) * 40;
Notice a couple of things: the capitalization (it's
very important to type the attribute name exactly as you defined
it) and the fact that we're controlling the wings via an attribute
which belongs to the bug node, hence the bug.WingSpeed. Move the
Wing Speed slider around and see how it affects the wings' motion.
|
| |
|
Play around with this in
order to get a feel for the almost infinite possibilities it offers.
Try to control the size of the wing arc (currently 40) with another,
new attribute. Also, you can attach the bug node to a turbulence
field, and try to control the wing speed via, for example, the
translateY attribute of the bug, so that its wings flap more quickly
when it approach the ground. Bear in mind that you've still haven't
set a single keyframe. Enjoy.
Please report any omissions, errors or steps
that are hard to understand (use the Contact section). |