Set up Maya Color Management with Python

I need to set up preferences before Maya starts.

In particular, I wish to turn off Color Management.
I have a Python script, launched in userSetup.mel, where I set the Maya Project, for example. If I add a code to edit color management, but it does not work in this way (although works if you run it in launched Maya from Script Editor):

pm.colorManagementPrefs(e=True, cmEnabled=False)

What is the usual way to define preferences (and color management) before Maya starts?

I’ve never done color management settings, but userSetup.py is my usual way of doing default/override settings during startup.

I created a userSetup.py in the same place where I have my userSetup.mel but Maya does not see it.

I define a path to userSetup.mel before Maya launches with:
os.environ['MAYA_SCRIPT_PATH'] = 'my_path'

How can I add a path to userSetup.py?

Probably another userSetup.py already exists and used:

import userSetup
reload(userSetup)

Result: <module 'userSetup' from 'C:\Program Files\Autodesk\Maya2019\plug-ins\MASH\scripts\userSetup.py'>

Somebody please correct me if I’m wrong about this, but this is the way I understand it.

Actually Maya will run every userSetup file it can find.
I’m unsure about the order, but for your purpose it shouldn’t matter.

What’s happening here is that userSetup.py runs before Maya is fully loaded, at least before the settings are loaded.
So settings made with Python commands are overridden by the rest of the startup sequence.

A way around this, I find, is to use evalDeferred to run a command on lowest priority.
This way it waits for startup to finish, then runs the given command.

Try putting this in your userSetup for example.

import maya.cmds as cmds

def foo():
	print('---This is the userSetup running---')

cmds.evalDeferred('foo()', lowestPriority=True)

If you check your script editor after maya loads you should see the print statement in there,

1 Like

I understand that it could be multiple userSetup.py files. How can I tell Maya, where to search for my own userSetup.py (which is located in a folder with all pipeline files)?

Tried to use evalDeferred in a script, launched from userSetup.mel, but it did not work:
Error: NameError: file <maya console> line 1: name 'foo' is not defined

@kiryha
userSetup.py files are loaded from following paths:
C:\Users$USER\Documents\maya\scripts\
C:\Users$USER\Documents\maya$MAYA_VERSION\scripts\

or whatever is set in PYTHONPATH environment variable

@PVDH Ps you don’t to import cmds as an alias. Instead you can do.

from maya import cmds

1 Like

I set path to userSetup.py explicitly with 'PYTHONPATH ’ environment variable:

print  os.environ['PYTHONPATH']
# C:\pipeline\tools;C:\pipeline\tools\at_maya\settings

Still, Maya does not run it. I have a print statement in userSetup.py and I don’t see it when Maya launches in the Script Editor.

So, Maya sees my userSetup.py but does not run it when launched…

But now, if I run import userSetup in Maya it uses my userSetup.py

It is going to print it in the output window unless you use linux.

1 Like

def executeUserSetup():
    """
    Look for userSetup.py in the search path and execute it in the "__main__"
    namespace
    """
    if not os.environ.has_key('MAYA_SKIP_USERSETUP_PY'):
        try:
            for path in sys.path[:]:
                scriptPath = os.path.join( path, 'userSetup.py' )
                if os.path.isfile( scriptPath ):
                    import __main__
                    execfile( scriptPath, __main__.__dict__ )
        except Exception, err:
            # err contains the stack of everything leading to execfile,
            # while sys.exc_info returns the stack of everything after execfile
            try:
                # extract the stack trace for the current exception
                etype, value, tb = sys.exc_info()
                tbStack = traceback.extract_tb(tb)
            finally:
                del tb # see warning in sys.exc_type docs for why this is deleted here
            sys.stderr.write("Failed to execute userSetup.py\n")
            sys.stderr.write("Traceback (most recent call last):\n")
            # format the traceback, excluding our current level
            result = traceback.format_list( tbStack[1:] ) + traceback.format_exception_only(etype, value)
            sys.stderr.write(''.join(result))

This is the code that maya runs at startup to handle userSetup.py

userSetup.py is run well before most of the GUI is in place, print statements will appear in the output window, unless you’ve hidden it, in which case they are sent to the void.

userSetup.mel is actually run AFTER the GUI is in place, so usually print statements from it end up at the end of the log.

1 Like

Correct, I have my userSetup.py and it working with evalDeferred() to turn OFF color management.

Have you tried turning your computer off and on again? You’ll also need __init__.py in your userSetup folder.

I’m currently working with a lot of env var, the implementation can be a bit wanky depending on the dcc supposed to read them.

The huge problem with PYTHONPATH and userSetup is that PYTHONPATH will be used by a lot of different things.
Plus, on windows, the fact that there are system and user env var can be a bit tricky. I also had bugs with maya 2019 when pointing PYTHONPATH to a directory with a subdirectory ‘maya’ (which, if you are working ina studio with a lot of td and tools, is sure to happen).
I personnally don’t like adding modules specific folders to this env var. Keep it clean, point PYTHONPATH to your main repo directory and user init files to load packages and modules.

It’s safer to launch maya with a command that will read a py file rather than using userSetup, plus it means you can have as many maya setup as you want.

All those years…

1 Like

I had the same issue some time ago, and it took me a while to find the reason!

Actually, my userSetup.py was working, I just expect print statement in Script editor while it was in Output window as max_wiklund mentioned.

My PYTHONPATH is clean, it has the only path to the root folder of our pipeline, and now I added a path to userSetp.py (which is located in the same folder, but not in the root, so I have to append it)

The shame is real. This is such a hard habit to break, and I don’t even know where I got it from…

By the way, if you wnat to use an OCIO config, you can simply use env vars:

os.environ[‘OCIO’] = path/to/config.ocio

Maya, Nuke, RV and other softs will automatically use it.

1 Like