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.

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

> 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()
>
>
> On Wed, 18 Aug 2021 at 04:10, bobrobertuma <bobrobert...@gmail.com> wrote:
>
>> 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
>>
>>
>>
>> *From:* python_inside_maya@googlegroups.com [mailto:
>> python_inside_maya@googlegroups.com] *On Behalf Of *João Victor
>> *Sent:* Tuesday, August 17, 2021 6:39 PM
>> *To:* Python Programming for Autodesk Maya <
>> python_inside_maya@googlegroups.com>
>> *Subject:* [Maya-Python] userScript.py is not being executed
>>
>>
>>
>> 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!
>>
>> --
>> 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_maya+unsubscr...@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
>> <https://groups.google.com/d/msgid/python_inside_maya/b3b1bf17-c520-4ef0-b7cd-d26afe63220cn%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>> --
>> 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_maya+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/python_inside_maya/002c01d793de%249210b430%24b6321c90%24%40gmail.com
>> <https://groups.google.com/d/msgid/python_inside_maya/002c01d793de%249210b430%24b6321c90%24%40gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> 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_maya+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCzBQWSaJkZLFWmsAhNpg2tSeo9PhUJ-aOCrEuzovfQJw%40mail.gmail.com
> <https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCzBQWSaJkZLFWmsAhNpg2tSeo9PhUJ-aOCrEuzovfQJw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAJZGTKV_VAe0rU6a6V73ek%2BWCio-_vH6fWdw0z1ux7ikax3LGQ%40mail.gmail.com.

Reply via email to