- Maya Programming with Python Cookbook
- Adrian Herbez
- 476字
- 2021-07-14 10:46:42
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.
- Angular UI Development with PrimeNG
- WebAssembly實戰(zhàn)
- Microsoft Dynamics 365 Extensions Cookbook
- C++面向?qū)ο蟪绦蛟O(shè)計(微課版)
- 算法基礎(chǔ):打開程序設(shè)計之門
- C程序設(shè)計案例教程
- 基于Swift語言的iOS App 商業(yè)實戰(zhàn)教程
- Java EE核心技術(shù)與應(yīng)用
- Java Web開發(fā)詳解
- Principles of Strategic Data Science
- Kotlin Programming By Example
- Simulation for Data Science with R
- PowerDesigner 16 從入門到精通
- Mastering Machine Learning with R
- Qt 5.12實戰(zhàn)