MEL uses variables to store information. Each variable must have a name that begins with a dollar sign, like $newValue or $random_float. There are several kinds of variables, each of which holds a specific kind of data. An int stores integers, such as 1, 5, or 47. A float can be used to store a number with a decimal point, such as 1.001. A string stores text, like "hello world." By declaring a variable, you can tell Maya what type you want that variable to be. You can also define an array for any variable type. An array contains a list of values of its type. For example, an array of strings could contain the text "red", "green", and "blue". To declare a variable and set its value: 1. | In the Script Editor, enter the variable's typefor example, float.
| 2. | After the variable type, enter a name for the variable, followed by a semicolon (;). For example: $testFloat; (Figure 17.9).
Figure 17.9. A line of MEL script should always be followed by a semicolon. This line declares the variable $testFloat as a float. | 3. | Press to start a new line.
| 4. | Type in the variable name, followed by an equal sign, the value you want the variable to have, and a semicolon. For example: $testFloat = 3.14; (Figure 17.10).
Figure 17.10. The second line sets the value of $testFloat to be 3.14. | | | 5. | Press on the numeric keypad to execute the script.
The value 3.14 is assigned to the variable $testFloat (Figure 17.11).
Figure 17.11. When the script is executed from the Script Editor, it disappears from the bottom pane and reappears in the top pane, along with its result. | Tips If you use a variable without declaring it, Maya will determine its type automatically, which may lead to unexpected results. For example, $x = 1 defines $x as an int, because 1 is an integer. If you then enter $x = 1.3, the result will still be 1, because an int can't store fractional values. You can combine declaring a variable and setting its value in a single line. For example: float $x = 1;. To use a variable: 1. | Declare a variable, and set its value using the steps in the previous task.
| 2. | In any MEL command, enter the variable's name where you would otherwise use a value of the same type. For example: sphere -radius $testFloat;.
When the command is executed, the variable's current value is used (Figure 17.12).
Figure 17.12. Because the value of $testFloat is 3.14, the newly created NURBS sphere has a radius of 3.14. | To declare an array and assign values to it: 1. | In the Script Editor, enter the type that the array will holdfor example, string.
| 2. | After the array type, enter a name for the array, followed by a set of square brackets and a semicolon. For example: $stringArray[];.
The square brackets indicate to Maya that you're declaring an array of strings, not just a single string variable.
| 3. | Press to start a new line.
| 4. | Type in the array's name, followed by an equal sign and a list of values separated by commas and surrounded by curly braces. For example: $stringArray = {"first", "second", "third"};.
The items are stored in the array in the order in which you list them.
| 5. | Press on the numeric keypad to execute the script.
The array $stringArray is created, and the values you entered are assigned to it (Figure 17.13).
Figure 17.13. An array containing the strings "first", "second", and "third" has been created. | To access an element of an array: 1. | Declare an array, and set its values using the steps in the previous task.
| 2. | In any MEL command, use the array name followed by the element's index in square brackets to access the value at that position in the array. For example: print $stringArray[2]; (Figure 17.14).
Figure 17.14. The print command displays a string. | Tips Array indexes start at zero, not one. So, if you have an array called $array with five elements, the first element is at $array[0] and the last element is at $array[4], not $array[5]. |