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:
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:
What are the requirements?
There will be links to everything else you need on the github page.
What am I going to get from this course?
What is the target audience?
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:
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:
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 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:
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:
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:
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.
So you've written out your script, but you want to save it down so you don't lose it? Maybe you want to print it out and frame it? Well let me show you how to do that in this little mini lesson.
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:
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.
The LS command is used to list objects in the scene, and you can give parameters to it to narrow down the list it gives back.
In this video we'll go over how to use the ls command properly to get back just the items we care about.
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:
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:
As usual, the code is available in the resources for this lecture.
So far we've been starting our scripts inside of Maya. This video instead goes over how to create our scripts in PyCharm instead and then access them in Maya.
Now lets create the gear itself!
We'll learn to:
The code for this video is available in the resources.
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
Classes are a way in Python to create a custom object type. Just like we have a string, an integer, a float etc..., we can have our custom object types that define a way to interact with them.
Now that we've learned about how amazing Classes can be (when used judiciously), we can go on to converting our functions into a Gear class.
Code is available in the resources for this lecture.
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:
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:
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.
Now that we have our time and object selection, we can figure out how to calculate the value of the key that we'll be placing.
In this video we'll go over:
With our logic out of the way, lets make our User Interface so we can simply drag a slider to set the value of the inbetween key.
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
This short lesson shows how to add scripts to the Maya shelf so that you can access them easily without using the script editor each time.
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:
Code is available in the resources of this lecture.
Qt has a long history in earning itself a spot as the most popular cross platform UI library around. Seriously, it's amazing.
Anyway I figure it's good to know why it exists, and why we need it, so this lesson goes over exactly that.
Okay so we know what Qt is, and we've used cmds, but I really want to drive home why you should prefer using Qt where ever possible.
"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.
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.
Okay, now we know what Qt is....how do you just get it started? Well first we need to import it and depending on your Maya version this can be different.
This video goes over those differences, so you don't get tripped up when you run into them.
Phew, we have that Qt preamble out of the way, lets get our hands dirty with code.
But we can't start our UI just yet! Remember, we need to make the logic for our code first.
In this video, we'll go over creating the directory to where we'll be saving our controllers.
Now that we have a nice clean directory to save our lovely controllers to, lets go about writing out the code to save our controllers down.
Sweet, our controllers are written to the directory, but now we need to find them so that we can access them.
In this lecture, we'll go over finding files on your computer using Python.
We've saved our controllers, we've found them and now we need to Load Them!
In this lecture, we'll go over loading back in the controllers we saved down.
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!
We may want to write additional data along with our controllers. We can use a file format called JSON which is a very common data storage format to store our data.
This lecture goes over writing out the data to JSON and then reading it back in.
Lets also save down some screenshots of our controller so we can know what it looks like.
Hooray, our logic is done! Can we start the UI now?
Almost, my friend! Before we start the UI, let me show you where you can find help regarding Qt should you get lost.
Yes! Now we can start by showing our window! Never have you seen such a pretty grey box.
In this lecture we'll go over setting up the basis for our user interface, and some common issues that people run into.
We can now start adding interface elements to our window to actually make it useful.
We'll go over adding buttons, textboxes and lists, as well as laying them out in a nice ordered fashion.
Our last video left us with the controllers just being listed out. I want them laid out as a nice gallery of thumbnails though, so in this lecture we'll be going over how to achieve that.
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.
Now that you understand the theory behind Signals and Slots, let's use them to connect up our UI so that it can 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:
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.
This project will have less preamble and we'll be jumping right in!
This lecture covers the following:
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:
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:
In this video we'll continue from where we left off and add the ability to change the color of our light.
We'll go over:
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:
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.
Now that our Light Manager is pretty complete, we want to be able to save our lights down as a JSON file that we can share with other people.
In this lecture we'll go over doing just that.
We've saved down all these light rigs as JSON files, but we need to be able to read them back in to Maya so that we can load back our old lighting setups or use some that other people have built.
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:
Continuing on from our last video, we'll now go over:
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
This course has used PyCharm as its' primary editor, but there are many others out there.
In this video, I'll go over a few of the more popular options.
Sublime text is one of my favorite editors other than PyCharm. It's weird that people have favorite editors, but when you're working in them all day, you kind of develop that.
This lecture goes over setting up Sublime Text for Maya.
Eclipse is also another common editor that can be setup to work with Maya. I'm not it's biggest fan, but you might work in a studio that uses it so I'll leave this little guide to setting it up.
I'm a Lead Pipeline Developer for feature film, with experience on major visual effects films like Guardians of The Galaxy and Suicide Squad, as well as Animated Features like Cloudy With A Chance of Meatballs 2 and Hotel Transylvania 2.
My background is as an animator, layout artist and generalist but I taught myself to program to work faster and eventually ended up shifting roles.
Today, my job involves creating software that is used by hundreds of artists every day, and is critical to production. I use the same techniques I learned to program to teach others to do the same.
Any opinions here obviously are my own and obviously not reflective of any studio I work at.