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:

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

Learning MEL – Step #1 – What is MEL? What Does MEL Do? Why MEL? A New Method

Hello to all users. I am going to begin a MEL tutorial series from the very beginning all the way to more advanced tutorials. First of all, I highly recommend you to read my article on Maya Relations Here.

What is MEL really? Why do we go through programming in Maya? Autodesk Maya provides a very satisfying UI which allows users of multiple level to deeply interact with. You can customize it to a very deep extent. It does not require you to learn MEL or Python, and still you are highly free in manipulating the elements. Nevertheless, MEL enables you to automate the process. To create repetitive tasks, managed jobs, systematized management and workflow, and innovative creation. Still, the most important task of MEL is its automating task. Automation must not be degraded with the name of automation  in your mind. What does it mean? I will explain it.

What does UI enable you to do? All the jobs are recorded in Maya script editor history one by one. Go to Maya script editor and then select Echo All Commands in the menu “History” of the Script Editor. Then every task that you do with Maya is recorded and shown in this window.

Now let’s begin to become more specific on the MEL.

1- MEL (Maya Embedded Language) I do not want to talk about MEL history and its ancestor, and some tiny unimportant details that are told one thousand times, and with a search on the net you can find them. I want to talk about MEL practically. To do something, to learn something as it could be applied in the software. Good.
MEL is a language in the software Autodesk Maya. This scripting language allows you to do task more professionally in the Autodesk Maya. There is another language available to Maya user which is Python. That is different. I do not intend talking about that. When you want to create/do something with Autodesk Maya MEL, you need the commands. Commands are executors of Maya real functions. Even in some cases they are directly the same Maya functions. But you can not say that maya has several parts: Modeling, Rendering, Animation, Rigging, Dynamics, Effects, Live, Lighting, Scripting etc. and I then choose scripting. You can be a modeler and a MEL programmer as well. MEL only allows you to boost your Maya Knowledge. Then it is necessary for you to be the master of some part[s] in Maya and move them high in level using MEL. When you want to create a sphere in Maya, the following command does the job:

polySphere;

Then it is essential to ask this question that:

Isn’t it essential to learn that Maya could create sphere, and why one must create spheres.

Then you know that when you create a sphere in Maya from the menu Create/Polygon/Sphere It creates a sphere in the center of the scene. But I can change the name of the sphere form the channel box or the attribute editor as well. How is it possible to be done using MEL. In quest of learning it, you search and find out that “polySphere” function in the MEL has a propoerty called “name”. So you write:

polySphere name=”testSphere”;

anyway, you soon understand that Maya reports a syntactical error in your script. I leave the narration here and become more scientifically concerned on the subject.

Linear learning of a programming language tells you that you must learn syntax first. Just as it was true with our language learning at the school. Nevertheless, it does not work. In recent theories (from 1970 up to now) in Teaching Language Methods, it has been proposed and also propagated, and eventually spread across the world that since a language is an instrument and not a target, we must teach language not linearly, but dramatically and in action. Talking about various topics such as health, psychology, sports, economy etc but using the new language. I agree with the method, as I myself have been taught English using this method which is called Communicative Language Teaching, or briefly CLT. To end this part, I could say you how to be taught MEL, and how to use it, linearly, but it is just boring, I teach MEL in action and explain parts whenever necessary.

I think now you know what MEL is and that what jobs it does, however briefly. It’s time to finish this first part of MEL series with the following two paragraphs. You need MEL. If you want to become a master in Maya, which means in industry and 3d creation, you have to learn a programming language. There is Python, more advanced, more technical and also updating, and usable in other applications such as Houdini and also in Web Development. But the story does not end in here, MEL is greatly documented for Maya, its child “expression” (which I talk about later) is living inside Maya. You can learn it easier, since it is all reported in Maya Echo All Commands much more better than Python. I learned PHP but just after learning HTML/CSS. It is relevant. Later the relation would be easier to grasp. Hence, MEL is crucial to learn. In a studio where its technical director is standing behind your chair watching your commercial modeling for their new advertising, he or she would not be satisfied to see you translating(that is, translate is “move” in Maya, and you must know it already) spheres manually by hand in the scene, he or she, rather, expects to see you writing  a MEL script, executing it, and have one hundred spheres manipulated across the scene all randomly.

In this first part, I talked about MEL and some points related to learning and using it. Keep in touch with this page, Learning MEL just would be filled with various tutorials on various applications of MEL.

Mostafa Talebi