Lesson 1 : Python in Maya
In this course, we will learn how to use Python in Maya and explore all the potential possibilities for the basics of Python within Maya.
Additional courses will follow to further expand our knowledge. We will teach you how to use Python in Maya, including utilizing scripts to code outside of Maya and still be able to execute them directly within Maya.
Table of Contents
1. Introduction to Python in Maya
2. Where to write the first scripts in Maya
3. First python on Maya script
4. Discovering tools
5. Putting it into practice
6. Working environment
7. Sources
1. Introduction
In this course, we will delve into Python within Maya and explore all the utilities Python can offer us in the context of Maya.
​
Throughout this course, we will learn how to use Python and create
our first scripts that will allow us to automate actions.
​
maya.cmds is a Python module integrated into Autodesk Maya that
allows manipulation of the interface and scene elements. It provides
a high-level interface for interacting with Maya from Python scripts.
​
We will cover the fundamentals of the Maya.cmds module, and we
will learn how to create scripts to group objects and utilize key
commands within Maya cmds.
2. Where to write the first scripts in maya
To do this, you just need to go to the script editor right here at the bottom, as shown in the example. This interface has three main buttons and two areas that are very useful for us.
​
Firstly, there's the Play button which will execute the Python program. This button will clear the program you've written just below, but it has the ability to execute the script only on the selected lines. Then there's the double button which will also execute the Python program, but it won't delete the program. However, you won't be able to select parts of the script to execute - it will run the entire script.
​
Next, the first area below is simply where you can write your Python script. Above it, the second area acts like a terminal. It's not exactly a terminal, but we can compare it to one. It will give us all the necessary information, whether it's Python code or information about the scene itself.
To start, if we want to use the maya.cmds module, we'll need to write the following in the dedicated script writing area, like this:
3. First python on Maya script
Now, we can begin writing our first lines of code. For each line of Python code using the cmds library, you'll essentially be issuing commands that Maya will execute.
​
To start, we'll learn how to create cubes, spheres, and groups. To do this, just enter the following commands:
What you need to know is that if the text isn't in blue, it means the method doesn't exist. To find out what methods the Maya module cmds provides, you can access this website either by clicking here or by going here in Maya.
From there, you'll find all the possible methods that Maya has to offer.
By visiting this website, you can discover the various parameters and their functionalities that you can use with the methods. At the bottom, you'll also find examples related to that method.
You shouldn't underestimate this documentation; it will be a tremendous help to you.
4. Discovering tools
Now, for the rest of the course, we're going to simulate a need as an example. For instance, let's say we need to create a group and parent the selected object to it every time we select an object. We'll even go as far as renaming the group with the name of the selected object.
​
In this example, we'll use the following methods: cmds.ls(), cmds.createNode(), cmds.rename(), and cmds.parent().
I'll detail step by step what these methods do and how they work.
​
cmds.ls() will allow us to retrieve information about all the selected objects we have in the scene. If we don't provide any parameters to the method, it will by default return all the objects present in the scene as a list.
Indeed, we can see that Maya will return all the names of the nodes present in the Maya scene.
​
We can specifically request to retrieve only the objects we have selected by using the sl=True parameter. We can easily find this in the Maya documentation right here. Moreover, we can see that sl takes a Boolean argument.
If we go back to Maya and test this now, you'll see that it returns the object I selected, which is the sphere. What you need to know is that three-quarters of Maya methods will return lists with the names of the nodes, even if there's only one element. It will still be a list.
That's how we can retrieve the selected node and its name.
​
cmds.createNode() allows you to create any possible node in Maya. For example, if we go back to what I said above about creating a sphere or a cube, I used cmds.polyCube() or cmds.polySphere(). These methods work, but the createNode() method allows you to avoid having to know many methods. What you need to know is that if you write the name of a node as an argument, you create that specific node. (However, please note that createNode() won't work for meshes.)
What's important to know is that even with these methods, they can return information. In this case, they return the names of the created nodes.
cmds.rename() allows us to rename a node.
For this, there are two ways to work with it. First, you provide the name of the node you want to change, and then as the second parameter, you provide the new name.
Alternatively, if an object is already selected, we don't need the first parameter; we only need the new name.
cmds.parent() is the last method we'll cover. This method allows us to parent nodes together. It takes the name of the object we want to parent as the first parameter, and the second parameter is the name of the target object.