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

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.

主站蜘蛛池模板: 北京市| 兖州市| 永靖县| 舟曲县| 上思县| 仙桃市| 白河县| 万源市| 大英县| 化德县| 揭阳市| 临江市| 定日县| 湖口县| 鄂托克旗| 绥江县| 北票市| 西林县| 册亨县| 扎赉特旗| 社旗县| 汪清县| 信阳市| 澜沧| 黄浦区| 西林县| 旺苍县| 开阳县| 清原| 文成县| 当阳市| 永春县| 分宜县| 化州市| 金堂县| 皋兰县| 高邮市| 惠东县| 吐鲁番市| 武鸣县| 永春县|