Announcements
Autodesk Community will be read-only between April 26 and April 27 as we complete essential maintenance. We will remove this banner once completed. Thanks for your understanding

userSetup.py and script editor import same module/package twice

dominik6UUXE
Contributor Contributor
403 Views
2 Replies
Message 1 of 3

userSetup.py and script editor import same module/package twice

dominik6UUXE
Contributor
Contributor

I have a singleton in a module I am importing in my userSetup.py.

# Package/Module.py
class Singleton:
    def get_instance(): ...
# userSetup.py:
from Package import Module
cls = Module.get_instance()
# In the script editor, I can use:
print(cls, Module)
# >>> (instance, module)

from Package import Module
cls2 = Module.get_instance()
cls == cls2
# >>> False

I suspect this is because the globals() from the userSetup.py gets passed into the script editor env and sys.modules stays empty.

While I dont plan to run this in the script editor, there will be other packages and modules or even shelves run from within Maya that will import the Module and all try to get the instance of the singelton

 

Any suggestions how to accomplish this without trusting my peeps that they dont `import Module`? Or without misusing pythons import system or having to pass globals() to any subsequent tool?

0 Likes
Accepted solutions (1)
404 Views
2 Replies
Replies (2)
Message 2 of 3

dominik6UUXE
Contributor
Contributor
Accepted solution

Wrapping it into a function and calling it through evalDeferred() seems to work. Not sure if that just works in this case or if it will generally work. "the next available idle time" is what the documentation says but I am not sure what that means specifically.

I havent tested it, but it might break if someone calls processIdleEvents(). Or not.. I dont know 🙂

 

0 Likes
Message 3 of 3

BigRoy
Advocate
Advocate

Are you sure your singleton implementation actually stores the singleton instance correctly in the module?