From the helpfiles:

An array is an ordered list of values. All values in an array must be of the same type. You can make arrays of integers, floats, strings, or vectors. Arrays grow as you add elements to them.

To declare an array variable, use:

  • the keyword of the type which this array will hold,
  • then the variable name,
  • add square brackets ([]) to the end of the variable name.

int $ari[];

The reason I go through array this early is because we use it very often with strings. Let's go through an
example:

string $ma_listSelectedObj[] = `ls -sl`;

We tell Maya that I want to "fill" the array with values of the kind "string". Then we tell Maya that this is
supposed to be an array by adding square brackets "[]". If you look at the backquotes you know that I
immediately want to cast a value to the array. I used the command "ls" which is used to list several things.
In our case here, selected objects. To make it interesting I want you to do this:

  1. create 5 polySpheres.
  2. select all of them
  3. insert the code above in the scriptEditor
  4. highlight all the code and hit numerical- enter and read the result in the historyWindow.

The result will be:

// Result: pSphere1 pSphere2 pSphere3 pSphere4 pSphere5 //

So now the array actually contains the result above. If you want to check that out, use the print command
to get maya to print the values.

print $ma_listSelectedObj;

When we write it like this we tell Maya to print the whole array. If we want to print one of the values, say
pSphere2, we have to say

print $ma_listSeleectedObj[1];

Now you might have been thinking "Isn't pSphere1 the FIRST value here? Shouldn't we be printing
$ma_listSelectedObj[2]?". You see, Arrays start at 0 so the first thing in the list will be $ma_listSeleectedObj[0].

You don't have to cast values to the array via a command. You can also define your own values like this:

string $ma_myColors[] = {"red","white","blue"};

The syntax: curly braces at the start and end. Semicolons to define the values and commas to seperate them.
There are no backquotes here because we do not use a command to cast the values to the array. This is
actually how we define arrays manually.

If you want to edit the arrayItems you can do this:

string ma_myColors[1] = "black";

Now you suddenly get "red" "black" "blue" if you try to print the array again.

 
 

As opposed to an integer a floating point number is a number with a fractional part. 10.1255 is a float while
an integer can't display what's after the comma. I say float because that's the short form for floating point
number. Let's declare a float:

float $ma_myFloat = 1.456;

You can declare a float like this or you can put the number between quotes like you'd do with a string. You'd
want to use a float for something like translate values:

float $ma_getTranslateX = `getAttr pSphere1.tx`;

A computer calculates integers faster than floating point numbers so if you can use integers, do so!

 
 

From the helpfiles:
A vector is a triple of floating point numbers (usually representing X, Y, and Z). It's convenient to have a triple-float data type in MEL because so many operations in 3D involve manipulating X,Y,Z values.

So as it says here vectors are pretty nifty. You don't think much about it, but every time you move an object
you set a vector value. It's pretty easy to illustrate:

vector $ma_myVector = `getAttr pSphere1.translate`;

This will return <<posX, posY, posZ>> , three floats. You can use several vectors to create new vectors, but
this is math and I won't go into that for now.

 
 

Concatenation: You can also add variables together. Say you create two strings, $ma_hello and $ma_name

string $ma_hello = "Hello";
string $ma_name = "Martin";

If you want to add these up you simply go

string $ma_sayHello = $ma_hello + $ma_name;

Here I declared a new variable and I cast the values of the two other variables into it. If you print
$ma_sayHello you can see that the result is HelloMartin. We would probably want a space between those and
we have to do that manually.

string $ma_sayHello = $ma_hello + " " + $ma_name;

You could also add + "\n" so Maya goes to a new line when printing the result. You can also write new strings
into there like this:

string $ma_sayHello = $ma_hello + " there " + $ma_name + "\n";
print $ma_sayHello;

The \n is a "special character" in MEL. There are several of these characters:

 

\"
\n
\t
\r
\\

 

quotation mark
newline
tab
carriage return
backslash
  Now experience with different variables and concatenate them in different ways.
 
 
 
 

The content of this tutorial is copyrighted by law.
Reproduction of the content without permission
will result in legal prosecution.

www.final3.com © 2006