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.
Quick modeling
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.

It's a bird!
Move the pivot points of both wings so that they can rotate around the proper axis.

Proper pivoting...
Parent the wings to the main body so that they move with it.

...and parenting
The Expression Editor
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).

The not-so-daunting Expression Window
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.

Your first 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.

A transform node controlled by an expression
The Sine function
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.

Finding lost expressions
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.

The wing flaps. Wow.
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.

You're going the wrong way
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.

That's more like it
Controlling the wing speed
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.

Creating a new attribute
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.

The new wingSpeed attribute
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;
Conclusion
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.

Final expression
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.
26 Comments
Pl. read this excellent tuts
cool!
pls read dis nice stuff..
Thank you so much, you saved my butt! Had a project that I needed this script for! Thanks again.
thanx!
this tutorial is cool! thanks:) hope i submit more.. like particle expressions.
i mean hope u submit more tutorials.
god luck. thanks again
how did you change the user interface colors? Are you able to tell me step by step, I am new to Maya, I am learning on Maya 8.5 Personal Learning Edition. Thank you.
I tried to creat an expression following the example above but when i click create the expression text disappears and no expression is created can someone help im a maya newb
its awesome tut
nice tutorial;
Really really nice, thanks a lot!
hey buddy i know this 1 i wnt litle advan……….than uuuuuuu
Great tutorial – just what I needed – thanks
Excellent tutorial! I was hoping to get my mind around expressions!
I am curious however about learning how and why the Sine function works. What inputs it requires, how to use it; etc.
cool
thanks a lot – very helpful.
its very helpful, thanks
very good thanks for your tut
It seems to flap down and then across one frame jump back to the top instead of flapping up again
Brilliant! I wish I had payed more attention in math. Your tutorial is quick and to the point! A+++
@robc – increase the playback range.. The default is 24 Frames which isnt enough time for the expression to make a full run.
thanks a lot, its a very helpful tut…may your next tut explain some more mathematical functions detail….& will you can help me in understanding how all the utilities works….
nice
Nice tutorial
Thank you so muchhh….
Can you please explain what is the difference between the data type vector and float?