 #### Maya Python for Newbies-Basics II

This tutorial is part of a series. If you have not yet checked out our first part, where we cover the basics of scripting in Maya, do catch up first before continuing.

All good? Let’s go!

Our agenda for this article:

• Go through some common terminology
• Revisiting the topic of functions
• How to print out a message
• If-Else statements

### Variables

Think of a variable like a box. You can assign items to put inside it. Maybe a hammer, or a wrench. However, as soon as you labelled the box as intended for a certain tool, that is all you can put in it.

If you labelled a box meant for hammers, that is all it’s going to be used for. If you labelled the box as a box for wrenches, that is all it’s going to be for.

In programming, this is pretty much how it works.

There are 3 common types of variables:

• Integers
• Floats
• Strings

Integers are whole numbers without decimal points. Some examples are:

• 7
• 39
• 450

Floats are also numbers but they refer to numbers with decimal points such as:

• 2.0
• 33.14
• 999.999

Strings are essentially any form of text:

• Bob
• Apple
• car

There are many more, but for now, let’s focus on these 3.

As mentioned, variables can be thought of as a container. In Maya we can create that container by giving it a name and assigning a value to it. Python is smart enough to pick out what type of variable it is based on what you assigned.

`myName = "Sul"`

In the example above, I’ve created a variable called myName and assigned the string value Sul to it. Python knows that it is a string because of the apostrophes at the start and end of the value(“ ”).

If you want to do this for integers and floats, it’s a little more straightforward.

`myBirthday = 9`

The same applies for float. Simply include the decimal point.

`myBirthday = 9.0`

### Math

Now, we are going to do some simple maths:

A+B=C

We are going to add two variables together to give us a third value. We will be using integers and floats only for this example.

`valueA = 20valueB = 5valueC = valueA + valueB`

If you have done algebra, this should be pretty straightforward. But basically, we assigned 20 to valueA and 5 to valueB. We then created a valueC which is going to be the sum ofvalueA and valueB together. We should expect the value of valueC to be 25.

We can check this by writing one more line.

`valueA = 20valueB = 5valueC = valueA + valueB`
`print valueC`

Note: print is a command we use to display the value of a variable. In this case it will display the value of valueC in the History log

You can also add strings together. Let’s use variables to form a short greeting.

`greeting = "Hello_"name = "Sul"greetingResult = greeting + name`
`print greetingResult`

And the result should print out Hello_Sul.

### Functions

As discussed in the previous article, functions are a set of commands that will do a certain action. We can also create our own functions to compartmentalise our process.

To create our own function, we use def.

`def MyFunction():`

In this line, we created a function called MyFunction. This is followed by () and :.

Inside the brackets is where we can include what are called “Flags”.

And do take note of indentation that comes after : . This is a way for us to basically define which statement the code is going to execute.

As you can see from this example, this indentation basically tells us which function the respective print statement belongs to.

Let’s create a system where the user inputs their name, and running the code will write a greeting message for them.

`name = "Bob"message = "Hope you have a nice day,"`
`def GreetUser(targetMessage, targetName):    finalResult = targetMessage + targetName    print finalResult`

In this chunk we created two variables called name and message.

We also created the function GreetUser. Within the brackets, we created two flags called targetMessage and targetName.

Another way to think of flags are thinking of them as temporary variables. Basically saying that we have created two new temporary variables.

To call a function, it’s simply the name of the function followed by an open and close bracket. If this function has flags then we will need to assign them as well.

In this case we will call our function using this line GreetUser(message, name).

Your code should now look something like this

`name = "Bob"message = "Hope you have a nice day,"`
`GreetUser(message, name)`
`def GreetUser(targetMessage, targetName):    finalResult = targetMessage + targetName    print finalResult`

What’s happening is that we are calling the function and assigning our name and message variable to the flags in the function.

### If-Else

If else is “probably” one of the easiest concepts to learn because you do it in your daily life already.

Here is an example.

If you are sleepy, go and take a nap. Else, catch up on that TV show you have been watching.

Now let’s put this in Python code.

`currentMood = "sleepy"`
`if currentMood == "sleepy":    Sleep()else:    WatchTV()`

We created a variable called currentMood where we can define what mood we are currently feeling.

if currentMood == “sleepy”: basically says “If our current mood is ‘sleepy’ ”.

So if we are sleepy, we will run the function Sleep() .

However, if we are not, then we will run what is in the else instead. Which is to WatchTV().

Note: You may be wondering why we use == instead of =. A simple way to understand this is to think of = as “to copy or assign” and == to compare. This is why we use == in if-else statements as we want to compare for a match in both sides. This is not the case in lines where we use = where we are just assigning a value.

Now what if you want to expand your choices between just sleeping and watching TV? That’s where elif comes in, short for else-if.

`currentMood = "sleepy"`
`if currentMood == "sleepy":    Sleep()`
`elif currentMood == "healthy":    Exercise()`
`else:    WatchTV()`

We added a new line, elif currentMood == “active”:. So if our currentMood is “healthy”, we will run the function Exercise() instead. As you may have guessed this can be extended further.

`currentMood = ""`
`if currentMood == "sleepy":    Sleep()`
`elif currentMood == "healthy":    Exercise()`
`elif currentMood == "angry":    SmashStuff()`
`elif currentMood == "sad":    Cry()`
`else:    WatchTV()`

So at this point, if currentMood do not meet any of the if and elif it will execute the else instead.

### If-Else in Maya

So, how do we apply this to Maya?

We can create a very simple system where we want to spawn a primitive based on a string variable we give it.

`import maya.cmds as cmd`
`shapeName = ""`
`if shapeName == "Cube":    cmd.polyCube()elif shapeName == "Sphere":    cmd.polySphere()elif shapeName == "Cone":    cmd.polyCone()else:    print "I do not recognise this shape"`

We have a variable shapeName. In here we input the shape that we want. If we input “Cube”, it will create a poly cube based on the if-else statement for “Cube”. However, if whatever we input does not match any of the if or elif statements it will go to the else statement instead. Which, in this case, will print out a sentence stating that we do not recognise the shape.

### Function in Maya

We can also turn this into a function.

`import maya.cmds as cmd`
`shapeName = ""`
`CreateShape(shapeName)`
`def CreateShape(targetShape):`
`    if targetShape == "Cube":        cmd.polyCube()    elif targetShape == "Sphere":        cmd.polySphere()    elif targetShape== "Cone":        cmd.polyCone()    else:        print "I do not recognise this shape"`

We created the function CreateShape and created a flag called targetShape. When calling this function, we assigned our shapeName variable to it. After we called this function, it will execute the if-else based on targetShape as that is what we have assigned it.

Note: If you were to remove targetShape and keep shapeName from the if-else as it is, it would still work since we are referencing the variable directly and not from the function flag.

### Summary

• The 3 main type of variables are ints, floats and strings
• Functions are defined by def
• If-Else statements uses if, elif and else.

If you find this article useful, please leave some claps! Do look out for upcoming articles where I’ll share a little bit more about the field of technical art! Maya Python for Newbies-Basics II was originally published in Mighty Bear Games on Medium, where people are continuing the conversation by highlighting and responding to this story.