Running Python Scripts in Maya

Multiple-thousands of downloads and counting of both jbUDKtools and jbUnityTools each…

…and by far, the majority of support questions I receive after people have downloaded these tools have to do with getting them running in Maya. While this is incredibly simple to do, it can seem complicated the first time around.  Here is a brief guide on running Python scripts in Maya with some additional information that I hope demystifies the process.

The Python file:

What is a Python “file”?  Python files are text files in the .py or .pyc file format.  .py files are Python source code files.  .pyc files are compiled code (bytecode) generated from .py source files. The first time you run a .py file, a .pyc file will be created. The next time you run or import the same .py file, if a .pyc exists it will be used instead of the .py file.  For the purposes of using these files in Maya:  a .py file will allow you to view and edit the source code (providing you’re not in violation of the license associated with the code!)  A .pyc file is compiled and you cannot edit the source code. Both files are loaded and run in Maya the same way.

Where to put the Python files:

We only need a couple of lines of code to run a Python file from Maya but before we attempt this we need to make sure the Python file(s) are in the correct location: a valid Maya Python path. There are ways to set custom Maya Python paths but most users opt for the default paths which are different depending on your OS:

Windows: <drive>:\Documents and Settings\<username>\My Documents\maya\<Version>\scripts
Mac OS X: ~/Library/Preferences/Autodesk/maya/<version>/scripts
Linux: ~/maya/<version>/scripts

These are the primary and default Maya Python paths. Put your .py and/or .pyc files in these locations.  Please note that there is a secondary Python path: a ‘scripts’ folder before the <Version>.  Use this if you prefer — I do so when I don’t have version dependent scripts just be aware that all installed versions of Maya will read Python scripts from this folder! If you develop a lot of tools for Maya and manage multiple compiled and source versions of scripts for several version of Maya this can lead to confusion.

Hey! Can’t find the Library folder in OS X?  The kind people at Apple have your best interests in mind (decided you weren’t smart enough to not break things on your own computer) and hid it from you. There are several ways to access the Library folder, the easiest is to hold down ‘Option’ while hovering over the ‘Go’ drop-down menu in Finder — the Library menu will appear. Magic.  Not.

 Running the Python Files:

Once you have the Python file in the correct location, you need to do two things:  1)  Import the .py or .pyc file into Maya and 2) Execute a function from the file.  Whether you’re a Python programmer or not, at this point it probably makes sense to stop thinking of the .py or .pyc file as a ‘file’ and consider it instead as a ‘module’. A Python module is any file containing Python functions or statements.  Importing a module into Maya gives the user access to all functions in that module.

The easiest place to enter the necessary commands is in the Python tab of the script editor in Maya (the icon in the bottom right of the Maya UI). An example of loading and running a function is (don’t forget that these commands are Python specific and MUST got in the Python tab of the script editor, not the MEL tab)  :

import jbUnityTools

In this case, the import action on the first line loads the Python module into Maya. The action on the second line executes the function jbUnityToolsUI() from the imported Python module called jbUnityTools.  Think of this as:

import moduleName

With this complete and tested (press the little blue arrows from the UI in the script editor to test) you can add a button to the shelf editor in Maya by selecting the text in the script editor and from the ‘File’ menu in the script editor, ‘Save Script to Shelf’.

Reloading a Python Module:

If you’re working on Python code, it’s common to be testing as you write — essential, even!  The obvious problem is, the first time Maya loads the Python module you’re working on it creates a .pyc which will be run next time instead of the .py file. See the problem?  To work around this we can add a ‘reload’ command to our run script in Maya:

import moduleName
reload (moduleName)

While importing and reloading may seem redundant, in effect this guarantees that we are loading (by reloading) the .py file — in this case, the one with our most recent changes.

Best of luck with your projects.