Popular Posts

Tuesday, February 22, 2011

Basic Mel: Variables

Defining Variables

Knowing how to define variables is the first thing you should learn if you want to start Mel Scripting. It is very basic, but after awhile this becomes second nature. Here is an example of how you would define one:

string $variableName = "Toast";

(variable type)  $(variable name) = (value);

The variable type always comes first, followed by the variable name.
The variable name must always come after a dollar sign ($). This lets Maya know the name of the variable.
Finally the value is what that variable equals.
You must end each equation in maya with a semi-colon (;). This tells Maya that this is the end of that equation.



Variable Types

There are 5 different variable types;

string - a string of letters, numbers and other characters. The value is always in quotations.
           examples :  string $name1 = "john";
                            string $name2 = "mike";

integer (int) - an integer is a whole number, either a positive or negative number.
           examples :  int $var1= 15;
                            int $var2 = -12;

float -  a float is a number with a decimal point.
           examples :  float $var3= 2.1578;
                            float $var4 = 95.5;

vector - a vector is 3 numbers, always floats.
           The first number in a vector is in the X position, the second is in the Y, and the third is in the Z. 
           examples :  vector $var5 = <<X, Y, Z>>;
                             vector $var5 = <<2.5, 5.0, 7.5>>;

                             $var5.x = 2.5;
                             $var5.y = 5.0;
                             $var5.z = 7.5;

matrix - think of a matrix as a spreadsheet. You define how many columns and how many rows.
           examples :  matrix $table[#of rows][# of columns];
                             matrix $table[3][5];

                             matrix $table[3][5] = <<1,2,3,4,5 ; 6,7,8,9,10 ; 11,12,13,14,15>> 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15


Arrays

Lets say you have a bunch of the same variables that want to arrange in a way that is simpler to remember. 
string $varA = "apple";string $varB = "toast";string $varC = "orange";string $varD = "taco";string $varD = "carrot";

An array lets you put them all into one variable by putting them in a row.
string $food[] = {"apple", "toast", "orange", "taco", "carrot"};

After the name of the variable, you will notice there is closed brackets, like this []. This tells Maya that this variable is an array. You can also use these brackets to find the variables inside of this array. By putting a number inside these brackets, you will get the variable at the position. So;
$food[0] = "apple";$food[1] = "toast";$food[2] = "orange";$food[3]  = "taco";$food[4] = "carrot";

* The positions always start with zero, not one.

you can make a float and integer array as well using the same method.
int $numbers[] = {5, 3, 7, 9, 2, 1};float $prices[] = {9.95, 1.50, 3.95, 5.00, 0.75};




7 comments:

  1. Kudo's on providing info to the public. Looks like the child of PHP and C#. - Aaron (Japan Society)

    ReplyDelete
  2. Excelent pagee men :D continuedd veryy good info

    ReplyDelete
  3. thank you so much for this! Very useful and easy to read.

    ReplyDelete
  4. Hope you'll page more stuff@great job

    ReplyDelete
  5. Very helpful! Thank you

    ReplyDelete
  6. Thanks for this page. Just wanna ask, can your use an int variable as index for an array? For example, can i do this?:
    int $i=3;
    string $Closet[] = {Socks,Shirt,Pants};
    print $Closet[$i];

    Thanks.

    ReplyDelete
  7. hey i started to day to learn MEL,i was finding stuff on goggle and i found this outstanding blog,really thank you.i just read two pages and i have zero knowledge about MEL.i hope it will help me.

    ReplyDelete