Learning MEL – Step #7 – Arrays (Part 1)

In this part we are going to cover arrays in MEL. Here I do explain what is array. I also would tell you three functions used exclusively with arrays.

Arrays are only and only a set of variables combined in one variable. Let’s give an example:

You have in a regular variable declaration:

string $text = “example”;

string $text_2 = “example_25”;

If you want to print them you simple would refer to them directly; such as:

print ($text);



The above example is not new to you, since we have already covered them. But what if you wish to put both variables in one variable? The solution is simple, array. But then how you can access them? Simply by using index of the element in the array. Now, I go into a comprehensive discussion of arrays in MEL.

How to declare?

The process is the same but with some extra steps to take:

after the name of variable, without any space, “[]” sign must be used.

If you want to assign it some sorts of data, then you must open a “{” and then write your element, separate them only and only with “,” and then close it with “}”. In the following parts it would be more explored. Look at this example:

string $array[] = {“element1”, “element2”, “element3”};

Why must array be used?

Never think that arrays are only an extra option to ease your path. They are as essential as simple variables are, and as inevitable as the use of simple variables are. Even sometimes arrays are the only option to use due to the language process at a certain scope. For instance, there are functions in MEL language that return the data  in an array, therefore to store their result one has to use an array, and nothing else. I will cover this part more in the future lessons, because  now it would be confusing.

When to use arrays?

Arrays are used largely in two ways:


First, when you are going to collect a bunch of data within separate sheets. Then one might ask what’s wrong with simple variables? You can not manage them properly, and also accessing them is nothing but a headache. Furthermore, if you want to use only regular variables, the look of your scripts would be ugly enough to prevent other programmers to apply modifications to your script. Any way, MEL is awkward in arrays. I do web programming with PHP, and it supports the use of more complex variables and thus easier workflow.

Stored Result:

This section makes the use of variables necessary. MEL has a function such as “ls” which only returns array. The below statement returns the list of selection.

ls -sl;

Most of the time, almost 99.99999 percent of situations there is a need to store the selection list. Because you are going to use the retrieved data;  Maya reports an error telling you that can not store data of array type into a regular variable. Even if there only be one object selected, which means an element that could be stored in a simple variable, still Maya does not accept it and errs; we now draw this conclusion that in some situations arrays are obligatory. The above statement could be written as so:

string $selection[] = ls -sl;

Now if I do print the $selection, it returns the full list of selected objects; however if there is only one object, you can refer to it like this:


Index of arrays begin from 0, and not 1.

Have you ever seen (surely you have) a tool in Maya works with two selected object; you must select an object and shift+select the second object? These are the examples:

Constrainting the objects: $selection[0] works as the parent (constraintor).

Curve to Surface: $selection[0] works as the profile or whatever curve.

and many other examples.

However you can directly declare an array with several elements:

string $array_variable = {“Hello” , “Goodbye”, “Function”, “Table”, “home”};

Now we have a variable with 5 subvariables. However, this is wrong to say subvariables, items in array are called elements. We have an array with five elements, it means the last index in array is 4 (remember we have already told index of array begins from zero). Well, to talk more about it: this is an array with string data, however you can use mutiple-data array. Look at the following example:

string $array_variable = {“Hello” , “Goodbye”, “Function”, “Table”, “home”, 2, -0.53574818747, true};

Now this is an array with all sorts of data in; string, int, float and Boolean.


Arrays are variables with capability of storing several variables with various data-type. Accessing them is possible only by using the relative key(index). Never forgot to use “[]” after the variable declaration so that MEL understand what to declare. In the next tutorial we would apply array-specific functions.

3 responses to “Learning MEL – Step #7 – Arrays (Part 1)

  1. Hi Sir,
    i regularly follow this tutorial on MEL script and is really helpful as a beginner . Any how i came across following error on executing the script . kindly let me know what went wrong.

    string $selection[]=ls-sl;
    // Error: string $selection[]=ls-sl; //
    // Error: Line 1.23: Invalid use of Maya object “ls”. //
    // Error: string $selection[]=ls-sl; //
    // Error: Line 1.26: Invalid use of Maya object “sl”. //

    Please do reply.



    • Hello buddy.
      Unfortunately the problem your script is my mistake in tutorial in not mentioning it. When you want to assign a function to a variable you have to use ” ` ” (without quotations). this sign ` (i think it is called ’tilt key’)must be around your function which is about to be assigned to a variable.

      string $selection = `ls -sl`;

      I think now you will not have any further problem.

Leave a Reply to iamsurag Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s