Learning MEL – Step #8 – Arrays (Part 2)

We already have covered arrays basic points. In this part I go over working with arrays.

When a new thing is introduced, in any place and field, from math to technology, in religion or politics, in CGI or in traditional painting, it comes with associated issues/advantages/components/tools etc. Therefore array also has it own functions and methods. Do not forgot that array is a hero in Computer Programming.

Related Functions:

Functions to be solely used with arrays are:

sort(), size() and clear();

Above three functions are general functions related to arrays. There are many functions used with array that are both complex and far from our point of study, thus I leave them uncovered. Let’s begin explaining our triple functions.

sort(): Use this function simply by placing the array inside the () of the function.


This function would sort the element in an array in “ascending order” and returns a sorted array. However, be aware that the array does not get sorted internally, only returned values from sort() function would sort the output. I return to this point later. Just for now, focus on this example:

string $examples[] = {“hello”, “laugh”, “that man”, “somewhere”, “awful”};
print $examples;

Regularly it outputs based on the order of declaration:

print $examples;
that man

But if you use sort() function, the result is this:


// Result: awful hello laugh somewhere that man //

However you if you like to keep the result of examples, you have two choices:

1 – Put the result in a new array, like this:

$examples = sort($examples);

Look at the above example. There is not need to use array curly brackets after $example, because MEL already knows it is an array, and you are not going to reintroduce it to MEL, but just what you indeed do is assigning a new set of data.

2- Declare a new array. look at below:

string $sorted_array[] = sort($example);

Look, do not mess up the above $example array with method number 1. The above $example variable is the array unsorted array.

Both of above methods work. Sometime you need only  and only direct printing without keeping the result necessarily in an array. In that specific case, use sort() directly.

size(): Use this function simply by placing the array inside the () of the function.


size() is a very very very important function which returns the number of elements in an array. why is it important? Because it dynamically allows you to know about the size of an array whenever you want to. For instance, if you want to print the list of selected nodes, how you would do that? of course you don’t know, since I have not yet covered it, but I will cover it perfectly in next tutorial. title Loops.

Look, this function would return an int number.

clear(): Use this function simply by placing the array inside the () of the function.

This function as its name is explanatory, would clear the array. It does not, however, undeclare it, it just remove every elements in the array , and let you have an empty array without any element.

Therefore, if I do clear our $example array, it will output:

clear ($example);

print ($example);

// Result: //

I wish you learn whatever we do cover here, as these are free tutorials.

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.

Learning MEL – Step #6 – Using Conditionals in MEL

In this tutorial I am going to make some output using conditionals. It is very easy and indeed essential to learn. However, for further advanced uses, it turns to be somehow complex. After all,  make sure that you have understood the previous section if you are an absolute beginner. At the bottom of this tutorial, there are several important notes about conditionals which must be read.

In this project I would tell you if X=1 then do this and if not do other thing. Very easy. But have in mind the syntax based on which the conditional is provided. Now I move to explain our little project.

  • What is/are the function of the script?
  • What is/are the design, order and/or segment of execution?
  • Command and Functions

What is/are the function of the script?

The script sees what the conditional include, and get the variable or place to which conditional is attached, then checks the value of variable etc., if it meets the condition then the given statement(s) is/are execute respectfully, if not it skips over the other following statement(s) or steps over to next part of conditional (optional). It is too vague now, I know. Let’s see it in practice in a few moments.

What is/are the design, order and/or segment of execution?

It includes basically three parts:

The conditioning, the checking, and then executing or skipping. (don’t pay attention to the // Mel Script , it is simple a comment, when there are two // may wont execute that line since it is considered (and is) to be a comment and nothing more. I will go over it later.

// MEL script



print (“This is a true word.”);


Look at the above script. It says that if $x is equal to the word “Childhood” then please print the phrase “This is a true word.”

But something is missing. Where the script goes to check $x, or let’s ask where is the $x? It must be before the conditional so that conditional checks something already read by execute (Maya). If it be after conditional then it means there is no $x and so you would get an error reporting undeclared variable. Look at the following part, now it is complete:

// MEL script

$x = “Manhood”;



print (“This is a true word.”);


With the above script what you get is NOTHING. For two big reasons:

1- You have not stated in the script that in case of FALSE condition, what to do.

2- You script ends by the conditional, for instance if you had lines after it, Maya would just like always execute them, after checking the conditional, regardless of its FALSE or TRUE.

But if $x = “Childhood” the phrase would have been outputted. Now it is the time of dissection:


The above line is the conditional statement. I haven not read anywhere but I call it the header of conditional. It is here that the trueness or falseness of the result is achieved. In IF-Statement you MUST only and only use “==” to check equality, and not “=” which is “assignment” in programming language.


print (“This is a true word.”);


In order for the conditional to work you must use opening curly bracket “{” and write you statements and the close it with closing curly bracket “}”. If you miss to put them you encounter syntactical error.

You could have done millions of jobs if the conditionals is true.

Note that with true I mean “meeting the standard of your conditional”. It means what you have stated is checked and is confirmed. For instance you could have told if $x is not equal to 5 then do something. Then if $x be 2, it means that it is not 5, and your conditional is true however.

Now what to do if the condition is false. You may decide to do something in case of FALSE condition.

It is easy, just next after the opening parenthesis put a “!” (exclamation sign) and then write the rest of your conditional.  For instance if you want to tell MEL if $x is not equal to zero then do something, you just state it as so:



print(“The variable X is not equal to zero.”);


Now Maya checks if $x is equal or not, and if it is not, then it execute the statement within the body of conditional, and if not goes with the rest of script.Now it is the time of real application. Let’s say if $x is equal to 2 print something, and if equals to 3 print something else. But I don’t go with one-time print execution, I want to do something so line by line our phrases get printed. Look here:

int $x = 2;
if($x == 2)
print (“Number 2 is printed.”);
$x = 3;
if($x == 3)
print (“Number 3 is printed.”);

In the above code you see two conditionals coming after each other. One following the other one. You can do this with unlimited number of conditionals. No two If-Conditionals in anyway could be related, and they are fully performing independently. Now, since each conditional is part of a script, it can modify, change or use variables and other accessible elements. In the first conditional we see,as regular  in case of true, it goes into the body of conditional where statements are stored. I have put the print function and then have changed the variable’s value to 3, so the executor moves forward and reaches the next conditional, here it checks for it and due to the fact that I have changed the variable’s value to 3,  it accounts for true and execute the insiders. You see nice it is. Much use in you future acts. Below you see a nice list of important points about conditionals:

  • Conditionals must be composed within two sections: One the parenthesis and another curly brackets.
  • Parenthesis are for condition to be checked while curly brackets are for the statements to be executed if the conditions is met as true.
  • Every statement within the body of conditionals must be followed by a semi-column.
  • If you want to check for true/false of your condition and if not met, run another condition, you must use “else”, as already described.
  • Else does not need (and must not) be compiled with condition-definition (parenthesis containing the condition just like if-statement), since it is simple the opposite of if-statement.
  • There are other types of conditionals which would be covered later, but fundamentally we have ended one important conditionals.
  • A variable declared within a conditional, could not be accessed/used from outside of it. Therefore the following code would report “undeclared variable” error. Because you have not declared the variable from outside of it, and it is a rule in MEL that variable declared within a conditional are only usable during the same conditionals. Thus, it is strongly recommended to declared variable before conditionals to avoid such problems.

Incorrect Code:

int $x = 2;
string $z = “This is regular variable”;
if($x == 2)
print (“Number 2 is printed.”);
$x = 3;
string $y = “This is condition-restricted variable”;
if($x == 3)
print (“Number 3 is printed.”);
print $y;

Correct Code:

int $x = 2;
string $z = “This is regular variable”;
if($x == 2)
print (“Number 2 is printed.”);
$x = 3;
string $y = “This is condition-restricted variable”;
if($x == 3)
print (“Number 3 is printed.”);
print $z;

Learning MEL – Step #5 – Conditionals, A Theoretical Overview

In this part we want to talk about conditions and their usage in computer programming. But without any actual work in MEL. If you are not familiar you can use our previous tutorials on MEL. Conditionals are a ways completing a super complex action in a computer programming language. It can does simple jobs from printing a text to most complex jobs such as simulation of particles and other dynamic systems. Conditions are exactly driven out of real life. The only big difference between real life and programming in terms of conditional is that many tasks in real life are done habitually, unconsciously while in computer there is no task to be done out of order and design of a super conscious computer, however in a limited scope.

Let me give you an example of a real world action which include step by step of conditionals. You want to eat a piece of bread with a bunch of fried potatoes. Now? Let’s begin the scenario:

You go to refrigerator to pick a slice of bread. You open the door and find that you have already run out of bread. So what to do? there are a plateful of french fried. Then this happens:If I do have bread, I would eat french fried with bread

But If I don’t have bread, I do go to super market to buy.

(now that I go)

if I have money I would but a bottle of soda

(now that I wanna but)

If there is black soda I would buy

but if it does not have I would b the orange one!

but If I don’t have I would not buy

This is the very usage of it in programming, too. But instead of “but” we use “else”.

For instance if you want to check if the user has entered the value or not, and if not, then report him an error, you must use conditionals.

If the sphere’s radius is less than 1 color it red, if it is greater than one, leave the color as it is.

If the character is in a distance longer than 100 units from camera, then move it 50 frames in second, if it is in less than 100 units from the camera move it in 25 frames in second.

This was a very brief overview of what conditionals in computer programming would indeed do. In later tutorials we would go through it practically.

Learning MEL – Step #4 – Variables in Practice[String]

In this part we are going to utilize variable and to see it all in practice. Now it is very important to ask our questions so that we get the points.

  • What is/are the function of the script?
  • What is/are the design, order and/or segment of execution?
  • Command and Functions

What is/are the function of the script?

It prints a text. But the text is a compilation of several pieces of texts. It adds them together, make a sentence, and prints them. Before the execution of print, in the end of the chapter, we also add a sphere creation script, and use some pieces of text to name a part of the script.

What is/are the design, order and/or segment of execution?

At first we declare line by line our required strings, and then bring our function “print” and “polySphere”.

            Command and Functions

We learn how to use “polySphere” function and some parameter related. We also reuse the function “print”.

We learn how to use operators to add text and make a final phrase, sentence or whatsoever.

In this project we want to print a text using a variable(s), but also use some other functions to expand our base knowledge. We already know how to print something in Maya, but here I go through print a string of characters using variables. First name and last name are going to be outputted via Maya print function. Therefore:

Declare a variable for the first name. Since you are going to output text, the data type of the declared variable must be “string”. Then  we set the name of variable also to “firstname” and assign it “Mostafa” (my own first name).

string $firstname = “Mostafa”;

Now we have successfully declared a variable. Whenever we do declare, such steps relatively are done. Now do repeat the same steps but this time set the name of variable to “space” and assign it a simple ” “, so that in our sentence and phrase we could give space between words.

string $space = ” “;

And now for last name:

string $lastname = “Talebi”;

Here let’s make a core phrase also. Set the name for this variable to “corePhrase”:

string $corePhrase = “The MEL Tutorial is written by:”;

You see that now we have put a sentence in a variable. We could even put longer text. Now this is what we have so far:

string $firstname = “Mostafa”;

string $space = ” “;

string $lastname = “Talebi”;

string $corePhrase = “The MEL Tutorial is written by:”;

It is the time of outputting. Use the “print” function as always for printing a text. But here things are a little new. You need “+” sign to put all strings in a sentence. “+” is a little strange for string summing, since “+” is for mathematical calculations and not textual representations. In other languages such as PHP you would use “.” for adding strings to each other. Now let’s do them. Note that to have a clean and errorless script, use parenthesis to add strings to each other. Doing do, let Maya understands to calculate parenthesis a s a whole for the print function. Now let’s write:

print ($corePhrase + $space + $firstname + $space + $lastname);

Note that we have used variable $space two times. We could have used it one million times. Now our whole scripts would be like this:

string $firstname = “Mostafa”;

string $space = ” “;

string $lastname = “Talebi”;

string $corePhrase = “The MEL Tutorial is written by:”;

print ($corePhrase + $space + $firstname + $space + $lastname);

Let’s do other things. Let’s make a sphere and name the sphere according to our $firstname and $lastname and then add a prefix to it by another variable.

Do write:

string $sphere_passifix = “has_created_this”;

bow we have declared a variable as a passifix for the sphere which is created. Now the function to create sphere. Note that we need a parameter in the sphere itself to name it properly. Use Maya MEL Documentation to see whether it has it or not (it has).

polySphere –name ($firstname + “_”  + $lastname + “_” + $sphere_passifix);

Note that we have put a string inline “_” for our name. We can use direct strings in place of variables to avoid a chaotic environment. A crucial point that you already must be familiar is that ” ” empty spaces are not allowed for name in Computers. They are weird.

-name is a parameter for the function polySphere, and is true as long as is used in the place after polySphere and the final “;”. It is called “flag” in Maya. So flag is the same as function’s parameter. Pay attention to the spelling, all lower case and without space between hyphen and the parameter name. Now let’s make the radius a little higher, for instance 2:

polySphere –radius 2 –name ($firstname + “_”  + $lastname + “_” + $sphere_passifix);

The order of flag is not important. For example, you could have written it like this:

polySphere–name ($firstname + “_”  + $lastname + “_” + $sphere_passifix) –radius 2;

Now our whole script would be like this:

string $firstname = “Mostafa”;

string $space = ” “;

string $lastname = “Talebi”;

string $corePhrase = “The MEL Tutorial is written by:”;

string $sphere_passifix = “has_created_this”;

polySphere -name ($firstname + “_”  + $lastname + “_” + $sphere_passifix) -radius 2 ;

print ($corePhrase + $space + $firstname + $space + $lastname);

I have put polySphere just before the print so that the print function output as the last function. Regularly Maya reports on every individual function, and if I place print before the polySphere, its output would be lost by output of polySphere function. You can test it yourself to see what I mean. The interesting point is the dynamism of variables. Now change the variables to whatever you like and you see it shifts accordingly and affects on all related parts.

Hope this tutorial be useful for you friends.

Learning MEL – Step #3 – Using Variables – Part I

In this part I am going to introduce one of the fundamental parts in Programming. Variables. Variables are little bags that store in themselves something useful. This is the most important feature of variables. They store data. For instance, you “declare” the variable X to be assigned 2, and later in the script, you write 5 + X (Just remember old days of school’s math formula), it equals 7. You can ask this question that why then not just writing 2 instead of X, what’s the use of it? There are several points important about variables, within which, you can find your answer also.

Variables are dynamic storage. For instance a value might just be unknown or changing in time and you want an area in script to grasp the change. Let’s give an example: There is a form, an input for the user to enter a number, then how do you know what value is entered? You retrieve the value from the input form and place it in a variable. By doing this, you assign the value to a variable. Look, = never means equal in Programming but is called assignment, and equal sign is ==. Later we would explore it more.

Variables ease the process of programming by referencing all values to one storage. For instance you want to say that how much a person does drink water during a year. You calculate the amount of water within a day and then multiply it by 365. And this calculation happens several times during the script.For the purpose of a neat script, a maintainable script and a safe script you must assign 365 to a variable. the variable let’s say is year. so whenever you use “year”, you simple is referencing it to storage which has 365 in itself. To put it in a nutshell, variables are words, exactly words which are representing a specific number. And there is something special about them, you can only declare a unique variable. Its name must not be mixed with another variable. If you use a name two times, you simply say to Maya to ignore the first declaration from the second-declaration on.

Another crucial limit of variable is that you cannot begin their name with numbers, if so, then Maya reports syntactical error.

After this introductory part I go in dealing with variables. For declaring a variable in Maya you need to put a “$” (without quotations) prior to the name.A Dollar Sign. Therefore:

cubeLength (this is not true)

$ cubeLength (this is not true; note the space after the dollar sign).

Anyway. The following lines give you the way it works. Here I want to print  “Hello World!” using variables.  Doing so, we see it in practice;

string $text = “Hello World!”;

print ($text);

If we dissect the first line, we would have the following parts:1) string

2) $text

3) “Hell World”

4) ;

Let’s explain them. What is string? string is any character in a programming language. It is a “data type”. Just you use it whenever dealing with text. Instead of string, you can say “text” even. Or in other words, it is “traditionally a sequence of characters”.

Examples of string:

“Table” “tree” “That Man Standing There” “Operating System” “ME Software” and …

Pay a close attention to quotation marks around each string. quotation marks are essential, without them, the text are not known as to be string. For instance:

(2 + 2) is a mathematical equation and if you give it you print function, Maya would output it as:


yes Maya would output 4 since it computes them and then prints 4. While if you give “2 + 2” to print function Maya would output:

2 + 2

Yes, because Maya sees it as a set of characters and not anything more.

Now move to the second part, $text? I have already explained the variable naming and I think you can now name variable easily.

Here you reach the “=” sign after the variable name. This is not equal sign, but it is assignment, by using this sign you are assigning a value to a variable. You could have already finished the declaration of the variables using ; and not = . The in following lines you would assign them value. Something like this:

string $text;

$text = “Hello World!”;

Note that only and only the first declaration of variable is accompanied by its Data Type.

For numbers you have to use “int” (without quotation) data type. Which is integer, but if your number has decimal, then you can use “int: and must use “float”(without quotation). However, if you accidentally give a number with decimal to an integer variable, decimals are stripped. and But you can use integer numbers with float data type. That’s why I most of the time use float data type when I’m not sure what happens to number in the future.

Examples of float:

2.1314556      10.5       5.89    3.14    1.8952434134   -5.0  -3   6 10     112   1100.1

Examples of integer:

5 8     12  112   6   -10    -78     0   23

Now I guess you have a basic understanding of what is variable and a primary knowledge about their usage. Lets do some calculations:

int $number_one = 10;int $number_two =  5;

int $result = $number_one  + $number_two;

print ($result);

This is the end of the first part of Variables. I hope it’s been useful for readers, and by keeping in touch, you can get more tutorials in this series.

Mostafa Talebi

Learning MEL – Step #2 – The First “Hello World” in MEL

Now It is the time of a practical first project. In this project we print a “Hello World” in Maya. So it is very significant to ask questions regarding the project.

Project-Related Questions

  • What is/are the function of the script?
  • What is/are the design, order and/or segment of execution?
  • Command and Functions

It is very important to ask these questions from yourself, and in most of our tutorials we indeed ask these questions. Now, I answer them one by one.

Answers to Project-Related Questions

        What is the function of the script?

The function of the script is that: it prints a text. You type your text, and when executing, it prints in Maya Command Line.

         What is the design, order and/or segment of execution?

In this project we are not going to design an UI (window), we want only a script. This means that our planning focuses only on the script itself. The script gathers the text and then gives it to the “print” function for the further execution.

          Commands and Functions

We need two important parts:

  • An area to store/put text
  • An area to print the “text”

Now it is the moment of writing the script.

A “function” is something in the script that would do a job. It executes something. Functions may or may not accept parameters. Many of the have parameters, but they are optional to be used or not. In this project we are not going to use any parameters.

Another necessary part is the text. “Print” function is a function that requires a text. You must give it a text for printing it. This is our script:

print “Hello World!”;

Thanks, we are done! Now run the script, for running it you need to hit the enter-button or the play icon on the script editor. Get used to select the entire script and then execute it. It will save your text in the script.

Now let’s begin to dissect the script:

We have three parts: print + “Hello World” + ;

Yes the semi-column is very significant, otherwise you will receive a syntax-error. It tells Maya that OK this line is complete and you can interpret it as a single statement.

Note!Programming language’s line is different from ordinary language’s. Statement is calculated from previous semi-column to the next semi-column. The above statement could have written like this:


“Hello World”


Because spaces and line breaks are not calculated in interpretation, and in some rare cases they are, but assume them not to be.

First word is “print”. If you use new versions of Maya, it is colored differently. However, I recommend you to use an editor for MEL if you are not a New Maya Version Owner. This word is a function. It does a job. “print” function does the job of printing a text.

“Hello World” is necessary for the print function. Indeed it is a part of it. For watching a movie on a DVD player you need a DVD or CD, otherwise it is impossible to watch anything. Indeed “print” function could be defined like this:

It is a function that accepts a text and prints in the Maya Command Line.

Now I hope that you are satisfied with what you have learned. Keep in touch and be informed instantly as tutorials and other stuffs pop up hust by adding your email into our newsletter form.

Mostafa Talebi