userScript.py is not being executed

39 views
Skip to first unread message

João Victor

unread,
Aug 17, 2021, 8:38:52 PM8/17/21
to Python Programming for Autodesk Maya

Hello there,
I am trying to run the userScript.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])

Sometimes it runs, and sometimes do not.
The userScript.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!

bobrobertuma

unread,
Aug 17, 2021, 11:10:22 PM8/17/21
to python_in...@googlegroups.com

I’m bad with python but shouldn’t you have to initialize it first as it’s being called as a startup script or perhaps set it to wait till Maya has loaded all the way?  I’m not sure at all, just a suggestion and or query.

 

Jason

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/b3b1bf17-c520-4ef0-b7cd-d26afe63220cn%40googlegroups.com.

Marcus Ottosson

unread,
Aug 18, 2021, 1:32:15 AM8/18/21
to python_in...@googlegroups.com

Try this.

from maya import cmds

def functionA():
    varA = False

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

cmds.evalDeferred(delayMe)

By “evaling” something “deferred” you delay having something called until Maya is loaded up, UI and all. Also don’t forget to actually import maya.cmds as cmds won’t be available until someone does. So odds are there were other scripts also called on startup that at some point imported it ahead of your userSetup.py, which would explain why it worked those times. Which is also the reason why - for the diligent - you should either rename that import, or import within a function so the name doesn’t spill over into other scripts.

def functionA():
    varA = False

def delayMe():

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

def setup():
    from maya import cmds
    cmds.evalDeferred(delayMe)

setup()

João Victor

unread,
Aug 18, 2021, 10:37:33 AM8/18/21
to python_in...@googlegroups.com
Thanks for the suggestion Marcus. I tested your code and some variations to check where the code is running, so I had this result:

# this first line which I call cmds, was successfully called
# (but I had to call cmds outside here,
# because it returned undefined if it was inside the setup() function)

import maya.cmds as cmds

def functionA():    # this function is put on memory successfully, but was not run
    print "its NOT printed" # it was not printed
    global varA    # it was not declared
    varA = "A"

def delayMe():    # it was run
    print "its printed"  # it was printed
    global varB    # it was declared
    varB = "B"

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

def setup():    # it was run
    print "its NOT printed"  # it was not printed
    global varC    # it was declared
    varC = "C"
    cmds.evalDeferred(delayMe)

setup()    # it was run

So I think I am gonna put everything from functionA() to delayMe().
Thanks a lot.

João Victor

unread,
Aug 18, 2021, 11:37:09 AM8/18/21
to python_in...@googlegroups.com
I just sent an email 57 minutes ago saying that some functions and variables worked because I executed it many times and it was working well.
So I executed now, exactly the same code, and it didn't work anymore.
I am going to load the .py file through userSetup.mel, or simply make a button on the shelf, I think its faster, but if you figure out whats happening let me know please.
thanks a lot!

Em qua., 18 de ago. de 2021 às 02:32, Marcus Ottosson <konstr...@gmail.com> escreveu:

bobrobertuma

unread,
Aug 18, 2021, 1:44:33 PM8/18/21
to python_in...@googlegroups.com

What are you trying to accomplish?

João Victor

unread,
Aug 19, 2021, 5:09:28 PM8/19/21
to python_in...@googlegroups.com
Hey Jason, thanks for replying!
Im sorry the delay, sometimes the emails from google groups goes to spam folder.

I am trying to declare variables and functions when Maya starts and when open a new scene. Its because I did some scripts in order to facilitate the navigation on the viewport.. to make it similar to 3ds max.
It seems sometimes doesn´t work, but sometimes work dont know why. About your suggestion in your latest email, I don´t know how to do that, if can tell me please let me know.

thanks a lot.

João

bobrobertuma

unread,
Aug 19, 2021, 5:24:21 PM8/19/21
to python_in...@googlegroups.com

I was simply suggesting what Marcus laid out properly that you need to invoke python earlier

e.g. import maya.cmds as cmds

João Victor

unread,
Aug 19, 2021, 7:32:01 PM8/19/21
to python_in...@googlegroups.com
I am not sure if I understood what you meant, because my code is looking exactly like this:

# this first line which I call cmds, was successfully called
# (but I had to call cmds outside here,
# because it returned undefined if it was inside the setup() function)

import maya.cmds as cmds

def functionA():    # this function is put on memory successfully, but was not run
    print "its NOT printed" # it was not printed
    global varA    # it was not declared
    varA = "A"

def delayMe():    # it was run
    print "its printed"  # it was printed
    global varB    # it was declared
    varB = "B"

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

def setup():    # it was run
    print "its NOT printed"  # it was not printed
    global varC    # it was declared
    varC = "C"
    cmds.evalDeferred(delayMe)

setup()    # it was run

And the varA which should be declared on functionA() is not being declared.
Could you tell me how you are suggesting to invoke python earlier? Because I executed Marcus´s code and it didn't work here. :/

thanks!

Reply all
Reply to author
Forward
0 new messages