About the Script Editor


You'll edit and execute MEL scripts in the Script Editor, and whenever you perform a command in Maya, the associated MEL scripts are printed in the Script Editor. In many cases, you can copy and paste commands printed in the Script Editor and save them on a shelf for easy reuse.

MEL has an extensive list of commands that are listed in the Maya Help files. Press to launch Maya Help and go to MEL commands in the Developer Resources section. These commands are listed alphabetically and categorically.

MEL scripts are text files and can be stored in several ways. You may write scripts that are used one time, save them to a shelf, or save them to a script folder on the hard drive. You can load a script into the editor for further development or even edit a script in an external text editor.

The Script Editor is divided into two areas. In the top pane, the results of any executed commands are reported. In the bottom pane, scripts can be typed and executed (Figure 17.1).

Figure 17.1. The Script Editor is divided into two main areas.


To use the Script Editor:

1.

Click the Script Editor icon in the lower right area of the Maya interface. The Script Editor opens.

2.

Create a cube by selecting Polygon Primitives > Cube from the Create menu. The MEL command used to create the cube is printed in the top pane of the Script Editor (Figure 17.2).

Figure 17.2. The MEL command used to create the cube is printed in the top pane of the script editor.


 polyCube -w 1 -h 1 -d 1 -sx 1 -sy  1 -sz 1 -ax 0 1 0 -tx 1 -ch 1; // Result: pCube1 polyCube1 // 

3.

Select the line containing the polyCube MEL command (Figure 17.3) and click and drag it to the lower pane.

Figure 17.3. Select the line containing the polyCube MEL command in the Script Editor.


4.

Edit the command's parameters for w 1, h 1 and d 1 to w 2 h 2 and d 2.

5.

Select the entire line of MEL script and press on the numeric keypad to execute the script (Figure 17.4).

Figure 17.4. Select all of the script in the lower section of the Script Editor and press on the numeric keypad to execute the script.


A cube twice the size of the first one appears at the world origin (Figure 17.5).

Figure 17.5. A cube twice the size of the first one appears at the world origin.


6.

Select the script and click and drag it to a Shelf to save it for later use (Figure 17.6).

Figure 17.6. Select the entire script in the Script Editor and drag it to a Shelf for later use.


An icon for the MEL script appears in the Shelf (Figure 17.7).

Figure 17.7. A default icon for the MEL script appears in the shelf.


7.

Select the two cubes that were created in the view and press .

8.

Click the MEL script icon that was created. A new cube twice the size of the first one appears in the view.

In the previous example, the parameters of the polyCube command were manually edited to make a large cube. Instead of manually typing in value for parameters, MEL can use variables to assign those values. This allows the script to modify those values over time or interactively based on any number of events or conditions that the user defines.

There are several kinds of variables. The ones you will use most are floats, integers, and strings. A float variable stores a value that has a decimal point in it such as 1.001. An integer variable stores whole numbers 1, 5, or 1007. A string variable stores lines of text (such as "hello world"). If you assign a value that has a decimal point in it to an integer variable, the value past the decimal will be lost.

Each of those variable types can also be an array. An array is a variable that can contain several individual and separate values. You can then use all of the values stored in an array or specify an index number you wish to use.

In the next example, a random value will be stored in a float variable and used to create a cube. Then we will use a string array to randomly move the cube.

To use random variables:

1.

Create a float variable called $R by typing float $R; into the bottom pane of the Script Editor.

Variables always begin with a dollar sign.

2.

Assign a random number to $R using the rand( ) command by typing $R=rand(10); after the previous line of script.

3.

Edit the polyCube command to use the value of $R for the value -w, -h, and -d.

 polyCube -w $R -h $R -d $R -sx 1 -sy  1 -sz 1 -ax 0 1 0 -tx 1 -ch 1; 

4.

Select all the lines of script and press on the numeric keypad (Figure 17.8).

Figure 17.8. Select all of the script in the lower section of the Script Editor and press on the numeric keypad to execute the script.


 float $R=rand(10); polyCube -w $R -h $R -d $R -sx 1 -sy  1 -sz 1 -ax 0 1 0 -tx 1 -ch 1; 

A cube with a randomly chosen size appears in the view. To make this script more useful, the cubes should not only be random in scale but in position.

5.

Create a string array variable called $Name by typing string $Name[ ]; into the Script Editor before the polyCube command.

6.

Modify the line with the polyCube command by putting $Name = in front of it.

This will store the name of the cube created by polyCube.

7.

Encapsulate the polyCube command and its parameters with single open quotes .

The script so far should look like Figure 17.9.

Figure 17.9. The script should look like this.


8.

Create a new set of variables that will be used to randomly place the cubes in the scene by typing the following lines at the end of the script:

 $Gx=gauss(10); $Gy=gauss(10); $Gz=gauss(10); 

The gauss command is like rand in that it creates random numbers that are positive and negative and uses a more natural method to calculate the numbers.

9.

Add the following line of script to move the cube to a new random position:

 move $Gx $Gy $Gz $Name; 

The move command moves the named object three values for x, y, and z. The x,y, and z values are stored in $Gx, $Gy, and $Gz. The name of the object created with the polyCube command is stored in the $Name string variable.

10.

Highlight the entire script in the Script Editor and press on the numeric keypad several times (Figure 17.10).

Figure 17.10. Select all of the script in the lower section of the Script Editor and press on the numeric keypad several times.


 float $R; $R=rand(10); string $Name[ ]; $Name =`polyCube -w $R -h $R -d $R -  sx 1 -sy 1 -sz 1 -ax 0 1 0 -tx  1 -ch 1`; $Gx=gauss(10); $Gy=gauss(10); $Gz=gauss(10); move $Gx $Gy $Gz $Name; 

An interesting formation of cubes appears (Figure 17.11).

Figure 17.11. An interesting formation of cubes appears in the scene.


Tip

  • You can use any word you like to name variables as long as it begins with a dollar sign. You cannot, however, put numbers directly after the dollar sign. The name $4 will not work but $var4 will.

    In the previous script, we created an interesting formation of cubes with a simple script. But executing the script over and over again is tedious. To automate this, we need a conditional control statement, such as while. Other conditional statements include if and for.

    While loops wait for the the condition to become true and then stop executing the lines of script contained in in between brackets { }.


To use a while loop:

1.

Using the script from the previous task, "To use random variables," add a new integer variable at the beginning:

 int $x=1; 

This variable is used to count off how many times the script has been executed.

2.

Add the while statement to control the flow of the script:

 while($x<10){ 

As long as $x is less than (<) the value of 10, the script will repeat. You could create another variable instead of the "hardcoded" value of 10, or simply change 10 to a higher value, as needed. We will need to add the closing bracket (}) in order to have a functioning script.

3.

Type $x++; at the end of the script to increment the value of $x.

The script will count up to equal the value of 10 and stop the script from continuing to execute. However, if the count value never equals the value given in the while statement, there will be no simple way to stop the program.

4.

Type the closing backet (}) at the very end of the script to close the while loop.

The script should look like Figure 17.12.

Figure 17.12. Now the script should look like this.


 int $x=1; while($x<10) { float $R; $R=rand(10); string $Name[ ]; $Name =`polyCube -w $R -h $R -d $R -  sx 1 -sy 1 -sz 1 -ax 0 1 0 -tx 1 -ch 1`; $Gx=gauss(10); $Gy=gauss(10); $Gz=gauss(10); move $Gx $Gy $Gz $Name; $x++; } 

5.

Highlight the script and drag it to the Shelf, then double-click it to see the results in the Perspective view.

Sometimes you may have objects that look too orderly. By repositioning them in a random fashion, you can make the composition look more natural.

That's a good time to use a for loop. Create an array to store the names of the objects you want to move. The for statement steps through each object and moves them.

Use the ls command to retrieve the names of the objects you wish to move. This command lists objects and nodes in a Maya scene based on the parameters you specify. The ls -sl command will only list objects that are currently selected.

To use a for-in loop:

1.

In the Script Editor, select Edit > Clear Input.

2.

Select the cubes created in the previous task, "To use a while loop."

3.

Type for($cube in `ls -sl`){ into the Script Editor.

4.

Type $x=gauss(1); $y=gauss(1); $z=gauss(1); into the Script Editor.

This variable will be used to randomly move the cubes.

5.

Type move -r $x $y $z $cube; into the Script Editor to add a new parameter to the move command.

The r flag stands for relative, which means that the specified values are passed to the current position of the object. By default, the object moves to the absolute position of the value passed to it.

6.

Close the for statement by adding a closing bracket (});

The script should look like Figure 17.13.

Figure 17.13. After step 6, the script should look like this.


 for($cube in `ls -sl`){ $x=gauss(1); $y=gauss(1); $z=gauss(1); move -r $x $y $z $cube; } 

7.

Highlight all of the script in the Script Editor and press on the numeric keypad.

The cubes shuffle from their original positions. The ls sl command is like a six pack of sodas with several bottles. The for statement takes one of those bottles and pours it into a cup called $cube. Each time the for statement is executed, it will take one value in the list of values created by `ls sl` and store it in the variable $cube. The list of values in this case is the names of the objects selected.

To use the if command:

1.

Using the script from step 6 of the previous task, "To use a for loop," add the following after the move statement.

 if ($y > 1) { scale -r 2 2 2 $cube; } 

If the value the $y variable is more than 1, the cube will be scaled to twice its original size.

2.

Highlight the entire script and press on the numeric keypad.

The script should look like Figure 17.14 and some of the cubes will randomly become larger than the others (Figure 17.15).

Figure 17.14. Select all of the script in the lower section of the Script Editor and press on the numeric keypad.


Figure 17.15. The script scales the cubes larger when the variable $y is greater than 1.


The setAttr command is very useful in MEL scripting because it allows you to assign values to nodes attributes without using the attribute editor spreadsheet. Adding setAttr to looping scripts like the one in the previous task can make repetitive tasks simple. Next, we'll turn off the shadows cast by the cubes.

To use the setAttr command:

1.

Select one of the cubes and press to open the Attribute Editor.

2.

Click the pCubeShape tab (Figure 17.16) and in the Render Stats section, uncheck Cast Shadows (Figure 17.17).

Figure 17.16. Click the pCubeShape tab in the Attribute Editor.


Figure 17.17. Uncheck Cast Shadows in the Render Stats section of the cube's Attribute Editor.


The following is printed in the Script Editor (Figure 17.18).

Figure 17.18. The Script Editor displays the MEL script for turning off the cube's ability to cast shadows.


 setAttr "pCubeShape57.castsShadows" 0; 

3.

Using the script from the previous task, "To use the if command," replace the lines of script between the brackets ({}) with

 setAttr ($cube+".castsShadows") 0; 

4.

To add an attribute to an object stored in the $cube variable, put a plus sign (+) between them. Since you are combining these values and passing them to the setAttr command, put them in parentheses.

5.

The script should look like this:

 for($cube in `ls -sl`){ setAttr ($cube+".castsShadows") 0; } 

6.

From the Windows menu, select General Editors> Attribute Spread Sheet to open the Attribute Spread Sheet.

7.

Click the Render tab (Figure 17.19).

Figure 17.19. Click the Render tab and locate the Casts Shadows column.


In the Casts Shadows column, notice that all the selected objects currently have Casts Shadows turned on. Keep this window open so that later we can see the effect of the script.

8.

Select the cubes, then select the script and press on the numeric keypad.

The Casts Shadows column of the Attribute Spread Sheet updates to indicate that the cubes can no longer cast shadows (Figure 17.20).

Figure 17.20. The values in the Cast Shadows column update, indicating that they are off.


9.

Select the script and drag it to a Shelf for later use.

10.

In the Script Editor, change the 0 to a 1 at the end of the script to enable shadow casting again.

 setAttr ($cube+".castsShadows") 1; 

11.

Select script and drag it to a Shelf.

Now you can toggle on or off shadow casting for all the selected objects with a single click.

Once you have started adding scripts to the Shelf, it becomes difficult to tell them apart. In some cases, you can hold the cursor over the MEL icon to reveal a snippet of the script. Add a comment to the beginning of the script to make this feature more useful.

To add a comment:

1.

Add the following line to the beginning of the script from the previous task, "To use the setAttr command":

 //disable shadow casting 

MEL will ignore any line of script that comes after two forward slashes.

2.

Select and drag the script back to the Shelf.

3.

Hold the cursor over the MEL icon to reveal the comment in the Tooltips (Figure 17.21).

Figure 17.21. Hold your cursor over an icon in the Shelf to reveal its Tooltip.




    Maya for Windows and Macintosh
    MAYA for Windows and MacIntosh
    ISBN: B002W9GND0
    EAN: N/A
    Year: 2004
    Pages: 147
    Authors: Danny Riddell

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net