Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 105 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
105
Dung lượng
1,15 MB
Nội dung
916 | CHAPTER 17 Mel sCrIPtIng 2. In the Outliner, select the blue_nParticle object, and open its Attribute Editor to the blue_nParticleShape tab. 3. Scroll down and expand the Add Dynamic Attributes section below the Per Particle Array Attributes list. 4. Click the General button to open the Add Attribute window. 5. In the Add Attribute window, switch to the Particle tab. 6. Scroll toward the bottom of the list, and Ctrl+click spriteNumPP, spriteScaleXPP, spriteScaleYPP, and spriteTwistPP. The PP indicates that each attribute is a per-particle attribute (see Figure 17.9). 7. Click the Add button to add these attributes. The new attributes should appear in the Per Particle (Array) Attributes list. If they do not, click the Load Attributes button at the bottom of the Attribute Editor to refresh the window. 8. Open the Script Editor, and take a look at the history. At the top you’ll see that the addAttr command is used to add the attributes to the blue_nParticleShape object. Notice that each attribute uses two addAttr commands to add the attribute (see Figure 17.10). You can copy these lines of code and adapt them to the script based on what you’ve learned so far. Figure 17.9 Select the per- particle sprite attributes in the Add Attri- bute menu. Figure 17.10 The Script Editor reveals the syntax for adding the per-particle array attributes. Mel sCrIPtIng teChnIques | 917 9. Edit the mySpriteScript.mel file so it reads as follows: //create an array containing the current selection string $mySelection[] = `ls -selection`; //set the particle render type of the current selection to sprite setAttr ($mySelection[0]+”.particleRenderType”) 5; //add per-particle spriteNum, scale, and twist attributes addAttr -ln spriteNumPP -dt doubleArray $mySelection[0]; addAttr -ln spriteNumPP0 -dt doubleArray $mySelection[0]; addAttr -ln spriteScaleXPP -dt doubleArray $mySelection[0]; addAttr -ln spriteScaleXPP0 -dt doubleArray $mySelection[0]; addAttr -ln spriteScaleYPP -dt doubleArray $mySelection[0]; addAttr -ln spriteScaleYPP0 -dt doubleArray $mySelection[0]; addAttr -ln spriteTwistPP -dt doubleArray $mySelection[0]; addAttr -ln spriteTwistPP0 -dt doubleArray $mySelection[0]; In this case, the pasted code from the Script Editor was changed so that blue_nParticleShape is now the variable $mySelection[0];. 10. Test the script. First, save the mySpriteScript.mel file in the text editor. Save the Maya scene as shot1_v04.ma. 11. Go back to an earlier version of the Maya scene, before pink_nParticle was converted to a sprite, so you can test the entire script. Open the shot_v02.ma scene for the chapter17\ scenes folder on the DVD. 12. In the Outliner, select pink_nParticle. Copy all the text in the mySpriteScript.mel file, and paste it into the work area of the Script Editor. 13. Press the Enter key to run the script. When you run the script, you should have no error messages. If you do see an error in the Script Editor, double-check the text of the script, and make sure there are no mistakes. Even though there are no errors, you may have noticed that something is not quite right. Select pink_nParticle, and open its Attribute Editor to the pink_nParticleShape tab. In the Per Particle (Array) Attributes section, you’ll notice that the per-particle attributes do not appear, even when you click the Load Attributes button at the bottom of the Attribute Editor. What happened? 14. In the Attribute Editor, click the pink_nParticle tab to switch to the nParticle’s trans- form node. 15. Expand the Extra Attributes section. Here you’ll find the per-particle attributes. They have been added to the wrong node (see Figure 17.11). 918 | CHAPTER 17 Mel sCrIPtIng This is a common mistake that is easy to make. When you write a MEL script, you must keep in mind the potential problems that can occur when you or another user applies the script in a scene. In this case, the MEL script did exactly what you asked it to: it added per-particle attributes to the selected node, which in this case is the pink_nParticle node. Your intent is to add the attributes to the shape node. You have two options: You can make sure that you—and anyone else who uses the script—remember to •u always select the shape nodes of the nParticles every time you use the script. You can build in a command that ensures that the attributes are applied to the •u shape node. The second option is more desirable and actually involves little coding. 16. Edit the text at the top of the script in the mySpriteScript.mel file so it reads as follows: //create an array containing the shape nodes of the current selection pickWalk -d down; string $mySelection[] = `ls -selection`; A new line has been added to the top of the script using the pickWalk command. This com- mand moves the current selection down one node (notice the -d flag, which stands for direc- tion, and that the flag is set to down) in the node hierarchy, which means that if a user selects the nParticle node, the pickWalk command at the top of the script will move the selection down to the shape node and then load the selected shape node into the $mySelection[] array variable. If the user has already selected the shape node before running the script, it will still function properly since there are almost always no other nodes below the shape node in the node hierar- chy. Later you’ll see how to add a conditional statement so the user will get an error if they pick Figure 17.11 The per-particle array attributes have been added to the transform node by mistake. Mel sCrIPtIng teChnIques | 919 anything other than an nParticle object. Anticipating user errors is a big part of becoming an accomplished MEL script author. Repeat steps 10–12 to test the script. This time the new attributes should appear in the Per Particle (Array) Attributes list of the pink_nParticleShape node, as shown in Figure 17.12. Save the mySpriteScript.mel file. Adding an Image Sequence to the Sprites At this point, you can apply the animated image sequence of the logos to the blue_nParticle sprites: 1. Open the shot1_v04.ma scene from the chapter17\scenes folder on the DVD. 2. Open the Hypershade window, and create a new Lambert shader. Name the shader spriteShade. 3. Select blue_nParticle in the Outliner. 4. In the Hypershade, right-click spriteShade, and choose Assign Material To Selection. 5. Rewind and play the animation to frame 50. The sprites appear as blue squares. 6. Open the Attribute Editor for spriteShade. 7. Add a file texture node to the Color channel by clicking the checkered box next to Color and choosing File from the 2D Textures category under the Maya heading. 8. In the options for the file node, click the folder next to Image Name, and select the logo.1.iff file from the chapter17\sourceimages folder on the DVD. Figure 17.12 When you correct the script, the new attributes are added to the Per Particle (Array) Attributes section of the pink_nPar- ticleShape tab. 920 | CHAPTER 17 Mel sCrIPtIng The image appears as a small pentagon with a hole in it. When you rewind and play the scene, each of the blue_nParticles should look like the small pentagon. The image has an alpha channel that is automatically used in the Transparency channel of spriteShade. This image is actually the first in an animated sequence of 60 frames. The process for adding image sequences to sprites is a little odd. The next steps explain how this is done: 1. Open the Attribute Editor for the file1 node that has been applied to the Color channel of spriteShade. 2. Expand the Interactive Sequence Caching options. 3. Turn on Use Interactive Sequence Caching, and use the following settings: Sequence Start: 1 Sequence End: 60 Sequence Increment: 1 4. Turn on the Use Image Sequence option above the Interactive Sequence options (Figure 17.13 shows this). 5. Rewind and play the scene. The sprites don’t look any different, and you’ll see a warning in the Script Editor complaining that images cannot be found once the animation passes frame 60. The spriteNumPP attribute controls how the image sequence is applied to each sprite. Until you create an expression for this attribute, you won’t see the image sequence prop- erly on the sprites. 6. Open the Attribute Editor for the blue_nParticleShape node. 7. In the Per Particle (Array) Attributes section, right-click the field next to spriteNumPP, and choose Creation Expression to open the Expression Editor. Figure 17.13 Turn on Use Inter- active Sequence Caching and Use Image Sequence to apply the ani- mated images to the spriteShade material. Mel sCrIPtIng teChnIques | 921 8. In the Expression section of the Expression Editor, type the following: spriteNumPP=1; 9. Click the Create button to make the expression. 10. In the Expression Editor, click the Runtime Before Dynamics button to switch to runtime expression mode. 11. Type the following: spriteNumPP=spriteNumPP+1; 12. Click the Create button to make the expression. 13. Rewind and play the animation. Now you’ll see each of the blue logos appears as a small pentagon that grows into the snowflake logo as it is born (see Figure 17.14). 14. Save the scene as shot1_v05.ma. To see a version of the scene to this point, open the shot1_v05.ma scene from the chapter17\ scenes folder on the DVD. Adding Expressions Using MEL The previous section described the standard manner in which the spriteNumPP attribute is used to animate sprite images. Imagine going through that whole process for multiple nParticle objects. This is where scripting with MEL can take much of the tedium out of working in Maya. In this section, you’ll add commands to your MEL script that will apply the same expressions to all selected nParticles in a scene. In addition, you’ll add expressions to control the twist and scale of the nParticles. 1. Continue with the scene from the previous section, or open the shot1_v05.ma scene from the chapter17\scenes directory on the DVD. Open the mySpriteScript.mel file in a text editor. Figure 17.14 The snowflake logo is animated as each sprite nParticle is born into the scene. 922 | CHAPTER 17 Mel sCrIPtIng If you look in the Script Editor when you add the creation expression for blue_nParticle’s spriteNumPP, you’ll see the following command: dynExpression -s “blue_nParticleShape.spriteNumPP=1;” -c blue_nParticleShape; The runtime expression looks like this: dynExpression -s “blue_nParticleShape.spriteNumPP=blue_nParticleShape. spriteNumPP+1;” -rbd blue_nParticleShape The expression is added using the dynExpression command. The -s flag specifies a string that is the expression itself surrounded by quotes. You’ll see that the creation expression uses the -c flag, and the runtime before dynamics expression uses the -rbd flag. 2. Edit the text in the mySpriteScript.mel file so it reads as follows: //create an array containing the shape nodes of the current selection pickWalk -d down; string $mySelection[] = `ls -selection`; //set the particle render type of the current selection to sprite setAttr ($mySelection[0]+”.particleRenderType”) 5; //add per-particle twist, scale, and spriteNum attributes addAttr -ln spriteNumPP -dt doubleArray $mySelection[0]; addAttr -ln spriteNumPP0 -dt doubleArray $mySelection[0]; addAttr -ln spriteScaleXPP -dt doubleArray $mySelection[0]; addAttr -ln spriteScaleXPP0 -dt doubleArray $mySelection[0]; addAttr -ln spriteScaleYPP -dt doubleArray $mySelection[0]; addAttr -ln spriteScaleYPP0 -dt doubleArray $mySelection[0]; addAttr -ln spriteTwistPP -dt doubleArray $mySelection[0]; addAttr -ln spriteTwistPP0 -dt doubleArray $mySelection[0]; //add expressions for per-particle attributes dynExpression -s “spriteNumPP=1;” -c $mySelection[0]; dynExpression -s “spriteNumPP=spriteNumPP+1;” -rbd $mySelection[0]; You’ll notice that in the expression text itself, within the quotes, you can print just the name of the attribute spriteNumPP without adding the array variable. Maya understands that the expression will be added to the selected nParticle object. 3. Test the script by selecting the pink_nParticle object in the Outliner. 4. Copy all the text in the text file, and paste it into the work area of the Script Editor. Press the Enter key. Mel sCrIPtIng teChnIques | 923 5. In the Hypershade, select spriteShade, and apply it to the pink_nParticle object. 6. If there are no error messages, rewind and play the scene. You’ll see the pink_nParticle object now has the animated sprites applied. The sprites are pink thanks to the pink color that was originally applied to the spheres in the first ver- sion of the scene. 7. Select the blue_nParticle object, and edit the creation expression. Add the following, and click the Edit button (see Figure 17.15): spriteTwistPP=rand(360); spriteScaleXPP=rand(0.5,2); spriteScaleYPP=spriteScaleXPP; 8. Edit the runtime before dynamics expression. Add the following line, and click the Edit button: spriteTwistPP=spriteTwistPP+1; 9. Rewind and play the scene. The blue_nParticles are rotated randomly as they fly through the scene. They are also sized randomly. Note that when you set a scaleXPP value for a sprite, you should then set the scaleYPP attribute equal to the scaleXPP attribute. This way, the sprites are scaled uniformly and remain square shaped. Figure 17.15 Edit the creation expression for the blue_nParticleShape in the Expression Editor. 924 | CHAPTER 17 Mel sCrIPtIng 10. To add these changes to the script, edit the text in the mySpriteScript.mel file so the expressions look like this (the expressions printed in this book span more than one line; when you place them in your script, you should paste them onto a single line without adding returns): //add expressions for per-particle attributes dynExpression -s “spriteNumPP=1; \r\n\r\nspriteTwistPP=rand(360); \r\n\r\nspriteScaleXPP=rand(.5,2); \r\nspriteScaleYPP=spriteScaleXPP;“ -c $mySelection[0]; dynExpression -s “spriteNumPP=spriteNumPP+1; \r\n\r\nspriteTwistPP=spriteTwistPP+1;“ -rbd $mySelection[0]; Figure 17.16 shows the script text in a text editor. The \r and \n that you see in the expres- sion stand for “return” (\r) and “newline” (\n). Adding these to the expressions in your MEL script creates spaces between the expressions in the Expression Editor, which makes the expressions organized and easy to read. 11. Edit the text, and save the mySpriteScript.mel file. Select pink_nParticle. Copy and paste the text from the text file into the work area of the Script Editor. 12. Select the last lines that add the expressions in the Script Editor, and press the Enter key (see Figure 17.17). By selecting just the last few lines and pressing Enter, you will execute only those lines that add the expressions. At this point, the other lines in the script have already been applied to the pink_nParticle object. To see a version of the scene, open the shot1_v06.ma scene from the chapter17\scenes folder on the DVD. Figure 17.16 Edit the text in the MEL script to update the expressions. Mel sCrIPtIng teChnIques | 925 Creating a Conditional Statement Conditional statements are used to direct the commands of a script toward one action or another. If a certain condition is met, then the script performs an action; if not, the script does something else. Conditional statements work very well for error detection. In the case of the current example, you’ll use a conditional statement to make sure the script works only when an nParticle object is selected. This can prevent errors from occurring in the script or in the scene. You will add a conditional statement to the mySpriteScript.mel file that tests to make sure the selected object is an nParticle object. If it is, then the script will run and perform all the com- mands you’ve created in the previous section. If it is not, the script will print an error message that says that the selected object is not an nParticle. There are several types of conditional statements. In this example, you’ll use the most com- mon if/then conditional. The syntax for this type of conditional looks like this: If (condition to test is true) { Execute commands; } else { Print error message or execute a different set of commands; } The statement within the parentheses at the start of the conditional is the statement that needs to be tested. If this statement is true, then the commands within the first set of curly braces are executed. Notice that these commands are indented. Using indentation in your script makes the script appear organized and legible. Figure 17.17 The lines that add the expres- sion commands are highlighted in the Script Editor. Pressing the Enter key executes just the selected lines of code. [...]... mySpriteScript.mel file to your local directory Save the script in the Maya\ scripts directory found in the My Documents folder Scripts are usually saved in the My Documents \Maya\ scripts folder on your local drive You may also save scripts to the My Documents \Maya\ 2 010\ scripts directory (Mac users can place their scripts in the Users\Shared \Autodesk\ maya\ 2011- x64\scripts folder), or if they are specific to your... likely not interfere with one of Maya s global procedures Scripts containing global procedures can be saved in the My Documents \Maya\ Scripts folder on your local drive directory (Mac users can place their scripts in the Users\Shared \Autodesk\ maya\ 2011- x64\scripts folder) These scripts should load automatically when Maya starts, making the procedures available for use within a Maya session You can source... any script in Maya Maya uses global procedures to create the interface and perform the tasks necessary to make Maya functional To make a procedure global, you simply start the procedure using the text global proc instead of just proc You should be very careful when creating your own global procedures It is possible to overwrite one of Maya s own global procedures, which can disrupt the way Maya works... within Maya, switch to the Python tab in the Script Editor This tells Maya to interpret any commands as Python and not MEL Likewise, you can switch the command line and the command shell to Python mode Before you can use Maya commands within Python scripts, you must first import the Maya commands into Python To do this, switch to the Python tab in the Script Editor, and type the following: import maya. cmds... key to execute the command You can test a Maya command by typing the following: maya. cmds.sphere (radius=1, name=’myBall’) Note that the syntax for the Python command is different from the MEL syntax Apostrophes are used around the myBall variable instead of quotes or accent marks, and the line does not end in a semicolon Mel and Python in Action: Molecular Maya Maya has a huge reputation as the industry... molecules such as DNA into Maya, and then we were limited by how we could display the structure itself to spheres or particles In the past year, this has all changed thanks to the efforts of Harvard Medical School scientist Gaël McGill and extremely talented Python programmer and biologist Campbell Strong Together they have created a free plug-in for Maya called Molecular Maya (or mMaya) This plug-in was... file on your computer, the Molecular Maya interface can connect directly to the online Protein Data Bank at www.pdb.org, and if you know the specific protein ID number, you can type this into molecular Maya, and, without having to leave Maya, the protein will be downloaded and appear in the viewport within just a few seconds Beyond just acquiring the protein, Molecular Maya allows you to easily switch... Smooth to paint the correct deformation weight values Use Maya Muscle╇╉ Maya Muscle is a series of tools designed to create more believable ╇╉ deformations and movement for objects skinned to joints Capsules are used to replace Maya joints Muscles are NURBS surfaces that squash, stretch, and jiggle as they deform geometry Master it╇╉ Use Maya Muscle to create muscles for the hind leg of the... scene in the chapter10\scenes folder of the DVD Solution╇╉ Open the jellyBeans_v01.ma scene in the chapter10\scenes folder of the ╇╉ DVD Open the Hypershade, and select the glass material—this is a standard Maya Blinn shader In the Raytrace Options of the glass shader’s Attribute Editor, turn on Refractions, and set Refractive Index to 1.1 Create a test render | Chapter 10: Mental Ray Shading... creating visual effects for film, commercials, and television Most studios in Hollywood use Maya as its primary 3D application But did you know that scientists, specifically molecular biologists, are starting to use Maya to help them visualize their own research in life processes? For many years, scientists using Maya s animation, dynamic, and rendering capabilities to as a visualization tool, struggled . You may also save scripts to the My Documents Maya 2 010 scripts directory (Mac users can place their scripts in the UsersShared Autodesk maya 2011- x64scripts folder), or if they are specific. scripts in the UsersShared Autodesk maya 2011- x64scripts folder). These scripts should load automatically when Maya starts, making the procedures available for use within a Maya session. You can. one that can be called upon by any script in Maya. Maya uses global procedures to create the interface and perform the tasks necessary to make Maya functional. To make a procedure global, you