Maya Programming with Python Cookbook
上QQ阅读APP看书,第一时间看更新

Adding custom folders to your script path

In order to write reusable scripts, you'll want to save your scripts to external files, and in order to do that, you'll need to make sure that Maya knows where they are.

How to do it...

Maya maintains a list of locations to search for scripts, used when you use the import (Python) or source (MEL) commands. If you want to see the full list, you can do so with the following code:

import sys

pathList = sys.path
for path in pathList:
    print(path)

This will provide you with a list of paths, including the following:

/Users/[username]/Library/Preferences/Autodesk/maya/[maya version]/prefs/scripts
/Users/[username]/Library/Preferences/Autodesk/maya/[maya version]/scripts
/Users/[username]/Library/Preferences/Autodesk/maya/scripts

Saving your scripts to any of those folders will allow Maya to find them. If you're fine with saving all of your scripts to one of those folders, that's totally fine.

However, you might want to add additional folders to the list. For example, you might want to save your scripts in a folder within the my Documents directory, maybe something like:

/Users/adrian/Documents/MayaScripting/examples/

Note that this is a typical example on Macintosh. On a Windows machine, it would look more like:

\Documents and Settings\[username]\MyDocuments\MayaScripting\examples

Either way, we'll need to tell Maya to look there to find scripts to execute. There are a few ways we could do it, but in keeping with the theme of the book, we'll do it using Python.

Run the following code in the script editor:

import sys
sys.path.append('/Users/adrian/Documents/MayaScripting/examples')

This is done by replacing /Users/adrian/Documents/MayaScripting/examples with whatever folder you want to use.

Once you've done this, you'll be able to import scripts stored in that directory as well. However, having to enter the preceding code every time you launch Maya would be quite frustrating. Luckily, Maya provides a way for us to execute custom Python code on a startup.

To make the code execute every time Maya opens, save it to a file named userSetup.py and save it to the following location for a Mac:

~/Library/Preferences/Autodesk/maya/<version>/scripts

Or for Windows, you can save it to:

<drive>:\Documents and Settings\<username>\My Documents\maya\<version>\scripts

How it works...

All of the code contained in userSetup.py will be run every time Maya starts up.

While the base list of paths that Maya will search for scripts is not altered by the above, it will cause Maya to add your custom folder to the list every time it starts up, which in practice amounts to the same thing.

There's more...

You can also add to your paths by creating the Maya.env file and saving it to the following location (on a Mac machine):

/Users/<username>/Library/Preferences/Autodesk/maya/version 

or

/Users/<username>/Library/Preferences/Autodesk/maya

On a Windows machine, save it to:

drive:\Documents and Settings\username\My Documents\maya\version 

or

drive:\Documents and Settings\username\My Documents\maya

For the specifics of the Maya.env file syntax, consult Maya's documentation. However, editing Maya.env can lead to crashes and system instability if you're not careful, so I recommend relying on userSetup.py instead.