Environment Variables

633 views
Skip to first unread message

stanislav.glazov

unread,
Apr 25, 2008, 9:16:15 AM4/25/08
to python_inside_maya
Hi guys!

I`ve a problem.
I`m writing a scripts, which must start maya in batch mode.
Because my computer has more when one maya version installed, I want
to set a environment variables
only for the script session.

I`ve a script named process.py, which has all function aka module
The script is importeв and the functions from them are called from
another pipeline script wrapper.py.

here is a small code listing from both scripts.

process.py:

def setVariables:
os.putenv('PYTHONPATH', 'c:/maya2008/Python/lib/site-packages/')

def initProcess:
print os.getenv('PYTHONPATH')
import maya.standalone
maya.standalone.initialize()
..bla bla

wrapper.py

def main():
setVariables()
someProcess()

if __name__ == "__main__":
from process import *
main()

If I start wrapper, I get an error message like this:

Traceback (most recent call last):
File "D:\JOBS\pyProject\wrapper.cmd", line 76, in <module>
main()
File "D:\JOBS\pyProject\wrapper.cmd", line 67, in main
s3d_init()
File "D:\JOBS\pyProject\process.py", line 22, in initProcess
import maya.cmds as cmds
ImportError: No module named maya.cmds

print os.getenv('PYTHONPATH') prints None


What is the way to solve a problem?
Thank you in advance!

Sylvain Berger

unread,
Apr 25, 2008, 10:54:30 AM4/25/08
to python_in...@googlegroups.com
mmm, I use maya standalone I have no problems like this.

I set my variables using the maya.env file located in the pref folder of the maya version I use. ([...]\my documents\maya\2008\maya\env)

This might solve the problem, although it might not be what you want.

By the way I never set environment variable in the computer, I always use this maya.env file, this way you can set your PLUG_IN_PATH, MAYA_SCRIPT_PATH, etc per version of maya instead of having them for all the maya version when using the system environment variables

Hope this help somehow

2008/4/25 stanislav.glazov <stanisla...@googlemail.com>:



--
They say, "Evil prevails when good men fail to act." What they ought to say is, "Evil prevails."
Nicolas Cage as Yuri Orlov in Lord of War.

Chris Trimble

unread,
Apr 25, 2008, 1:00:19 PM4/25/08
to python_in...@googlegroups.com
Try:

* When your python script launches, extend os.environ with what you need (MAYA_SCRIPT_PATH, MAYA_PLUG_IN_PATH, etc).  Then launch Mayabatch with the subprocess module and hand in this environment.  This is what we do because it offers the most flexibility -- no user has Maya.env set anywhere, nor do they have permanent environment variables set for Windows.  Everything is set up based on what script they use to launch Maya, so a user can change settings/projects easily.

* I haven't used standalone very much, but in the code below if all you need is to set your PYTHONPATH, just import sys and append to sys.path.  Once you're in Maya standalone, I imagine you could use setenv to set up the rest of the variables you need as well.

HTH,
  Chris



2008/4/25 stanislav.glazov <stanisla...@googlemail.com>:

Chadrik

unread,
Apr 25, 2008, 2:18:49 PM4/25/08
to python_inside_maya

if you are trying to get access to maya.cmds from a python script
which is called from an external interpreter (outside maya) you should
use mayapy, found in the maya bin directory. i assume that you are
already doing this or else the maya standalone calls would not work.
the problem is that when you start maya this way, it does not parse
your Maya.env. pymel makes this much easier: when it is imported in
batch mode, it parses your Maya.env and sets up your environment
variables as they would be in GUI mode. it also handles importing and
intializing maya.standalone -- you just import pymel as you normally
would.

in case anyone else is curious, you can ensure that your script is
using mayapy by:

1) calling it with mayapy:
/usr/autodesk/maya2008/bin/mayapy myScript.py
2) adding a mayapy directive to the first line of your script:
#!/usr/autodesk/maya2008/bin/mayapy

here's a little mayapy demystification. mayapy is just a simple shell
script. this is the contents of mayapy on osx with explanations of
each code segment:

#!/bin/sh

#get the current path where the mayapy script resides
path=`dirname $0`
# set python to use maya's internal copy.
pythonhome=$path/../Frameworks/Python.framework/Versions/Current
export PYTHONHOME=$pythonhome
# setup environment variables necessary for finding frameworks and
libraries needed by maya's modules
export DYLD_LIBRARY_PATH=$path/../MacOS:$DYLD_LIBRARY_PATH
export DYLD_FRAMEWORK_PATH=$path/../Frameworks:$DYLD_FRAMEWORK_PATH
# set maya's location
export MAYA_LOCATION=$path/..
# execute maya's python interpreter
exec $pythonhome/Resources/Python.app/Contents/MacOS/Python "$@"

now python is ready to use maya's modules without having maya open.


-chad



On Apr 25, 10:00 am, "Chris Trimble" <tri...@gmail.com> wrote:
> Try:
>
> * When your python script launches, extend os.environ with what you need
> (MAYA_SCRIPT_PATH, MAYA_PLUG_IN_PATH, etc).  Then launch Mayabatch with the
> subprocess module and hand in this environment.  This is what we do because
> it offers the most flexibility -- no user has Maya.env set anywhere, nor do
> they have permanent environment variables set for Windows.  Everything is
> set up based on what script they use to launch Maya, so a user can change
> settings/projects easily.
>
> * I haven't used standalone very much, but in the code below if all you need
> is to set your PYTHONPATH, just import sys and append to sys.path.  Once
> you're in Maya standalone, I imagine you could use setenv to set up the rest
> of the variables you need as well.
>
> HTH,
>   Chris
>
> 2008/4/25 stanislav.glazov <stanislav.gla...@googlemail.com>:
Reply all
Reply to author
Forward
0 new messages