Sunday, March 7, 2010

Creating and Baking Expressions

In this week's Maya Tip, we'll work through creating expressions and using the motion data to create anim curves that we can edit in the Graph Editor. Expressions are custom algorithms that you can write in a modified version of MEL script that can be linked to almost any node in Maya. This might sound complicated but in reality it can be as simple as connecting one value to another by writing a simple expression like ty = frame; On the left of this expression, you see the attribute that is being modified and on the right you see the value that is being assigned to the attribute on the left. Maya uses a reserved variable called frame to store the current frame number.

Start a new Maya scene and throw a NURBS sphere on the stage. Select its Translate Y attribute in the channelBox.  Navigate to the Edit menu in the channelBox and select the Expressions... item.

This opens up the Expression Editor. You should now have nurbsSphere1.translateY as your Selected Object and Attribute. In the big text box below (labeled Expression) is where we can type in the following: ty = sin(frame); Press the create button. You have now attached an expression node to your ty of your sphere. The connection is indicated by the purple tint in the channelBox.

Go ahead and play your animation and you will see your sphere bobbing up and down. It is a bit fast, so let's slow it down by decreasing the frequency of our sine wave. If you closed the Expression Editor, open it back up by going to Window:Animation Editors:Expression Editor. Make sure the sphere is selected then Select By Expression Name from the Select Filter menu. This will allow you to retrieve any expressions you created on your sphere (or any other object.)

Our current expression should be named expression1. Click it and you should see that Maya expanded our code to read: nurbsSphere1.translateY = sin(frame); Enjoy the fact that Maya allows you to write shorthand in the Expression Editor! Divide the frame number by some number to reduce the frequency. I divided it by 6: nurbsSphere1.translateY = sin(frame/6); Click Edit to commit the changes and then play the animation.

Just for fun, link another attribute in the same script window by writing this code on the next line: nurbsSphere1.rotateZ = cos(frame/6); Click on Edit again to commit the changes.

Play the animation again. Notice that the sphere barely moves in rz. This is because Cosine returns a value from -1 to 1 and these angle values are very tough to detect when the ty motion is in effect. This means we need to increase the amplitude a bit. Do this by multiplying the trig function by the angle range you would like to see in motion. I multiplied mine by 10: nurbsSphere1.rotateZ = cos(frame/6)*10;

If you know some MEL, you can really go crazy in the Expression Editor. But for now, let's bake this simulation so we can edit the curves in the GE. Select the sphere and visit Edit:Keys:Bake Simulation. Open your GE because it is time for some cleanup. Luckily, the GE has a function that can do most of it for you. If you select the ty anim curve, you'll notice there's a key on every frame. There are ways to have the animation bake with less keys but let us continue with this example so we can use another useful tool.

Select the entire ty anim curve in the GE and navigate to Curves:Simplify Curve. You will find that the function approximated your baked data using less keys. Now you are at complete freedom to edit this curve any way you please.

I hope you found this tutorial useful.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.