mayapy2 not running userSetup.py files from PYTHONPATH?

mayapy2 not running userSetup.py files from PYTHONPATH?

david.oberst
Contributor Contributor
3,913 Views
6 Replies
Message 1 of 7

mayapy2 not running userSetup.py files from PYTHONPATH?

david.oberst
Contributor
Contributor

Maya will run userSetup.py from any directory on PYTHONPATH.  So if we have C:\ourdir\userSetup.py, and a script file "ourScript.py" that looks like this:

import maya.standalone
maya.standalone.initialize(name="python")

we in Windows we can go:

SET PYTHONPATH=C:\ourdir
mayapy2 ourScript.py

and our userSetup.py gets executed during maya.standalon.initiialze() as expected.

 

However, with Maya 2022, at least with the Python 2 "mayapy2.exe", the standalone initialize doesn't seem to be executing the userSetup.py.  Is this a glitch, or has Maya 2022 changed something about its startup sequence?  If you run the GUI version "maya.exe -pythonver" with the same PYTHONPATH, it does work, so I'm not sure if it is something about mayapy2.

0 Likes
3,914 Views
6 Replies
Replies (6)
Message 2 of 7

david.oberst
Contributor
Contributor

The same thing happens with the Python3 "mayapy" in Maya 2022.
I notice that Maya 2022 has new "Security Preferences", set in the "Security" pane of the Preferences dialog.  Specifically, there is a "read and execute 'userSetup' scripts checkbox, which seems to be saved as the "SaveModeExecUserSetupScript" optionVar in the "userPrefs.mel" preferences file.

 

I'm guessing the Maya 2022 startup code has some sort of internal flag that by default prevents the userSetup files from being executed.  The maya app probably does an early check of that optionVar value, and if it is 1, alters the internal flag or whatever mechanism allows the maya initialization to run the userSetup files.  But mayapy doesn't look at the preferences, and so can't indicate that it wants the startup code to run the userSetup files, and there doesn't seem to be any documented way (environment variable, new parameter to maya.standalone.initialize) to change this.

 

If this is correct, then Maya 2022 has broken the previous behaviour of maya.standalone.initialize(), which would run the userSetup.py files it discovered.  This isn't mentioned in the Known Limitations page or any of the Maya 2022 change notes that I can find.

Message 3 of 7

david.oberst
Contributor
Contributor

The problem is in maya.app.startup.basic.executeUserSetup().  There's a new check in that function:

    isExec = cmds.optionVar(q='SafeModeExecUserSetupScript')
    if isExec != 1:
        return

which checks that preference value and returns if executing userSetup files has been disabled.  Presumably the maya app loads the userPrefs.mel preference file early in the startup sequence, and the value is 1 by the time executeUserSetup() is called.

However, I don't think the mayapy interpreter is designed to read preferences, and in any case the value is 0 when the function runs, and no userSetup files are executed.  I've confirmed by setting a breakpoint at that spot that if I do a debugger expression eval of "cmds.optionVar(iv=("SafeModeExecUserSetupScript",1) and continue, everything is fine and the userSetup files load.

But the maya.cmds.optionVar() function isn't available before calling maya.standalone.initialize(), so I can't just set it myself before I call that.  I tried to monkeypatch a wrapper of maya.standalone.initialize() or maya.app.startup.basic() that would set the value before continuing, but there's the same problem of the optionVar() function not being available at that time.  maya.standalone is a compiled ".pyd" library package, so I can't check and see if there's something else I could hook into.

Does anyone know if there is a way to have the mayapy read a prefs file, or some other way to set that "SafeModeExecUserSetupScript" optionVar value?

0 Likes
Message 4 of 7

david.oberst
Contributor
Contributor

I've come up with a somewhat warty workaround (see below) for this for Maya 2022.  The maya.app.commands.processCommandList() function is where all the various command stubs for maya.cmds are patched in, after which maya.cmds.optionVar() becomes available.  I've created a wrapper for processCommandList() which calls the original function, then sets the "SafeModeExecUserSetupScript" optionVar preference to 1.  Now when the initialization() function runs, the userSetup,py discovery and execution occurs as in previous versions.

 

This will also work if you import pymel.core, which calls initialize() itself, among other things.  Note that this workaround won't activate the userSetup hash checking option - those hashes are stored in the userPrefs.mel prefs file.

 

It would be nice if someone from Autodesk could comment if the break in initialize() behaviour was intentional or accidental, and perhaps something could be done to avoid needing this sort of workaround.  Perhaps an optional "executeUserSetup" option to initialize().

 

import maya.app.commands
orig_processCommandList = maya.app.commands.processCommandList
def new_processCommandList():
    orig_processCommandList()
    import maya.cmds as cmds2
    cmds2.optionVar(iv=('SafeModeExecUserSetupScript', 1))
maya.app.commands.processCommandList = new_processCommandList

maya.standalone.initialize(name="python")

 

Message 5 of 7

daviesm
Alumni
Alumni

Hi,

 

this change in behaviour is not intentional we would except the default to the same as it ever was.

 

We will investigate the issue.

 

Mark Davies

Autodesk

Message 6 of 7

david.oberst
Contributor
Contributor

Also, it appears the underlying APIs don't "know" they are running in a mayapy interpreter until after maya.standalone.initialize() is called.  If you do this:

import maya.OpenMaya as om
import maya.standalone
print(om.MGlobal.mayaState(), om.MGlobal.mayaState()==om.MGlobal.kLibraryApp)
maya.standalone.initialize(name="python)
print(om.MGlobal.mayaState(), om.MGlobal.mayaState()==om.MGlobal.kLibraryApp)

 you get "(0, False)" the first time, and "(2, True)" the second time.   That 0 value is MGlobal.kInteractive, the value the Maya GUI app returns.  It is unfotunate that MGlobal.mayaState() is unable to return kLibraryApp before initialize(), but really unfortunate that it returns a wrong value, instead of None or some sort of enumerated value that means "I don't know yet".  Perhaps a new MGlobal.kNotYetDetermined=-1 enum value?  There doesn't seem to be a clean way to check if you are in a standalone interpreter - I stole an idea from PyMEL and check mayaState() plus whether there are missing commands from maya.cmds - which only get added after initialize().

It looks like they didn't consider the mayapy case when they added the safe mode checking in executeUserSetup(), and assumed the optionVar would always reflect an explicit value from loaded preferences.

0 Likes
Message 7 of 7

ben.barker
Explorer
Explorer

Just as an addendum to this for people from the future, like me, this was fixed in 2022.1 and beyond. If you experience this problem make sure you are updated to the latest version of 2022.

0 Likes