One of the first things to check when a (python) script is not loading or importing properly is your *pythonpath* environment variable. This is because Python modules are VERY dependent on path location. So if you ever get an error that says something like this:
import myModule # Error: No module named myModule # Traceback (most recent call last): # File "<maya console>", line 1, in <module> # ImportError: No module named myModule #
myModule() # Error: name 'myModule' is not defined # Traceback (most recent call last): # File "<maya console>", line 1, in <module> # NameError: name 'myModule' is not defined #
then you better check your pythonpaths by using simple code like this:
import sys for pythonPath in sys.path: print pythonPath
You’ll need to do this so much, especially in your Maya session, that you may well want to make a little method and attach a hotkey to it and or put it in a tools menu. While you’re at it, go ahead and list the rest of the environment variables you’ll need to diagnose scripting issues with some code like this:
# replace the ";" with ":" for OSX # or better yet determine your system and # automatically do it. I'll leave that up to you. # (hint: try os.name) import sys from pymel.all import * def getEnvironment(): scriptPaths = mel.getenv("MAYA_SCRIPT_PATH") plugInPaths = mel.getenv("MAYA_PLUG_IN_PATH") pythonPaths = mel.getenv("PYTHONPATH") iconPaths = mel.getenv("XBMLANGPATH") pathPaths = mel.getenv("PATH") sysPaths = sys.path allScriptPaths = scriptPaths.split(";") print "\nMAYA_SCRIPT_PATHs are:" for scriptPath in allScriptPaths: print scriptPath allPlugInPaths = plugInPaths.split(";") print "\nMAYA_PLUG_IN_PATHs are:" for plugInPath in allPlugInPaths: print plugInPath allPythonPaths = pythonPaths.split(";") print "\nPYTHONPATHs are:" for pythonPath in allPythonPaths: print pythonPath allIconPaths = iconPaths.split(";") print "\nXBMLANGPATHs are:" for iconPath in allIconPaths: print iconPath allPathPaths = pathPaths.split(";") print "\nPATHs are:" for pathPath in allPathPaths: print pathPath print "\nsys.paths are:" for sysPath in sysPaths: print sysPath
The PyMEL import is to take advantage of the handy ‘mel.’ command which lets you call the mel-only “getenv” command as a python method.
It’s worth the effort to put this together as you’ll soon find you’ve run it for the 20th time in the last week, and you’ll be on one of your artists’ machines trying to figure our why the script won’t run for her. The first thing you should do is confirm that her environment is what you think it is by calling this simple method.
Hope it makes your debugging life just slightly less painful. 🙂