Storing scene information

You will store the value of the measurement annotation as a variable. A variable is a name used for convenience to refer to a certain location in memory. This location in memory can store data.

Variables have four main characteristics; name, type, value and scope. Name, type and scope are defined at creation time. Defining the name and type of a variable is referred to as declaring a variable. In MEL the name of the variable is always prefixed by the “$” symbol, indicating to the scripting language that the following characters name a variable.

The scope of the variable determines where the variable can be accessed from. If a variable is declared within a block of code, it cannot be accessed from outside the block of code.

Name and type cannot change after creation. The value of a variable can change, but the value must be of the earlier defined type.

NoteVariables in MEL should always have their type explicitly defined. If not explicitly stated, the scripting language attempts to imply a data type for the variable from the context of the first appearance of the variable. It is good practice to explicitly declare variables as implicitly defining variables can lead to scripting errors and slows down the execution of the script.

To store scene information as a variable

  1. In the Script Editor, type the following and press enter:
    float $diameter_barrel;

    This command declares a variable of data type float, with the name diameter_barrel. A floating point value is a number containing a decimal.

  2. To assign a value to the variable, type the following.
    $diameter_barrel = 4.2;

    The diameter of the barrel is 4.2 units. This value from the scene is assigned to the variable. A value is assigned to a variable with the assignment operator (=).

Storing other types of data

MEL also supports other data types that are common to most other programming languages.

To declare a string variable

  1. Type the following in the Script Editor
    string $testString = "this is a test";
    print $testString;

    The following is output to the Script Editor:

    this is a test

    The string data type is used to store multiple characters. The print command outputs the value of a variable to the Script Editor.

    NoteVariables can be declared and assigned a value in the same statement.

To declare an int variable

  1. Type the following in the Script Editor
    int $testInt = 5;
    print ($testInt/2);

    The following is output to the Script Editor:


    The int data type is used to store integer values. Integers are positive and negative whole numbers. In programming, when an integer is divided by another integer, the result is an integer, which is why the output here is not 2.5 (2.5 is not an integer).

    The print command’s argument is enclosed in brackets. The print command can only take one argument, so brackets are used to tell Maya to evaluate the contents of the brackets before executing the command. Brackets can also be nested in a method similar to mathematical statements to determine the order of evaluation of statements.

In the steps that follow, you create a row of barrels by spacing the barrels evenly. This creates barrels that are just touching, which ensures that there are no intersections during the rigid body simulation

In the next section, we’ll use the move command to position the duplicate barrels, using the saved value of the diameter as an argument for the Z value of the command.