Maya Python for Newbies – Basics
In this guide, I will be giving a very brief introduction to Python in Maya. This guide is suitable even for those without any knowledge in programming.
- Knowledge in Maya/Maya LT
- Maya (Any version that has support for Python should work)
In this part we will:
- Go through how we are going to code in Maya’s built-in Script Editor
- Understand the difference between MEL and Python
- Get a brief introduction on modules and functions
- Find functions from the Maya technical documentation to perform standard Maya procedures
The Script Editor
Python files are just text files and can be edited in any text editing programs. For the purpose of this guide, I’ll be using Maya’s script editor.
The script editor icon can be found at the bottom right corner of the UI.
This will bring up the script editor.
The only two things you really need to care about are the two boxes highlighted in red. The top box is your history. It will display scripts you ran or actions you made. The bottom box is where you will be spending most of your time in this guide as that is where you input your commands.
You may notice the tab that says MEL:
MEL is the default scripting language that Maya uses. Unlike Python, MEL works in both Maya LT as well as Maya. However, we are not going to be coding in MEL, we will be coding in Python.
To create a Python script, simply click on the ‘+’ sign next to it and select “Python”.
Now you are ready to start coding in Python!
In Python, before we write our main code, we need to import modules. Modules can be compared to a toolbox. You can have a toolbox specifically for hammers or a toolbox that is specifically for wrenches.
To import a module, simply use the import command:
import (Module name here)
There are potentially thousands of modules that you could use. However, for starters, the module maya.cmds is usually enough for use in Maya. So let’s import that:
import maya.cmds as cmd
You may have noticed I added as cmd at the end. This is my way of shortening maya.cmds. This is not compulsory but it does help speed things up later on. Essentially, if we want to call a function from this module, we will just have to type in cmd instead of maya.cmds.
Using the toolbox analogy again, if modules are the toolboxes itself, a function is a tool in it. A function is basically a task that the code will execute. Just like how a hammer is meant to hammer nails.
Some examples of functions in Maya:
If you want to create a cube through Python, the function is called polyCube(). To call this, simply type in cmd.polyCube(). If you did not shorten maya.cmds then you will need to type maya.cmds.polyCube() instead.
import maya.cmds as cmd
You can try running this code by pressing Ctrl + Enter. If everything works you should have a cube in your viewport!
Let’s say you want your code to do a certain action that you know how to do manually. How do you find the name of the function for that?
All you have to do is actually just to look at the Script Editor history log.
This log keeps track of pretty much every action you make in Maya.
Note: It’s also written in MEL. Which means that if you wanted to you could just copy-paste this into a MEL script and it’ll just repeat whatever you did.
I highlight this because we can find the function we want in Python using this method.
Let’s say we do not know what’s the function to create a sphere. How do we find out?
Simply create a sphere and look at the history log.
We can see polySphere. I’ll explain everything that comes after it in the future.
You can head on to the Maya Python documentation and lookup that function using their search bar.
After searching “polySphere”, you can then access information about this particular function, including examples on how to use it.
We can create this sphere the same way we created the cube with cmd.polySphere()
I gave a simple example for creating a sphere but this applies to anything else you do in Maya.
Saving your Code
You can save this code into a Python file. Just click on to File>Save Script in your Script Editor. End off the file name with “.py”.
So to summarise:
- A module is a toolbox that carries a variety of tools.
- Functions are the tools that are included in that toolbox.
- We can call a module by using import followed by the module we want to import. Usually, we would import the maya.cmds module in Maya
- There are available functions to do Maya operations that can be found in their technical documentation
If you find this article useful, please leave some claps! Do look out for upcoming articles where I go into more detail about Python in Maya.