MSFBA Chapter 02: Variables and arrays - put them in a box with a proper address.

November 6, 2017

1. What is a variable?

 

To understand what is a variable, I believe it’s good to know why do we need one in the first place. 

 

Let’s get back to Mike, the robot. Let say that this is my instruction list for Mike:

 

- pick up a flower

- smell the flower

- put a flower on your head

 

And Mike carry out the task perfectly, great! However what if I change my mind, I want Mike to do the same thing but with a sock instead of a flower. I have to rewrite the entire instruction like this:

 

- pick up a sock
- smell the sock
- put a sock on your head 

 

Well, it’s not too bad here as I only have to edit 3 lines of instruction. But what if my instruction is a thousand line long? What if I want to change more than one thing? It would be a nightmare everytime I want to change my instructions as you would imagine. 

 

Therefore, to make my life easier, I could use a placeholder instead of a direct object like flower or sock. What if I write my instruction like this:

 

- my object is a Flower

- pick up my Object

- smell my Object

- put my object on your head

 

Now my instruction is one line longer despite doing exactly the same thing as before. How could this be good? Well, as you might have already guessed, the extra line allow me to edit any object into my instruction with ease. All I have to do is to edit the first line like this:

 

- my object is a Sock
- pick up my Object
- smell my Object
- put my object on your head 

 

Easy peasy!

 

In that example, my obj is a placeholder. The thing you can assign to that placeholder varies between various object, making it a....<drum roll>.... variable.

That’s righht, in programming, we call that placeholder a variable. You can visualize variable like a box. You will give the box a name and put any object inside that box. So the next time you refer to the box’s name, your computer understand that you are refering to the Object INSIDE the box, NOT the box itself. Let’s try out this simple example. Copy paste the follwing script into MEL tab in your script editor and run

 

int $subdiv = 10;
polySphere -subdivisionsX $subdiv -subdivisionsY $subdiv;
polyCube -subdivisionsX $subdiv -subdivisionsY $subdiv -subdivisionsZ $subdiv;
polyPlane -subdivisionsX $subdiv -subdivisionsY $subdiv;

 

And if everything go right, you should have a cube, a sphere and a plane of 10 subdivision each side. Let’s break the script  down line by line

 

int $subdiv = 10; 

 

This line assign the number 10 into the placeholder $subdiv. I sense you have lots of question here but be patient, for now let’s make sure you understand the purpose of this line. $subdiv is a variable, a box with the name of $subdiv in which we place the number 10 inside.

 

polySphere -subdivisionsX $subdiv -subdivisionsY $subdiv;
polyCube -subdivisionsX $subdiv -subdivisionsY $subdiv -subdivisionsZ $subdiv;
polyPlane -subdivisionsX $subdiv -subdivisionsY $subdiv;

 

The next three lines are three simple commands to make a cube a cone and a plane with the flags to modify their subdivision values. These flags are given a value of $subdiv. Maya will be like:” ahhh, I see $subdiv so I will go to find the box name $subdiv, retrieve whatever inside the box and give that value to those flags”.

 

In other word, the code above is the same to Maya as this:

 

polySphere -subdivisionsX 10 -subdivisionsY 10;
polyCube -subdivisionsX 10 -subdivisionsY 10 -subdivisionsZ 10;
polyPlane -subdivisionsX 10 -subdivisionsY 10;

 

And if you want your subdiv to be other value than 10, you just have to change the first line. How neat is that!

 

Of course, in principle, that’s how it work. In detail, there are still thing that left unexplained for example the int right in front of the first line. Well, the entire session below is dedicated to explain just that.

 

2.1 Data type

 

In MEL, beside declaring the name of the variable, you also have to explicitly tell Maya what kind of data you can put into it. 

 

In our previous example, you can see that to declare a variable you do as follow:

1. Declare a datatype

2. Declare a name of the variable, all MEL variable name MUST start with $.

3. Assign a value to the variable.

 

 

 

 

There are many types of data but most of the time, you only work with a handful of basic type. Here is a starter pack:

 

int - integers, consist of whole number such as 1, 20, -7, 2478...

 

float - real number, consist of all real number, including numbers with decimal such as 7.453, 3.1415, 7.0, -2647.4646...

 

string - characters, consist of everything you can type on a keyboard such as “hey”, “ho”, “let’s go”... All value of string type MUST be put between “ “ or ‘ ‘.

 

Vector - consists of 3 floats. This is more complicated compare to the previous 3 so I dedicated a whole session just for this dude. 

 

IMPORTANT: In MEL, once you declare a variable with a certain type. You can no longer change its type unless you restart Maya. This won’t work:

 

int $myVar = 5;

string $myVar = “woohoo”;

 

In other word, you shall not have two different types of a variable with the same name.

 

2.2 Using appropriate data type

 

I would imagine that if you are totally new to scripting, the concept of data type can be quite hard to grasp, especially when it come to when and what data type to use. So I think it would be helpful that we will learn them via different example of different scenarios you could have with variable. It’s best if you could follow along and try out in Maya instead of just reading these.

 

Example 1. Doing math with variables. 

download script 

 

Example 2. Concatenate string.

download script 

 

Example 3. Making a simple zero out group script.

download script 

2.3 vector

 

The word vector can conjure up some terrying memories you might have from highschool math class. In all honesty, vector math is indeed crucial if you want to be a Technical artist and many vector math concept is a huge topic on its own. 

 

Luckily within the purpose of this tutorial series, we won’t concern too much about vector math and simply deal with a simpler definition of the vector data type.

 

Now go back to the box allegory, vector is a fancy box that have three smaller boxes in it. The three boxes is subsequently named x,y,z and can only contain a float data type. In short, vector is a data type consists of 3 float.

 

To declare a vetor value, put them in between <<  >> and separate with a , .

Example:

 

vector $myVec = <<2.3,5.0,-3.4>>;

 

In that example, 3 value are stored within one vector type variable. So how do we retrieve them? From left to right, the “box” name is x,y,z. You would put a variable name then a dot then x y or z

 

print ($myVec.x); //print out the first value

print ($myVec.y); //print out the second value 

print ($myVec.z); //print out the last value 

 

To sum up, just treat vector as a data type that allow you to store 3 float value inside a single variable.

 

So why do we use vector? Because it’s really convenient. There are lots of thing inside maya using vector type for example translation, rotation, scale (possess three float channel x y z), color ( possess three float channel r g b)... it’s totally make sense to allow programmer to have a data type that can one shot three values at once instead of putting one by one value.

 

For example, let say I want to make a simple script to move my sphere, the follow three scripts work exactly the same, but one of them is way more convenient, see it for yourself.

 

// making a sphere
polySphere -n "mySphere";

 

// method 1: make three variable

float $xValue = 4.5;
float $yValue = 2.3;
float $zValue = -3;

move $xValue $yValue $zValue "mySphere";

 

 

// method 2: make a vector

vector $moveValue = <<4.5,2.3,-3>>;
move ($moveValue.x) ($moveValue.y) ($moveValue.z) "mySphere";

 

 

 

3. Array - a fancier box

 

Vector is convenient but Array take it to a whole new level. In fact, array is going to be the most useful thing you can ever have in scripting. If we stick with our box allegory. An array is a box consists of endless smaller boxes. The only rule is these smaller boxes content must be consistent in term of data type. For example a string array must consist of all string values, a float array consists of all float value, you cannot have a mixed type array in MEL.

 

To declare an array, you do the same thing as declaring a single variable with and extra [] at the end of the array name. To assign elements to an array put them in {} separate by a comma.

 

string $strArray[] = {“yo”,”sup”,”?!”,”how are you?”};

 

float $flArray[] = {2.3,4.6,3.1415,6.0,7.8};

 

int $intArray[] = {3,7}

 

You can also use the function size() to find out how many items inside your array. For example:

 

print size($strArray); //the result is 4

print size($flArray); //the result is 5

 

Tips:
- print is an excellent command to test out a script as it only prints out the value in the script editor history session without doing any effect on your maya scene.
- // is called comment out, anything you type after // will become a comment. A comment is something for human to read; think of a note you give to yourself to help you organize your thought. To the computer, it simply ignores anything you put behind //. The color also turn red for the comment. 

 

Next we need to know to to retrieve value out from an array. Go back to the box allegory, remembet array is a box that contain many smaller boxes. These boxes are given an index number starting from 0 (machine count from 0, this is something worth remembering). The first item you put in an array will be put to box 0, second item to box 1, third item to box 2 and so on.

 

To retrieve the data, you would first call for the array name follow by [index number] like this

print $strArray[1]; // result is “sup”
print $flArray[3]; // result is 6.0 

 

At this point, array might seem overkill. Why would you need to store so many values inside a single array? Would it be more confusing as oppose to using variable? To answer this please move on to the next topic where we will discuss loop, array will be a compulsory ingredient there. However, at this point in time you can use array to improve further on the offset group script above

 

check out the improved version here:

download script

 

 

 

Please reload

Recent Posts

Please reload

Archive

Please reload

Tags

Please reload

 

+65 90781743

©2017 by Technical Dood.