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

Conversational MEL Part 2

Topics Covered: variables; arrays; assignment; return values; basic loop;
Sample Script: Node name search and replace

Introduction


When we left off, we�d combined a pinch of cut-and-paste with a dash of basic UI to build a custom window for changing settings and automating short workflows. In this tutorial we�re going to break out of our monkey-see-monkey-do prison, and write a script from scratch. By the end of this article you will have written your very own mass-object-renaming-search-and-replace script! Once done this script will examine any selected objects looking for a word you specify. When found it will rename the object by replacing the specified word with something else. Are ya ready?

In this article we will go over:
  1. Several new MEL commands
  2. Variables, data types, and arrays
  3. Return values and assignment
  4. A basic loop
  5. Some tips on how to design and write complex scripts
  6. Your very own mass-object-renaming-search-and-replace script

Object Names


We�ll start our journey with the unassuming �ls� command. �ls� is short for �list� (I know, not exactly the world�s longest word to begin with). So what exactly does this �list� command do?

According to our trusty Oxford Pocket Dictionary it either
  1. does something with �palisades enclosing an area for a tournament�
  2. refers to �the process or an instance of listing� (useful definition, that)
  3. �names one by one�

Needless to say definition #3 is the one we�re interested in. The �ls� command names, one by one, all of the objects in your scene. Getting this list of objects is almost always your first order of business in any script - before you can do something, you need something to do it on.

Why don�t you give it a try. Start a new scene, create a few polygon objects, and type �ls� in the Command Line:

ls results


You should see something like the above. The text �// Result: � followed by a list of names. These are the names of every object in your scene (who�d a thunk there could be so many in a nearly empty scene, eh?). If you scroll on over to the end of the list you should see the polygon objects that you just created.

ls results


These names are just that: names. They are strings that refer to specific objects in Maya. If you remember from last month, a string is short for a �string of characters� - basically one or more words or letters (numbers and punctuation are fine too). These strings are not objects themselves, they simply refer to some object in your scene.

Hello My Name Is...


Knowing an object�s name is enough to interact with it. This is similar to the fictitious �sit� command we went over last month:

sit �Bowser�;

The word �Bowser� is not the same thing as the living, drooling animal at your feet - but using his name is enough to get him to obey a command. The same is true for Maya objects.

Let�s give this object name thing a go using the new command �select�. As you might expect the �select� command selects objects - all of the objects referred to in its command arguments to be exact. Type �select� in the Command Line, copy all of the output from �ls� after �// Result: � ...

Copy the ls command results


... and paste it into the Command Line.

Paste the ls command results


Hit enter and see what happens:

All objects selected


Bammo - everything in your scene is selected. If you�re thinking �That�s all well and good, but do I really have to go through all that cut and paste rigmarole every time I want to select something? C�mon man, there�s gotta be a better way!� - Fear not, a better way there is. Gather close and dim lights, because it�s time to talk about a little something I like to call �variables and data types�.

Variables


A variable is basically a container, a bucket, which holds some bit of information.

Data being set into a variable
Data in a variable

You can think of a variable as the stained napkin with the phone number of the cutie you met last night, the �Today�s Special� line on the menu in front of Annie�s Kitchen, or even a literal bucket like those carried on the backs of quick-service motorcycles. It is important to note that a variable is distinct and separate from the information it contains.

Call me
Today's Special


A stained napkin is not a phone number, the words �Today�s Special� are not the same as �fried eggs�, and the quick-service dude is probably not delivering a blue bucket.

Like the preceding examples, the information contained in a variable can be changed.

Variable getting new data
Variable has new data

The quick-service gent will deliver the contents of his blue bucket and likely refill it with a new package elsewhere. Each morning Annie will erase yesterday�s special and write a new one. And the next time a hotty offers their number you can whip out that old napkin, cross out the previous number, and hand over the pen.

Let's talk...
Today's Special: hotdog


What does not change, though, is the meaning of the information. Your trusty napkin will always contain the number of your latest acquaintance, the �Today�s Special� line will always contain the name of the current special, and the quick-service guy will always be delivering the contents of his blue bucket.
We�ll walk through the �Today�s Special� example in Maya-ese:

string $todaysSpecial;

This line declares a variable. In Maya variables don�t exist until they are declared. A parallel to real life might be manufacturing the napkin, sign board, or blue bucket. Only after a variable has been declared can it be used to store information.

There are two parts to this line:

string $todaysSpecial;

$todaysSpecial is the variable name. All variable names start with a dollar-sign �$�, and can contain only letters, underscores �_�, and numbers (although a number can't follow the '$'). Ignoring these rules will end you up with a big plate o� �Syntax error�:
Bad variable declarations


Like an object name, the variable name is the way that you refer to the variable and through it the information it contains.

string $todaysSpecial;

string, by now an old friend, is the data type of the variable. A variable can only store information of its declared data type. A restaurant would no more write their menu on the side of a quick-service bucket, than that delivery guy would try and wrap his package up in an old napkin.

The Maya data types are notably less exciting than mystery packages and the phone numbers of attractive singles. There are only three: string, int, and float. (Actually there are more than three, but the others aren�t important yet.)

A string, as we�ve touched on before, is a series of letters, words, numbers, and punctuation like:

�beating a dead horse�
�Enough already, I get what a string is.�
�pCube1�

An int, short for �integer�, is a whole number like:

3
-42
1337

A float, short for �floating-point�, is a number containing a decimal point like:

-3.2
99.44
0.0001234567

The term �floating-point� refers to the fact that the decimal point can appear anywhere in the number. It�s interesting to note that all ints can be represented as floats. int 3 can be written as float 3.0, int -42 can be written as float -42.0, etc... Okay, so maybe it�s not that interesting.