Python For Maya: Artist Friendly Programming
- 8 hours on-demand video
- 4 articles
- Full lifetime access
- Access on mobile and TV
- Certificate of Completion
Get your team access to 4,000+ top Udemy courses anytime, anywhere.Try Udemy for Business
- Develop tools in Python, inside and outside of Maya.
- Create advanced user interfaces to interact with their tools and manage assets.
- Have the skills necessary for a studio technical director.
Welcome to Python For Maya: Artist Friendly Programming
I'm happy you've chosen this course! If you have any feedback or questions, please leave it in the discussions :)
This is just my introduction video to the course. It's the same as the promo video, so feel free to skip it to continue on to the next few videos if you've already seen it.
Hi, welcome to Python for Maya!
Check out my github page in the resources for this lecture
On there you can find:
- Code for our projects with comments that you can follow along with
- Links to the software we'll be using
- Up to date resources to continue your learning outside of this course
I'll keep this page up to date so that it can be your one stop shop for everything that is needed to supplement this course!
With that out of the way, let's get started!
The script editor is something that can be elusive to people even after years of using Maya, but for our course it will of course be one of our best friends.
It is split into two parts:
- The Top Section shows you feedback from Maya, including what commands are being run when you click buttons or any errors that happen
- The Bottom Section is where you can enter your scripts, with the option of which language to use. Of course with our projects we'll be sticking to Python.
It can be invaluable to figuring out how to do things, because if you're stuck you can do the action manually and it tells you exactly what Maya is doing.
Many developers start off by piecing what is happening in the script editor. I'll go over that briefly in our videos, because it is a useful resource, but we'll try not to let it be a crutch for us.
You can execute scripts in two ways inside the script editor.
- If you click the double blue arrow in the script editor toolbar, it will run anything you have either selected or the whole script.
- If you select some code, you can then hit ctrl+enter (cmd+enter on macOS) to execute just that.
If you select a variable and run it, Maya will print out the variable. This is the same as doing print variable. This only happens if you select the variable alone. If it's run as part of a larger script, you'll need to be explicit and use print.
Our first line of Python!
This may seem really basic but Hello World is one of the most important lines of code you can write.
It shows that Python is working for you, and that you aren't encountering any errors in your system.
Most importantly though, it shows how easy it is to use Python.
You can find annotated code in the resources for this lecture.
Note: This lesson is short just to get you into Python. Longer videos are to follow! Don't despair. :)
Let's kick it up a notch! Printing names isn't fun, or maybe it is for someone, but let's move on anyway !
The first thing I do whenever I start any 3D application is to create a cube. That is the simplest action that any 3D application should be able to do.
Similarly, it's the simplest thing I should be able to do with Python. So let's try it.
Note: Another short lesson, but the next one will be meatier.
Now this is the real meat of this first project! We'll be creating a animation prop rig using our little companion cube!
We'll go over some key concepts:
- Getting values back from Maya commands
- Storing these values in variables
- Chaining Maya commands together to build our scripts
- Accessing items inside a list
You can find commented code for it in the resources for this lecture.
We've had our first encounter with variables in the last video, but lets get down and understand them.
This video goes over variables and their types.
Variables can have types which is basically a way of saying what kind of object does this variable refer to. Is it a number, a word, a list or something else completely?
Maya has a few different programming languages that it supports, and each programming language in turn has access to a few different programming libraries inside of Maya.
Programming Languages are literally the language we use to program. Just like some of us speak English, Spanish, Esperanto or Hindi (not me though), Maya can speak these different languages:
- Python (Duh otherwise this course wouldn't exist)
- MEL (The Maya Expression Language)
- C# (Only on Windows)
These are the ways the languages can interact with Maya. In English we may say: "Take the elevator" but someone else may say "Take the lift", Maya has multiple ways for the languages to give it directions.
The libraries inside Maya are:
- cmds : Maya's commands library. Can only be used by MEL and Python.
- OpenMaya: The Maya API that almost all Maya commands are built around. Very deep access but also complex. Can be used by Python, C++ and C#.
- OpenMaya 2: OpenMaya 1 was originally designed for C++ which makes it a bit cumbersome in Python. OpenMaya2 is the new Python version, but still in progress. Can only be used in Python
- PyMel: cmds and OpenMaya were designed for Mel and C++ respectively. PyMel combines both of them to give a very Python friendly library to use. Can only be used in Python.
So you can see, Python gives us the most flexibility of all the languages. This comes at certain costs, but makes it the clear winner for our course.
Many Maya users don't know how Maya constructs objects and object relationships under the hood, but Nodes are a very important concept in Maya and one you should be familiar with before writing more code.
This is a short overview video to describe how nodes work. We won't really be dealing with them much directly, but still great info to have.
There are two major versions of Python: Python 2 and Python 3.
You'd think that Maya would use the latest version, but the vfxplatform.com has chosen to stay on Python 2. There are several reasons for this, but I'd like to go over them now so you aren't confused when you see websites recommending Python 3 instead.
To be clear: If you're doing Maya development, you will be using Python 2. For anything else, feel free to use Python 3.
This is our first real project.
We'll be creating a script that renames objects in our scenes by appending a suffix to them based on their object type.
In this project we'll learn:
- How to get help for our Maya commands
- How to write If statements as well as If/Elif/Else statements to control our logic
- How to use For Loops to loop through lists
- How to setup and use PyCharm
- Create Functions to contain our code
- Learn to use Dictionaries
- Use string formatting to create complex strings
- Learn about scopes in Python
It may seem like a lot, but each concept is broken down into bite sized pieces to go over them.
You can find code for it in the resources for this lecture.
No developer comes into this world magically knowing every command that Maya has. Well maybe someone does, but it would be an odd skill for a baby to have.
So instead, for the rest of us mere mortals, there's some pretty good documentation about each Maya command and we'll go over how to look up and read this documentation.
If Statements are powerful ways to control our logic so that we only run parts of our code when certain conditions are met.
For example, we wouldn't start a party without some sweet tunes, so in Python we could tell it to only do it if the music is there.
An important thing to note here is that Python handles two different kinds of indentation:
- 4 spaces
- 1 Tab
Spaces are always preferred. Anyone who says otherwise is a loony. Sometimes you will be changing someone elses code that uses tabs instead of spaces. That is the only time I can recommend tabs, but I also recommend cursing at them.
Fortunately, PyCharm by default converts tabs to spaces, so you won't be hitting a space bar multiple times. Just hit the Tab key once, and it will add 4 spaces. It is also smart enough to understand the current code, and do the best behavior accordingly.
IMPORTANT: Never mix tabs and spaces. It's a world of pain. Use one or the other, and most good editors will let you use tabs, but convert it to spaces for you.
For Loops are another core programming principle. They let us run the same logic for every item inside of a collection (lists, dictionaries, sets, tuples etc..).
Noone wants to write the same code for every single item in a list, especially when some lists can be thousands or millions of items long. So we use a for loop to tell python to run it on each item sequentially.
While Loops are like the child of the if statement and for loop.
They run as long as a condition is met. These are useful when you don't know exactly how many loops you need to run, but you know that when the condition is met, that you can stop.
Think of looking for your keys. You don't know how many times you'll walk around the house looking for them, but once you find them you know you can stop.
Often, we need to know the type of the object in Maya. Are we dealing with a joint, a mesh or something else entirely? This is probably one of the most common things to do in Maya and in this lesson we'll go over how to find what kind of object you have.
The desired behavior is that if an object has no other children other than it's own shape, then we'll give it a suffix that represents it's shape type, but if an object has multiple children, we'll treat it as if it's a group. This also applies if a geometry object has children because it has a mesh shape as a child as well.
You can find the source code link in the resources.
In case you do want to always rename an object by it's shape type, then please make the following change:
# Add the shapes=True to only get its shape children shapes= cmds.listRelatives(obj, children=True, shapes=True) or  # If there were shape children, get the type of the shapes if shapes: objType = cmds.objectType(shapes) else: # otherwise get the object type of the object itself objType = cmds.objectType(obj
If Statements are cool, but what if you have multiple conditions you want to check for? Or if you want to tell Python to do something if the condition isn't met?
Like if we can't start the party then lets go to sleep. Similarly we can tell Python what to do quite easily when different conditions occur.
NOTE: There is a bug in the latest version of PyCharm that doesn't let you set mayapy.exe as the Python interpreter. Fortunately it's not completely necessary and you can skip that part, and simply add the completion files as shown in this video. I will file a bug report with Jetbrains to get this sorted.
So as much as I like the Maya script editor, it's kind of a drag when your script becomes larger than a few lines of code.
This is where PyCharm comes to the rescue. Not only is it great for large code, it also gives you ton of help while coding. It's a programmers best friend.
In this lesson, we'll go over the proper setup of PyCharm for Maya.
In this lesson we'll go over containing our code within functions for easy use.
Functions are another core principle of programming. They're basically commands that you're telling the computer to run, and they can take optional details called arguments or parameters.
For example, you ask Dave to get you apples, but you can also tell him how many apples to get. The getting of the apple is a function, and how many is the parameter.
We'll now go over Dictionaries which are a type of object in Python used to store relationships.
A real world Dictionary (think Oxfords Dictionary) stores the relationship between a word and it's definition. Similarly Python dictionaries store the relationship of a key and a value, where the word is the key and the definition is the value.
Python has many, many, many ways to combine words together to make sentences or other words. This is called String Formatting and it's probably the thing I spend the most time doing at work.
There are multiple ways to format strings in Python and I'll go over all the ones that work in Python 2. There are actually more ways in Python 3.6, but we're limited to Python 2.7 for Maya
Variables in Python have a scope. Scopes are the life span of a variable, and when they reach the end of their scope they are cleaned up.
A real world analogy would be of a candy bar. When you are done eating it for desert, you throw it away. In this case, the candy bar is the variable and the desert is the scope. You can't use the candy bar before desert or your mom will get mad at you, and once it's thrown away you don't want to be sorting through the garbage.
The Gear Creator project will teach us the following skills:
- Creating geometry using Python
- Modifying the geometry using Python
- Creating our own object types in Python, called Classes
- Converting from functions to classes
As usual, the code is available in the resources for this lecture.
So we've got a gear, but the director wants to change all the pretty gears we've worked so hard to make.
We could delete them all, OR, we could modify them to match his new visionary idea for more teeth on the gears.
In this lesson we'll go over modifying the gears we created.
Code is here: https://github.com/dgovil/PythonForMayaSamples/blob/master/gearCreator/gears1.py
Calling our scripts from the script editor is alright, but noone wants to be typing commands all the time. This isn't the 60s anymore. So lets make a User Interface for our tools!
We'll be making an Animation Tweener that adds inbetween keys to existing animation.
In this project we'll learn:
- How to interact with time in Maya and animation controls
- How to make a user interface with Maya cmds
- How to connect parts of an interface to functions
- How to reuse our interface to make multiple other ones so we don't have to type the same code
Code for this project is available in the resources of this lecture.
Maya has a few different libraries we can use for making user interfaces. Largely they all now use the same library under the hood (Qt) but these are the 4 different ways to access it:
- Maya cmds has a subset of Qt that you can use from Python or Mel.
- Qt is a C++ library that you can you use from C++
- PyQt and PySide are Python wrappers for Qt so that we can use Qt from within Python directly.
This project will use Maya cmds, since a lot of maya code uses it, but the next few will switch over to PyQt/PySide instead.
Before we get into writing our UI, it's always important to write the logic for it first. This lets us test the logic without the UI to see if it's working on its' own, but also it means that other tools can use our functions without needing to use our UI.
So for that reason, we'll start our project by writing the Tween Function to add keys inbetween other keys.
This is the first of two videos in which we'll be getting the time and object selection from Maya so we have the necessary information to perform our tween.
It's a lot of work to create a user interface, and we have to repeat that each and every time we make one. If only there was a way to reuse our code! The answer is : Classes!
In this lesson we'll go over taking the code from our TweenerUI and sharing it with a new UI for our Gear Creator
In the last project we learned to make UIs with the Maya cmds library. In this project, we'll switch to using Qt instead because it lets us have way more flexibility in how our UIs are made.
This project will show you:
- The advantage of Qt over Cmds
- Writing Qt code that can work across Maya versions
- Using Qt.py: A wrapper for the multiple Qt libraries in Python
- Interacting with the OS to make directories, and save and read files
- Creating screenshots
- Making a gallery UI that lets us select what controller to load in
- Using Signals and Slots in Qt to connect our UI to functions.
Code is available in the resources of this lecture.
"If it walks like a duck, sounds like a duck then it must be a...."
This is a bit of a digression. I've mentioned Duck typing and it isn't about hitting your keyboard like a duck (How would a duck type anyway?).
Duck Typing is a concept in many programming languages that support Classes, where different classes can behave the same so you can use them in a similar manner without having to know what it is.
Whereas cmds requires you to know what type each UI element is in case you need to modify it, Qt objects let you modify them directly without needing to know what they are.
If you're writing Qt interfaces for Maya, you have a problem today. There are 4 different ways to use Qt, and neither is a clear winner.
- PySide comes with Maya 2011 - Maya 2016 and uses Qt4
- PyQt4 also uses Qt4 and is often nicer than PySide but doesn't come with Maya.
- PySide2 comes with Maya 2017 and above and uses Qt5
- PyQt5 again uses Qt5, but doesn't come with Maya but is nicer than PySide2 to use.
Additionally each of them do things slightly differently. It can be quite hard to support them all. Or Not.
There's a library called Qt.py that lets you just use it, and it takes care of all the differences for you so that you can sleep peacefully at night knowing your code is safe and sound.
A link to Qt.py is available in the resources for this lecture.
NOTE: In the video I say to use the direct download from Github. Instead, use the releases page: https://github.com/mottosso/Qt.py/releases
This will give you a more stable release than the one from the main page. I'll reupload the video soon with the changes, but just want to make that note here first.
Uh Oh! We have a bug in our code! But don't fret, this is super common. I have bugs in my code all the time, it's like a developers right of passage. Heck, some of us have even brought down whole studios with a simple bug (not me though...I swear! )
So lets find our bug, squash it and carry on with our lives!
In this short lecture, I'll introduce you to the concept of Signals and Slots in Qt. These are the backbone of how Qt interfaces actually work.
Signals are events that an interface element will say has happened. For example, when a button is clicked, it sends out a signal saying that an event has happened where it has been clicked.
Slots are the other side of a signal. You connect slots to a signal, so that Qt automatically runs that slot when the signal is emitted. Slots are just a fancy name for a function.
The Light Manager
In this project we'll make a UI to create, manage and edit lights in our scenes. It will also write out the lights and read them back in using just JSON.
We'll learn the following skills:
- Using PyMel and when to use PyMel and when to use Maya cmds
- Making our own custom interface controls in Qt
- Using the OpenMaya API to combine Maya controls with our Qt controls
- Using Anonymous functions with Partial and Lambda
- Using Loggers
- Writing portable code that can be used in any version of Maya
Code is available in the resources for this lecture.
PyMel is another Python library inside Maya.
It can really help clean up your code and make Maya a lot easier to use. It does have several drawbacks in terms of speed and stability so several studios don't allow its use, but it is very useful otherwise and I think it's very useful to learn it.
We need a custom user interface that does more than the built in Qt ones do. Fortunately Qt lets us build our own using other Qt elements.
This lecture covers:
- Creating a custom Qt Widget for our needs by combining other ones together
- Using lambda which is a way to define a single line function without a name.
Often we'll use lambdas for when we don't want to create a whole function because we won't be using it more than the one time, and they're simple by nature.
NOTE: In the video I forget to add the second "QtWidgets.QSizePolicy.Maximum" parameter for the vertical value of "scrollWidget.setSizePolicy". It should be scrollWidget.setSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Maximum)
See line 188 here: https://github.com/dgovil/PythonForMayaSamples/blob/master/lightManager/lightManager.py#L188
This lecture is a continuation of the last one as we finish up the behavior of our custom widget.
This video will cover:
- Creating buttons to hide the lights
- Creating a custom Qt Signal to tell other lights to hide instead
- Making a slider to control our light intensity
This lecture covers two important topics:
Unfortunately, like we learned earlier, the different Qt libraries have some differences, and while Qt.py tries its best to handle them all, there are a few it can't.
This lecture goes over writing code that can work across all Qt libraries.
You likely won't need this, and likely have chosen to stick with just one. Still, I cannot predict which one you chose as it depends on your setup and Maya version, so this video will cover all of them so you can pick the bits you need.
Python has a great way of controlling what information gets reported by using loggers. These are like print statements on steroids that you can enable and disable across your code very easily.
This lecture goes over combining Maya interface controls with our own Qt controls.
We'll go over:
- How to parent our UI under Maya directly so we don't need to store it in a variable
- How to make our UI dockable
Note: If you're using Maya 2016 or below, the portion of this lecture that handles the docking uses a different Maya control than in 2017 and above. I have the code here: https://github.com/dgovil/PythonForMayaSamples/blob/master/lightManager/lightManager2016Below.py
The sections that are different are in the LightingManager's __init__ method, the getDock and deleteDock functions. They are marked with MAYA2016 so should be easy to find.
If you find that at all confusing, or get stuck, ask a question in the Q&A section and I'll respond as soon as I can.
Another Python fundamental is Try and Except. This concept lets us say that we should try something out, and if it doesn't work, then do something else.
For example if you try to eat a pie and it's too hot, you'll instead go and drink water rather than stop and be in pain.
Python isn't just useful inside of Maya, in fact it is super useful outside of it. To show that, we'll be creating a nice little file renaming utility.
This lecture goes over:
- Running scripts in the command line
- Creating an argument parser to understand the paremeters we are giving our script
While we've covered several of the most common libraries you will be using, we haven't been able to fit in two other common libraries.
Sys is a module in python that lets you access system information, including the version of Python being run, what modules are loaded and where Python will look for other modules.
Subprocess lets you launch external applications from Python. This is really useful for example to call preexisting applications rather than having to redevelop your task in Python, or just launching an application for your user.
e.g. We can launch an image editor to edit the screenshots we save out in the Controller Library project
- You will require a basic knowledge of Autodesk Maya, like creating and manipulating simple objects.
- You will need a working copy of Maya. Preferably 2017 and above, but 2011 should be the minimum. Links to the trial and educational version will be provided.
- No programming knowledge is required, but a basic understanding will help.
Welcome to Python for Maya: Artist Friendly Programming!
This course will take you from your very first line of Python code to creating advanced user interfaces with all that fluidity we expect of a professional. You'll learn skills that will help you work faster by automating repetitive tasks so you can focus on the work you want to be doing. This can in turn also add incredible value to you on the job market and help you move up the ladder faster.
Whether you're a beginner or already familiar with Python, there's a lot this course can offer you. Projects are split up by difficulty and there's a project for you whether you're a modeller, animator, rigger or lighter.
Here's what we'll cover:
- Python Fundamentals: functions, classes, if statements, etc...
- Advanced Python concepts like lambdas,partials, regular expressions, etc..
- Writing User Interfaces with Maya Cmds and Qt
- Writing data to disk and loading it back in to Maya as JSON
- Creating command line tools
- Setting up a professional workflow
- The Qt interface framework: custom widgets, signals and slots, stylesheets etc
Additionally, this is currently the only course that covers changes introduced in Maya 2017.
You'll have full source code access with comprehensive comments to follow along with, as well as other resources that will help you learn when you're done.
I taught myself Python several years ago when I was an artist, and today hundreds of artists use my tools everyday. I'll be using the same project driven methods to teach you Python.
Ready to start your programming journey? Let's go!
The most important resource is:
- Links to all the software you'll need.
- Source code for all our projects with comments.
- Other resources to further your education.
What are the requirements?
- Maya 2011 or higher (2017 preferred).
- A computer with an internet connection
There will be links to everything else you need on the github page.
What am I going to get from this course?
- You'll have the skills to build tools that will speed up your work.
- The necessary workflow to build code that can be shared with your team or studio.
- The necessary skill set that we'd ask of a pipeline developer in major studios.
What is the target audience?
- Beginners with no programming experience
- Intermediates with some experience looking to hone their skills
- This course is for artists who want to learn Python programming to make tools, work more efficiently or add job value.
- The course is targeted to beginners with no programming experience as well as intermediate's who want to learn new skills.