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.