Adverti horiz upsell
Conversational MEL Part 2
Conversational MEL Part 2
JamesPiechota, updated 2006-11-29 06:52:29 UTC 35,030 views  Rating:
(5 ratings)
Page 2 of 4

Variable Assignment


Back to our example:

string $todaysSpecial;

This line declares a variable of type string called $todaysSpecial. Let�s say it�s Monday and Annie�s Kitchen has just opened. Annie�s first order of business is to write out the special of the day:

Todays Special: fried eggs


$todaysSpecial = �fried eggs�;

The �=� in this line is called the assignment operator, it takes the value �fried eggs� and stuffs it into the variable $todaysSpecial. The assignment operator mimics the real life action of erasing the menu board and writing out by hand the current special of the day.

If you were to read this line out loud you might say �The variable todaysSpecial is assigned the value �fried eggs��. Or, for some real geek cred, you could say �todaysSpecial gets �fried eggs��. It�s best not to read the �=� symbol as �equals�, as in �todaysSpecial equals �fried eggs��, because, as we�ll see later, the word �equals� can be ambiguous - and besides all the geeks will laugh at you.

At this point we believe the $todaysSpecial variable stores the value �fried eggs� - but how can be sure? Enter the �print� command. This command simply prints its arguments to the Command Feedback and Script Editor. Give it a try:

print "Hi there!";


A little earlier I mentioned that you can access the information stored in a variable through its name. To illustrate this use the print command to print out the special of the day:

print $todaysSpecial;


As we can see Today�s Special is in fact �fried eggs� - if I were to walk into the restaurant on Monday and order Today�s Special, I would get some fried eggs. Earlier I mentioned that all variable names must start with a '$' sign. Just to see what happens why don't you try printing out 'todaysSpecial' without the dollar sign:

print todaysSpecial;


The day passes and now it�s Tuesday. Again Annie walks out to the menu board, erases the old special of the day and writes out a new one:

Todays Special: hotdog


$todaysSpecial = �hotdog�;

Assigning a new value to a variable will replace the previous one. Yesterday�s special is gone, replaced with the new special. If I order the special of the day on Tuesday, I won�t get fried eggs, instead I�ll get a hotdog. We can verify this using the print command again:

print $todaysSpecial;


It�s worth pointing out that although the value stored in the variable has changed, the meaning of the variable may stay the same. $todaysSpecial still holds the name of the special of the day, even if the exact dish has changed. The blue bucket on the bike always holds the current delivery regardless of the exact contents. And your napkin still holds the key to future romance, even if it�s now with Frank instead of Fred or Jessica instead of Julie.

This last point about the meaning of a variable is not a rule - just good practice. Maya only requires the data type of the value match the declared data type of the variable. You are quite free to assign unrelated values to the same variable:

$todaysSpecial = �hotdog�;
$todaysSpecial = �Hi, I�m Joey.�;
$todaysSpecial = �adsf.,m909%;(2.s�;

Assigning a value of the wrong data type, however, may lead to trouble:

int $myVariable = "hotdog";
// Warning: int $myVariable = "hotdog"; //
// Warning: Line 1.28 : Converting string "hotdog" to an int value of 0. //
// Result: 0 //

When the wrong data type is assigned, Maya will try and convert it to the right data type. In the above example Maya converted �hotdog� to the integer value of 0 - probably not what the scripter had in mind. Not all conversions will be so unexpected, and in later articles we�ll show examples where you may want to convert values from one data type to another, but, for the time being, it�s best to make sure your variables are only assigned values of the appropriate data type.

To recap:
  1. All variable names start with a dollar-sign �$�, and can contain only letters, numbers, and underscores �_�.
  2. A variable must be declared before it can be used.
  3. A variable can only store data of a specific data type: string, int, or float.
  4. The value stored in a variable can change, and the assignment operator �=� is used to change it.


Arrays


If you�ll remember back a bit, we embarked on all this talk about variables and data types in hopes that it would make our selection job a bit easier to do. Let�s see where we�re at.

Before, we had to manually copy the output from the �ls� command and paste it into the select command.

Copy the ls output


Now, through the power of variables we can assign the name of each object in the scene to a variable and pass that to the �select� command.

string $firstObjectName;
$firstObjectName = �time1�;
string $secondObjectName;
$secondObjectName = �renderPartition�;
string $thirdObjectName;
$thirdObjectName = �renderGlobalsList1�;
string $fourthObjectName;
$fourthObjectName = �defaultLightList1�;
.
.
.
select $firstObjectName $secondObjectName $thirdObjectName .......

Whoa! What happened?? That�s not easier at all! That�s like the opposite of easier!

The problem is that the variables we�ve discussed are designed only to hold a single value at a time, and the output from the �ls� command is, well, a list of values. In order to store a bunch of values at once we need something called an array.

An array is a bunch of variables all put together - a complete menu at Annie�s Kitchen or a stack of napkins with phone numbers. But instead of referring to each bit of information by name, you refer to each one according to the order with which it is found in the array:

Array variable


As a real-life example let�s take look at Annie�s Kitchen�s menu:

Full menu


Here a bunch of different menu items are collected together as a single list - and in fact many of the patrons at Annie�s Kitchen probably order their dish by number: �Excuse me, miss, I would like one order of #3 and two orders of #5, please� - or, more likely: �Hey, waitress! One #3, two #5s.�

Let�s see how this would look in MEL:

string $fullMenu[];
$fullMenu[1] = �hotdog�;
$fullMenu[2] = �hamburger�;
$fullMenu[3] = �fried eggs�;
$fullMenu[4] = �reuben sandwich�;
$fullMenu[5] = �homefries�;

Even without knowing what those little [] things mean it doesn�t look that different from the real life menu, does it? Let�s take a look at the first line:

Array

string $fullMenu[];

The [] at the end of the variable name tells Maya that you are declaring an array variable. Whereas

Regular

string $todaysSpecial;

declares a variable named $todaysSpecial which can store a single string variable, our first line declares a variable named $fullMenu which can store a list of string variables. Assigning to an array variable is almost the same as assigning to a regular variable:

Regular

$todaysSpecial = �fried eggs�;

The only difference is that we have to indicate which element of the array we want to assign to:

Array

$fullMenu[3] = �fried eggs�;

Here we�ve assigned the value �fried eggs� to element 3 of the $fullMenu array variable. Easy peasy, right? Unfortunately there�s one little curve ball: the numbers start from 0. Array indices, as they are formally called, start from 0 and count upwards. In our example above:

string $fullMenu[];
$fullMenu[1] = �hotdog�;
$fullMenu[2] = �hamburger�;
$fullMenu[3] = �fried eggs�;
$fullMenu[4] = �reuben sandwich�;
$fullMenu[5] = �homefries�;

There would be a gap at the beginning of the array: element 0 isn�t storing any information. You can verify this using the print command:

print $fullMenu[0];


This wouldn�t be that big a deal except for this whole �we start counting from 0� thing is embedded pretty deeply in MEL and programming culture. Many MEL commands and fellow scripters will assume your arrays have information at index 0 - so it�s best to try to get into the habit of always starting from 0.

If we correct both our real-life and MEL-life versions we end up with:

Full menu starting from 0


string $fullMenu[];
$fullMenu[0] = �hotdog�;
$fullMenu[1] = �hamburger�;
$fullMenu[2] = �fried eggs�;
$fullMenu[3] = �reuben sandwich�;
$fullMenu[4] = �homefries�;