官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 和田市| 应用必备| 托克逊县| 彭山县| 枣强县| 江油市| 麻江县| 红河县| 盐源县| 平舆县| 固始县| 岐山县| 海城市| 阳山县| 金乡县| 渝北区| 上思县| 泰宁县| 定襄县| 乡城县| 蒙山县| 白沙| 汉川市| 隆回县| 阆中市| 松原市| 萨嘎县| 含山县| 龙里县| 溆浦县| 北流市| 万安县| 莲花县| 习水县| 任丘市| 图木舒克市| 北安市| 石棉县| 双城市| 定远县| 东源县|