Conversational MEL Part 2
0 Comments
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:
- Several new MEL commands
- Variables, data types, and arrays
- Return values and assignment
- A basic loop
- Some tips on how to design and write complex scripts
- 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
- does something with �palisades enclosing an area for a tournament�
- refers to �the process or an instance of listing� (useful definition, that)
- �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:
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.
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.
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: � ...
... and paste it into the Command Line.
Hit enter and see what happens:
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.
|
|
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.
|
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.
|
|
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.
|
|
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�:
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.
Author: JamesPiechota
Submitted: 2006-10-19 11:21:40 UTC
Tags:
Software: Maya
Views: 35,161
Related Items
-
2 and single Sofa Set 3D Model
$25.00 (USD) -
2 Storey Office Building 3D Model
$20.00 (USD) -
Amazon Kindle 2 3D Model
$75.00 (USD) -
U-2 Spy Plane 3D Model
$28.00 (USD) -
HTC HD 2 3D Model
$75.00 (USD) -
iPad 2 with the Apple Smart Cover 3D Model
$60.00 (USD) -
Polikarpov po-2 soviet biplane 3D Model
$99.00 (USD) -
Taepodong 2 3D Model
$299.00 (USD) -
Apple iPad mini 2 3D Model
$20.00 (USD)