Accessibility
navigation | page content |
Accessibility
top of site | navigation |
Latest Tutorials
Tutorials

Scripting with Maya

Aladdin Ayesh highlights the basics of animating your scene and how scripts can simplify the process.
Scripting in Maya

Animation is possibly the most exciting element of using Maya. Scientists love to animate their results to prove their ideas; architects bring life to their designs through animation, and students can impress with animated projects. In this article, we look at the basic animation functions available in Maya. Advanced utilities will also be mentioned briefly for animators who would like to expand their skills.

Frames and keyframing
The basic concept of animation and filmmaking is the concept of ‘frame’. A frame is a still picture or graphic that presents a scene at a particular point of time. A series of frames over a sequence of time points create an animated scene. Changes between these frames determine whether the animated scene is still, animated, or a slide show. If there’s no change between the frames, in other words; it’s the same frame over different time points, the scene will appear still. If there are major changes between every frame and the next, the animation is more a slide show rather than an animation. The combination of steady object changes within a stable and slow-changing background within the same context, or scene, creates an animated scene. The changes between keyframes are usually considered substantial, apparent or greater than those changes that occur in-between frames.

Timeline and animation clip
At the bottom of the screen, you may notice what looks like a ruler. This is in fact a timeline. We use this timeline to set up the key frames.

To begin creating animation, start a new scene. Draw a basic shape, such as a ball. Now click ‘S’, this will set the current view as a key frame. Click on the move tool from the left-hand side toolbox. Select another position on the time frame and make it number ‘8’. Move the object to a different position on the canvas and press ‘S’. Select another position on the timeline and move the object again and press ‘S’. Once you have moved the object to a few places across the timeline, click on forward play to see the animation.

Path animation
We can create animation using key frames and the repositioning of the object during these frames, but you’ll often need to tune your animation, and in this case, to produce the right motional effects. Alternatively, we can use path animation to speed up the process. Let us assume our ball is part of a scene, such as the Moon going around the Earth or a tennis ball on a court. There is clearly a set path for this object and tuning the animation to follow this path manually is difficult. Instead, we can use a curve to specify the path. Then we click on Animate | Motion Paths | Attach to Motion Path | ’’. We select ‘start/end’ option and set up the number of frames from the beginning of the animation to the end.

Character set-up
We can only cover the basics here, but the possibilities are endless. If we follow the tennis ball scene to its logical conclusion, we’ll want to add two people to play on our court. First, we need to construct their skeletons. We do that by selecting ‘joints’, from the ‘Animation’ tab menu or ‘Skeleton’ and select the ‘Joint’ tool from the main menu. When we click on the scene a joint will be formed. By clicking again, we create a second joint with a link between the two. To create a part of a character, for example an arm, we decide how many joints there are. It’s very much like drawing on paper using the circle technique. If we are drawing an arm there are about three joints: shoulder-end, elbow, and hand-end joints. By clicking on the joint tool again or on the pointer, the links between joints will stop, so we can start a new part of character set-up. Once we’ve created the skeleton we can dress it with material in the same way we’ve done previously in the series; we can use the ‘Skin’ menu tools.

Constraints
You may want to constrain the movement of joints or have animated parts follow certain rules. For example, you may have seen a cartoon where a character’s eyes follow a moving object, such as our tennis ball. We can create a similar effect by using a particular type of constraint called an ‘Aim’ constraint. Under the ‘Constraints’ menu, you will see a selection of constraint types to try out.

Scripting
Maya provides greater flexibility through script programming. There are two types: ‘Maya Embedded Language’ (MEL) and ‘Expressions’. You can use the Expressions editor to change object attributes during animation, while MEL is a full scripting language that will enable you to automate processes and effectively extend the standard Maya menus. Start a new scene and click Window | General Editors | Script Editor. In the scripting menu try typing the following:

 sphere; 
This simple command will draw a sphere on the scene. Now try the following:
 move -relative 3 0 0; 
Finally to remove the sphere, type:
delete nurbsSphere1;
Notice ‘nurbsSphere1’ is the name of the sphere object we have created. If we keep the script editor open while we are working on our scenes, we can see every action we take visually on the scene being recorded as a MEL command.

Creating shelf components
It’s often the case that we want a set of steps to be accessible through a single button, like macros in word processors and spreadsheets. MEL enables us to do that in a very simple way. To create your own buttons on the Maya shelf menu, decide on the functions you want to automate and then start a new scene. Start the script editor. Perform these functions using Maya tools. Here is an example of creating a sphere and giving it a material link. As you click on the sphere and as you add it to the scene the script editor records that. Something like the following will show on your script editor:
 CreateNURBSSphere; 
sphere -p 0 0 0 -ax 0 1 0 -ssw 0 -esw 180 -r 6
-d 3 -ut 0 -tol 0.01 -s 30 -nsp 30 -ch 1;
objectMoveCommand;
connectAttr -f blinn3.outColor blinn3SG.surfaceShader;
Click on the ‘Custom’ section in the shelf menus. These are tab menus that usually appear directly above the scene and contain icons. You will notice that the ‘Custom’ section is empty. Now, click on the script editor and select the lines you want to record in your automated button. From the ‘File’ menu on your script editor select ‘Save Selected to Shelf’. A dialogue box will open requesting a name for the script. To manage our automated buttons, click Window | Settings/Preferences | Shelves. We can delete the scripts we do not like, change their orders and much more.

Working with Expressions
Let us create a NURBS cone in the same way we did previously using the tool bars. While the cone is selected, click Window | Animation Editors | Expression Editor. A new window will open with the cone attributes and expression editor. In the editor write:
 cone.scaleX = time * 2 
This expression will control the scaling of the cone along the X-axis. Finally, let’s create a second object, for example NURBS Sphere. We will control the attributes of the two objects based on the following conditions:
if (time < 3) {cone.scaleY = time; cone.scaleY=time;};
if (time < 3) sphere.scaleX = time;
Notice how we’ve changed two attributes of the cone within one conditional statement using the curly brackets: ‘{}’.
Aladdin Ayesh  
  PC Plus Issue 223 - December 2004