Calculated Animation with Expression Editor
Maya 2012 Tutorial
How to Calculate and Set an Objects Rotation or Movement with Another Object's Motion in Maya
The Expression Editor is used to add scripting to your objects. The objects must be added to the workspace and named. The code is then added to the Expression Editor and the objects are connected mathematically.
In this example we will add two cylinders to the stage. The second cylinder is twice the radius of the first. As the first cylinder rotates, we want the second one to rotate at half the speed and in the opposite direction.
Add a polygon cylinder to the stage by choosing Create > Polygon Primitives > Cylinder. Make sure to include the options.
Set the following values: Radius = 1, Height = 2, Axis Divisions = 20, Height Divisions = 1, Cap Divisions = 1, then press the Create button.
Activate the Component mode by clicking the Component icon and click the Face icon. Press the 5 key to see your cylinder in shaded view.
Select one of the faces, then shift select so you select every other face on the top of the cylinder.
From the Polygon menu set, choose Mesh > Extrude.
Pull the arrow upward to extrude the selected faces slightly upward. We will now be able to see as the wheel rotates. Press the q key to exit the Extrude mode.
Click the Object mode icon (F8) to return to the Object mode.
Duplicate the object by choosing Edit > Duplicate (Cmd/d (PC - Ctrl/d)).
In the Top view, activate the Move tool and drag the second cylinder next to the first cylinder.
Select the first cylinder, open the Channel Box and open the initial entry in object's Construction History. In this example it was named polyCylinder1.
Change the Radius value to 0.5.
In the Top view, move the cylinders so they touch.
Select the cylinders, one at a time, and note the name for each cylinder in the Channel Box. In this example they are named pCylinder1 and pCylinder2. pCylinder1 is the smaller cylinder.
Deselect both objects and open the Expression Editor by choosing Window > Animation Editors > Expression Editor.
Choose By Expression Name from the Select Filter menu.
Enter a name of rotateObj for your expression.
Type the following code in the Expression text box:
pCylinder2.rotateY=-0.5*pCylinder1.rotateY. Press Create, then press the Close button.
Select the Rotate tool (e key) and rotate the small cylinder around it's y axis. The second cylinder rotates at half the speed of the first cylinder.