Expressions are MEL scripts that are executed on a per frame basis. That means that any parameter with an expression assigned to it will execute that expression for each frame in a sequence. Using expressions lets you animate objects easily and realistically using a few simple commands. About the Expression Editor You'll use the Expression Editor to create and edit expressions (Figure 17.31). It is divided into several areas. In the bottom area, scripts are displayed and edited; above, objects, expressions, or script nodes are displayed. Figure 17.31. You can you create and edit scripts and expressions in the Expression Editor. When you select an expression or script job in the selection area, the statements are displayed in the lower section. When an object is selected, you must also select the attribute that the expression is assigned to in order for it to be displayed in the bottom area. The following task includes a simple expression that uses the sin function to move a sphere up and down. sin is short for sine as in sinewave. sin creates an oscillating value based on the input. The input has to be incremented at every frame in order for it to animate. If sin has the same value at every frame, there would be no animation, so you need to use the time node. The time node is easily accessible to expressions and not to MEL scripts executed from the Script Editor or elsewhere. Another unique attribute of expressions is that you use an attribute of an object or node like you would with a variable. This is not only convenient but is faster to execute. Adding expressions like this can create simulated movement for butterflies, blinking eyes, or heaving chests. To create an expression: 1. | Create a sphere and press to open its Attribute Editor (Figure 17.32). Figure 17.32. Press to open the sphere's Attribute Editor. | 2. | In the Transform Attributes section, type =sin(time) in the middle Translate field and press (Figure 17.33). Figure 17.33. Click in the middle Translate field and type =sin(time). The field turns a violet color, indicating that it is controlled by an expression (Figure 17.34). Figure 17.34. The field turns violet, indicating that it is controlled by an expression. | 3. | Press Play . The sphere moves up and down quickly (Figure 17.35). Figure 17.35. The sphere moves up and down quickly. | 4. | Right-click the violet-colored field and select Edit Expression from the pull-down menu that appears (Figure 17.36). Figure 17.36. Right-click the violet colored field and select Edit Expression from the menu that appears. The Expression Editor opens with the object and its assigned attribute already selected (Figure 17.37). Figure 17.37. The Expression Editor opens with the object and the attribute already selected. | 5. | To speed up the movement of the sphere, edit the line of script as follows: pSphere1.translateY=sin(time*10); | 6. | Click Edit, then press Play on the timeline. Multiplying the value of the time node by 10 will cause the object to move much faster. | To gain more control over an expression, you may wish to keyframe the values that you input into them. To do so, you need to add an attribute to the object. You can then use the value of the attribute in the expression instead of hardcoding the value into the expression. To add an attribute: 1. | Select the sphere from the previous task, "To create an expression." | 2. | From the Modify menu, select Add Attribute to open the Add Attribute dialog box (Figure 17.38). Figure 17.38. Select Add Attribute from the Modify menu to open the Add Attribute dialog box. | 3. | In the Attribute Name field, type Height (Figure 17.39). Figure 17.39. Type Height into the Attribute Name field. | 4. | For Data Type, click Float (Figure 17.40). Figure 17.40. For Data Type, choose Float. | 5. | Click OK to add the Attribute to the sphere. In the Channel Box, the attribute's Height appears (Figure 17.41). Figure 17.41. The new Height attribute appears in the Channel Box. | 6. | In the Channel Box, right-click Translate Y and select Expressions from the pull-down menu (Figure 17.42). Figure 17.42. Select Translate Y in the Channel Box; then right-click and select Expressions from the menu that appears. The Expression Editor opens. | 7. | Edit the expression as follows (Figure 17.43). Figure 17.43. Edit the expression to use the attribute. :pSphere1.translateY=pSphere1.Height* sin(time*10); The expression is now relying on input from the Height Attribute to determine how high or low to move up and down. | 8. | In the timeline, click frame 1 to go to the first frame. | 9. | In the Channel Box, right-click the Height attribute and choose Key Selected from the pull-down menu (Figure 17.44). Figure 17.44. Right-click the Height attribute and select Key Selected from the menu that appears. | 10. | Go to frame 100 in the timeline and type 10 into the Height attribute in the Channel Box. | 11. | Right-click the Height attribute and select Key Selected from the pull-down menu. | 12. | From the Windows menu, select Animation Editors to open the Graph Editor. The Graph Editor opens and displays the animated attributes of the sphere (Figure 17.45). Figure 17.45. The Graph Editor displays the selected object and the attributes that are animated. | 13. | From the View menu, select Show Results (Figure 17.46) to see the curve created by the sin function and the height curve. Figure 17.46. Select Show Results from the View menu of the Graph Editor to view curves created by the sin function. The curve moves up and down and its height follows the Height curve exactly. You can edit the Height curve and see the results update in the graph editor (Figure 17.47). Figure 17.47. Edit the Hieght curve in the Graph Editor to show the effect it has on the sin function. | 14. | Press Play on the timeline. The sphere starts out moving up and down a little bit and as the Height increases, the sphere moves up and down much more. | About script nodes Expressions are stored in the Maya scene file, but MEL scripts that you create are not stored on a per scene basis. In order to store MEL scripts in a scene file, you can create a script node. Script nodes are specialized in that they execute in response to certain events, such as opening a scene file or rendering a frame. One example of how you might use this feature is to make sure that a special interface or dialog box is open whenever the scene file is opened. To create a script node: 1. | From the Windows menu, select Animation Editors to open the Expression Editor. | 2. | From the Select Filter menu, select By Script Node Name (Figure 17.48). Figure 17.48. Select By Script Node Name from the Select Filter menu. The top portion of the Expression Editor switches to Script Node mode. | 3. | In the Script Node Name field, type HelloWorld (Figure 17.49). Figure 17.49. Type HelloWorld in the Script Node Name field. | 4. | In the Script field, enter the following script: confirmDialog -message "Hello World" -button "OK; | 5. | Click Create. The HelloWorld script appears in the Script Nodes section of the Expression Editor. | 6. | From the Execute On menu, select Time Changed (Figure 17.50). Figure 17.50. Select Time Changed from the Execute On menu. You do not need to click Edit since this setting will automatically be saved for the Script Node. | 7. | Move to a new frame in the timeline. When the current frame is changed, it triggers the HelloWorld script and a confirmation dialog box appears (Figure 17.51). Figure 17.51. Move to a new frame on the timeline to trigger the HelloWorld script node. | 8. | Click OK. | To delete a script node: 1. | Open the Expression Editor. | 2. | From the Select Filter menu, select By Script Node Name. | 3. | In the Script Nodes section, click the name of the script node you would like to delete. | 4. | Click Delete. The name of the script node disappears from the Script Nodes section. | |