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 3 of 4

Return Values

Okay, so now, armed with the power of variables, data types, and arrays, surely our selection job must be a lot easier. Let�s see:

string $objects[];
$objects[0] = �time1�;
$objects[1] = �renderPartition�;
$objects[2] = �renderGlobalsList1�;
$objects[3] = �defaultLightList1�;
select $objects[0] $objects[1] $objects[2] ..........

Hmm... still not looking all that easy, eh? Earlier I said �the �select� command will select all of the objects referred to in its command arguments�. Well we�ve already got all of our objects packaged up nice and neat in the $objects array, there�s really no reason we have to pull them out one-by-one to pass to the �select� command.

Just like you can access a regular variable�s value through the variable�s name, so can you access all of an array�s values through the array variable�s name. Quickly try printing out the entire menu from Annie�s Kitchen:

print $fullMenu;

print $fullMenu;

With this in mind we can update our code snippet again:

string $objects[];
$objects[0] = �time1�;
$objects[1] = �renderPartition�;
$objects[2] = �renderGlobalsList1�;
$objects[3] = �defaultLightList1�;
select $objects;

Now we�re getting somewhere... but I�m still thinking it would be easier to just copy-and-paste the output from the �ls� command.

If you�ll remember last month we identified the main components of a MEL command as:

commandName flagsAndArguments target

I must apologize because I wasn�t entirely truthful: there�s one more component we haven�t talked about yet. A complete breakdown actually looks like this:

returnValue commandName flagsAndArguments target

Many, although not all, commands generate some information and give it (or �return it�) to you the scripter. This information, called the command�s return value, can be a value of any of MEL�s supported data types. For some commands the return value is used to provide information about changes the command may have made, and for some commands, like �ls�, the return value is the whole point of the command.

Let's take a look at the real-life act of buying groceries. You're the proud parent of two teenage children and your refrigerator has gotten pretty bare. You turn to your loving progeny and issue the command 'buyGroceries'. As expected your adorable kids each turn to the other and say "You heard him, go get some groceries." Looks like 'buyGroceries' may be in need of the '-who' flag to indicate which of your offspring should do the chore. In this case there are two possibilities:

buyGroceries -who "Joey";
buyGroceries -who "Sally";

.. and the return value will indicate what groceries were purchased. Lest you wonder which child to saddle with this burden, let's see what they would buy.

Sally, your first born, responsible, A-student goes shopping and returns with a few bags of food:

buyGroceries -who "Sally";
// Result: vegetables rice tofu fruit fresh-juice //

Joey, your youngest, the class clown and alternative learner, goes shopping and returns with a single bag of "food":

buyGroceries -who "Joey";
// Result: ho-hos ding-dongs twinkies //

In addition to the quasi-real-life example above, you�ve seen several MEL return values over the past two articles:

Here the return value (found immediately after the text �Result: �) is a string storing the name of the scene - by default always �untitled�:

file -f -new;
// Result: ./untitled //

Here the return value is a string array containing the names of the newly created polygon sphere nodes (we�ll get into more details about nodes, and why two names are returned, in later articles):

polySphere -r 1 -sx 20 -sy 20 -ax 0 1 0 -tx 2 -ch 1;
// Result: pSphere1 polySphere1 //

And, as you�re no doubt very familiar with at this point, here the return value is a string array containing the names of all the objects in the scene:

// Result: time1 renderPartition renderGlobalsList1 ...

Note the �// Result: � text - this exists to differentiate return values from regular printed messages. To see what I mean try making a sphere and executing the �print� command:

print versus return

See? No matter how sneaky I try to be, you will always be able to tell the difference between printed messages and return values. Why is this important? Because return values, like any other value, can be assigned to a variable. Printed messages, however, �are for display purposes only� - they are not values, and can not be assigned to variables.

Give this a go by assigning the return value from the polySphere command to a string array variable:

string $spheres[];
$spheres = polySphere -r 1 -sx 20 -sy 20 -ax 0 1 0 -tx 2 -ch 1;

Missing backquotes error

Nothing�s ever easy is it? You need one more, tricky, bit of syntax before you can assign the return value of a command to a variable: the backquote `. This frustrating piece of punctuation closely, oh so very closely, resembles the single quote ' - in fact it�s my personal belief that it serves no other purpose than to cause MEL scripters torment (which might explain why it dares not show its face in any sensible written languages and has been banished to the far left corner of the keyboard with only the equally useless �~� for company.)

singlequote versus backquote

Any time you want to assign the return value of a command to a variable you have to enclose the command in backquotes:

string $spheres[];
$spheres = `polySphere -r 1 -sx 20 -sy 20 -ax 0 1 0 -tx 2 -ch 1`;

To verify that the value was actually assigned, try printing out the $sphere variable:

print $spheres;

If you accidentally use single quotes instead of backquotes you�ll get an error message like this:

string $spheres[];
$spheres = 'polySphere -r 1 -sx 20 -sy 20 -ax 0 1 0 -tx 2 -ch 1';
// Error: $spheres = 'polySphere -r 1 -sx 20 -sy 20 -ax 0 1 0 -tx 2 -ch 1'; //
// Error: Line 2.12: Syntax error //

Finally, we can get somewhere with our selection task:

string $objects[];
$objects = `ls`;
select $objects;

Not half bad, eh? If you execute those three lines of MEL you should see something like:

All objects selected

You�ll note that some objects aren�t selected. This is due to details about how Maya handles selection of certain types of objects (�sets� in this case). I won�t get into it now, but if you�re interested you can check out the �Sets and Partitions� section of the Maya Docs.

We have one more concept to go over before diving into the renaming script.

But first a quick summary:
  1. The �ls� command lists all of the objects in the scene.
  2. The �select� command selects all of the objects referred to in its command arguments.
  3. The �print� command prints a message to the Script Editor and Command Feedback.
  4. A variable is a bucket for holding information. Variable names must start with a �$� and can contain only letters, numbers, and underscores.
  5. The type of data stored in a variable (i.e. �data type�) can be either a string, int, or float.
  6. A string
  7. An int is a whole number.
  8. A float is a number containing a decimal point.
  9. An array is a bunch of variables put together under one name. Each element of an array is referred to by number.
  10. A return value is information that a command gives or returns to the scripter. Return values can be assigned to variables just like any other value.
  11. When assigning the return value of a command to a variable, the command must be enclosed in backquotes ``.