
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:
- create 5 polySpheres.
- select all of them
- insert the code above in the scriptEditor
- 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. |