userSetup.py is not being executed

Hello there,
I am trying to run the userSetup.py file placed in Documents\maya\2020\prefs\scripts , but there are functions and variables which are not declared when Maya is open…
I basically did it:

def functionA():
    varA = False

cmds.scriptJob(event = ["SceneOpened", functionA])

The weird thing is that sometimes it runs, and sometimes do not.
The userSetup.mel runs always, so I think I could put at the end of the mel code, a line to call the python code.

Any suggestion? thanks!

Maybe try using evaldeferred:

https://download.autodesk.com/us/maya/2011help/CommandsPython/evalDeferred.html

Okay, so is the problem that you can’t see varA in the main namespace after a scene is opened?
Because if yes, that is 100% expected from python, as any variables set in a functions scope stay in that function’s scope, unless you use the global keyword, which will then place the value in the module’s scope.
Now anything in userSetup.py is executed in the __main__ namespace (which is what you see when working in the script editor), but anything imported by a different module won’t see that unless you go digging for it.

Now everything I’ve said above is true, and a possible explanation for what you are currently seeing, however I’ve left out one very big thing. All of it is a terrible idea :slight_smile:

So I’ve got a question for your question instead, which is:
“What exactly are you hoping to do, and what use do you see for it?”

hey Ben, thanks for the suggestion… I tried it and didn´t work. :confused:

hey Bob, thanks a lot for explaning this… its very useful!
I also tried it, but didn´t work:

def functionA():
    global varA
    varA = False

cmds.scriptJob(event = ["SceneOpened", functionA])

this is what you meant about global declaration?

so answering the question:
I did a setup for a customized viewport navegation, so I need to have these functions and variables declared when maya starts… I can put everything in a button on the shelf, but I want to check if I could avoid to click everytime maya starts or open new scene.

thanks a lot @bob.w !