Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
Showing results for 
Show  only  | Search instead for 
Did you mean: and script editor import same module/package twice

Message 1 of 3
212 Views, 2 Replies and script editor import same module/package twice

I have a singleton in a module I am importing in my

# Package/
class Singleton:
    def get_instance(): ...
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 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?

Labels (2)
Message 2 of 3
in reply to: dominik6UUXE

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 🙂


Message 3 of 3
in reply to: dominik6UUXE

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators

Autodesk Design & Make Report