Scripting Maya with Python #1: Third Party Packages

So you want to write an awesome (Autodesk) Maya plugin.  You know Maya supports scripting with Python and you want to add some advanced features to your Maya workflow.

Maya’s Python implementation is designed as a scripting interface.  As such it completely lacks package management (no pip).

So lets say you want to make some network calls from inside of your Maya Plugin.  However, the ubiquitous python “requests” package is not available in the Maya Python installation.  We cannot install it using pip because that is missing too!  Lucky with some basic knowledge of Python packages we can work around this limitation.

The solution that probably fits with most use cases is simply downloading the desired third party package (using pip on your local machine or by visiting pypy.org).  Make sure it works with the specific version of Python used by your version of Maya (mine uses 2.7.11).  The downloaded package can then be copied into a folder that Maya Python recognizes.  I have been using /Documents/maya/scripts/ for my plugin location.  Once the package has been copied, you should be able to import it from a Maya python script.

If you use multiple third party packages, your /Documents/maya/scripts/ directory may get pretty full and hard to use.  We can solve this too!  We can create a special /Documents/maya/scripts/userSetup.py file.  This file is run automatically by Maya on startup.  Inside of it we can add:

import sys
sys.path.append("yourpackagedirectory")

Then we can create that package directory and move all of the packages inside of it.  By appending the package directory to the Python path, python will check that directory for the target of import statements.  The packages will be more organized and your plugin code will be easier to find!

 

Leave a Reply

Up ↑

Discover more from Max Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading