Maya.env Configuration of Variables Using userSetup.mel and userSetup.py

maya2011_thumbuserSetup.mel, userSetup.py Tutorial Part 2: Theory and Practical Use

For the Advanced user:

     

Delving into what how userSetup files work can be tedious.  That’s why i’m posting how to configure Maya properly for those people who are perpetually scratching their heads, trying to figure out what way is better or why things aren’t working out like they planned.  Check out my previous post on userSetup and see if you are pondering things after reading all of that.  Of course, i’ve learned some things about the process too since then because my startup process is a lot more complicated than sourcing a couple of tools.  I’m setting up whole programming environments!  Learning Maya startup configuration has been a process of trial and error, searching and finding or sometimes not finding.  Here’s the beef.  Maya loads script paths in a specific order.  Once you learn that order, the daunting task of setting configurations for a whole studio is as simple as pie.  I work at a smaller studio, so the process of discovery falls on my shoulders and so I had to rise to the challenge.

     

Case Study: Finding the Solution to a Problem with userSetup.py


So where do you find the source code to decipher what’s really going on when Maya starts up?  After finding this relic of a thread online, It crystallized one piece of the puzzle.  I was having the hardest time figuring out why PYTHONPATH wasn’t working for all my scripts.  After all, I was pointing it to find my userSetup.py and also my Maya2011 python\libs\site-packages directory so I can have access to all my downloaded modules yet my PyQt modules were failing to import while my userSetup modules were sourcing just fine.

Here's the order of how Maya finds scripts for python taken from the post
linked above, which is directly from module: maya.app.startup.basic

Per-version prefs scripts dir (eg .../maya8.5/prefs/scripts)
Per-version scripts dir (eg .../maya8.5/scripts)
User application dir (eg .../maya/scripts)
Look for userSetup.py in the search path and execute it in the
"__main__" namespace
Set up sys.path to include Maya-specific user script directories.
Set up auto-load stubs for Maya commands implemented in libraries
which are not yet loaded
Run the user's userSetup.py if it exists
Register code to be run on exit

Annoyingly, Maya2011(SAP) is a completely new installation.  It was as if 2011 was a fake release,  Hah!  Fooled you!  says Autodesk, but I won’t succumb to their will and resist re-installing python modules all over again.  This is why I decide to continue with the alternate solution of using the userSetup file to my advantage.

Back to the point, I’m setting PYTHONPATH on the system level along with MAYA_SCRIPT_PATH with a .bat file that I compiled into an exe.  This saves me the extra work of opening Windows Control Panel every bloody time I want to switch Maya versions or setups.  I store lots of system env configurations this way now after learning that other big studios do the same, yet another piece of the puzzle materialized.  Traversing from work to home gets a lot easier for me as well.

The problem is still unsolved though.  Why is it when I try to import PyQT4 modules in Maya now do I get an import error such as “No module named PyQt4.QtCore” ?  I thought PYTHONPATH would surely take care of that.  Well, the answer is that unlike MAYA_SCRIPTS_PATH only the FIRST path in PYTHONPATH gets added to the list of user scripts paths when (at least my) Maya starts up.  It doesn’t make sense because from the list above, all of the paths in PYTHONPATH should be added before running userSetup.py.  That means all other paths have to be inside the userSetup.py that i’ve generated.  I put site-packages path inside that and everything works as it should, however the logic doesn’t compute.  So I dig deeper…

     

Troubleshooting


Doing a quick test inside Maya:

import sys
for path in sys.path: print path

Actually just executing this does the same thing but not as pretty.

import sys
sys.path

   

I find a peculiar path in the list, and it’s definitely not one but two paths:

# Result : N:\Scripts\Maya\Startup\bat C:\Program Files\Autodesk\Maya2011\Python\Lib\site-packages #

   

How on Earth did this get added like this?
The string that I use to create the sys path in the .bat file:

set PYTHONPATH=N:\Scripts\Maya\Python\userSetup_configs\MBPRO_JA;
C:\Program Files\Autodesk\Maya2011\Python\Lib\site-packages

   

I found on Python.org to set environment variables:

set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib

   

What is this? %PYTHONPATH%?  So add it like this:

set PYTHONPATH=%PYTHONPATH%;N:\Scripts\Maya\Python\userSetup_configs
\MBPRO_JA;C:\Program Files\Autodesk\Maya2011\Python\Lib\site-packages

It turns out that after learning about windows environment variables this makes a lot more sense, but not sure why it was failing in the first place sense %PYTHONPATH% was empty in the first place.

   

Lo and behold, I do the test again:

for path in sys.path: print path
#Result: N:\Scripts\Maya\Startup\bat
N:\Scripts\Maya\Python\userSetup_configs\MBPRO_JA
C:\Program Files\Autodesk\Maya2011\Python\Lib\site-packages #

Everything works! :) So using the list and by process of elimination I was able to figure out how to get everything working smoothly again.

     

Creating a .bat Maya Launch Process For Maya or Maya Standalone


An example .bat I can optionally compile into a pretty exe with an icon:

set MAYA_SCRIPT_PATH=N:\Scripts\Maya\MEL\jaTools\scripts;N:\Scripts\Maya\MEL\userSetup_configs;
set PYTHONPATH=%PYTHONPATH%;N:\Scripts\Maya\Python\userSetup_configs\MBPRO_JA;C:\Program Files\Autodesk\Maya2011\Python\Lib\site-packages
"C:\Program Files\Autodesk\Maya 2011 Subscription Advantage Pack\bin\maya.exe"

   

If you’re trying this out for the first time, the devil’s in the details.  Sometimes things don’t work if you leave spaces or put paths in the wrong order.  Always think about it like you are sourcing scripts in Maya, for instance where are the most important ones and put them first on the list.

To launch Maya Standalone from the commandline without a GUI for running batch processes, the .bat file is very useful for making sure all of the environment settings are perfect for launching mayapy.exe.

set MAYA_SCRIPT_PATH=H:\Scripts\Maya\MEL\userSetup_configs;H:\Scripts\Maya\MEL\jaTools\scripts
set PYTHONPATH=H:\Scripts\Maya\Python\userSetup_configs;C:\Documents and Settings\Administrator.USIM\My Documents\pymel-1.0.2;C:\Program Files\Autodesk\Maya2009\Python\lib\site-packages
"C:\Program Files\Autodesk\Maya2009\bin\mayapy.exe"

     

Update userSetup.mel from part 1: Trimming the Fat


Since MAYA_SCRIPTS_PATH is treated a little differently I am also able to make big changes to the way i’m starting up Maya with userSetup.mel.  After playing around with mel for a while I quickly learned that the userSetup was getting out of control massive!  Thousands of lines of code for UIs that run my tools were bloating my userSetup and slowing down my startup time.   I was able to quickly downsize the file back to the way it should be, very minimal.  Now, almost the only thing I need the userSetup to do is hold my custom script, plugin, and icon paths.  I house NOTHING inside the Maya prefs directory, not even a userSetup.  Back in the day, I’ve lost plenty of work and organization has gotten completely out of control when I do that, and I guess I’m a bit of a control freak when it comes to my work.
 

Read more about setting up Maya for customization on djx’s blog.
 

 
  • Pingback: Maya.env Configuration of Variables Using “userSetup.mel” | CG Hijinks

  • http://animateshmanimate.com John P. Neumann

    Already threw this your way, but %PYTHONPATH% is the environment variable for Python in Windows. Similarly PYTHONPATH is the environment variable for Python in OSX (*Nix OS’) and is set in the .bash_profile or .profile – generally speaking.

    Also, if you want your Python environment variable to stay constant, you can use Microsoft’s setx instead of set. In Vista/7 you’ll have to run the dos shell as an administrator to use setx since it sets it in the registry. More info on that can be found here: http://technet.microsoft.com/en-us/library/cc755104%28WS.10%29.aspx

    • http://www.jonasavrin.com Jonas Avrin

      Yo, arrantsquid, thanks for posting! Yes, good insight there. The purpose for using the cmd shell is to be able to set environment variables temporarily so setx isn’t necessary, but nice exposition anyway.

      • http://animateshmanimate.com John P. Neumann

        Got it. I generally use all the same libs, so setx (setenv) is usually how I do things. :)

        • renatopolimeno

          Nice one!
          What about one .env inside a server to populate custom python scripts ?
          I mean, N machines with project set in the same server/folder, how to import someCustom.py using any machines without problems ?

          thanks,

          • http://www.jonasavrin.com Jonas Avrin

            Hi Renato! I’m sorry I wasn’t able to get to this earlier, just been swamped. I use an exe that sets PYTHONPATH and then use userSetup.py to sys.path append() necessary scripts that artists need to use. I personally don’t like using .env too much manual copy pasting files. If you need more help just contact me.

          • renatopolimeno

            Hey Jonas ! 
            Since I´m new to Maya I thought .env would solve my problem but unfortunately it didin´t..

            I´m completely lost and don´t know how to spread some custom scripts to a few users using a folder in the server…

          • renatopolimeno

            btw,
            Can I use one common usersetup.py from server to point all local machines from that ?
            I mean, putting one-by-one inside drive:UsernameDocsmayaversionscripts with sys.path.append() is the only way to go ?

          • http://www.jonasavrin.com Jonas Avrin

            Renato, I don’t use .env at all, nore do I have anything in my local userdocumentsmaya. Just use a shell script .bat to set up the environment and launch maya. Some other scripts may be necessary to make things more convenient in your userSetup(s). I sent you an email.

  • Pingback: Studio Level Managment Of Scripts and Tools in Maya | LosArt3D.com

  • Pingback: Custom Maya Startup: Adding User Python Site-Packages | CG Hijinks