Translation Services

Hello

I want to introduce you a translation site where you can submit your translation orders.

Please visit Tarjom.ir website. This website offer full services on translation from English to Persian language.

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 #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:

4

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:

print

“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

mostafatalebi@rocketmail.com

https://artixel.wordpress.com