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);

or:

print($text_2);

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:

Declaration:

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:

$selection[0];

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.

Conclusion:

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.

Advertisements

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

if($x==”Childhood”)

{

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”;

if($x==”Childhood”)

{

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:

if($x==”Childhood”)

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:

if(!$x==0)

{

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.