This is the very basics. With what you know now you can create some basic stuff, but this is far from enough to create more advanced scripts with user input or repeating code with different values etc. I would like to go through three more things before I actually attack the script I promised I'd go through.
- if - statements
- loops
- procedures
Sure you can use strings here and there to build something now, but it's when all of this starts reacting to each other the fun starts.
if Statement
What do you do when you are thirsty? You go get a drink... If I'm not thirsty, I'll not get a drink. For simplicity I'm going to make an int called thirsty.
int $ma_thirsty = 1; //This represents me being thirsty...
Now I've defined that I'm thirsty so Maya will understand this. Then I'll make Maya ask me if I'm thirsty and if I am I want her to tell me that she'll bring me a glass of water in a minute. If I'm not thirsty I'll make her tell me to let her know when I am. I'll write down the code first and then go through it:
if ($ma_thirsty == 1){
print "I'll get you a glass of water in a minute";
}
Then you can go:
if ($ma_thirsty == 0){
print "Let me know if you get thirsty then:)";
}
But in Maya you can also say "if variable is true --> do this", "else --> do that". So we could rewrite this:
if ($ma_thirsty == 1){
print "I'll get you a glass of water in a minute";
}
else{
print "Let me know if you get thirsty then:)";
}
What we say here is "if variableis 1, get glass of water". "If it's anything else, let me know". So if the variable was 0, 2, 3, 4, 6 or whatever, but 1 it will print "Let me know if you get thirsty then:)"... Let's say we want the if statement to print something else when the variable is 2, but still print the other things we already stated we could use something called else if:
if ($ma_thirsty == 1){
print "I'll get you a glass of water in a minute";
}
else if ($ma_thirsty == 2){
print "Get it yourself";
}
else{
print "Let me know if you get thirsty then:)";
}
So now Maya takes "2" into consideration too while filtering all the other numbers out in the else- section. 1 of course will still be in the first if statement.
We could have declared the variable as a string too. We could check if the string said "yes" or "no" instead of 1 and 0. 2 could have been "maybe" and so on. Notice the syntax here:
if(){
}
What you want to check goes in between the brackets, the commands you want to execute based on the check goes between the curly braces. There are two ways of writing the curly braces. Right now I'm in a conversion. I used to write it like this below because it is easier to read, but now I save a line by writing it like above. If you check the scripts Alias wrote you can see they also write it like this.
if ()
{
}
You can also use a command directly in the if statement. Let me write down an example:
string $ma_testArray[] = {"red","white","blue"};
if (`size($ma_testArray)` == 3){
print "yup, you have the colors of the norwegian flag";
polySphere -n "superSphere";
window -e -wh 100 100 $ma_superWindow;
}
I have to use backquotes because the command is executed while I check it. Remember how we used back-quotes when we declared a variable? We had to do that because we wanted the result of the executed command cast into that string. This time we want to execute the command directly to check the value. Size is a command you can look up in the helpfiles. I will not go through all commands I use because there are so many of them and some of the fun in MEL is to discover new commands and flags. I also had to use double equalsign. When we use a single one we give the one side a value of the other side. When we use double we check if the two sides are equal or not.
The reason I put those other commands in the if statement was just to show you that you can have several commands between the curly braces. You can put loops in here, procs, commands... Anything you want.
Next page will deal with loops.