Python - force SGTK to reload modules

Hey there,
I’m currently working on a publish pipeline, using STK to do checks and publish operations with my own modules.

env hook tk-multi-publish2 loads a module A, which loads a module B, which does some stuff.

My problem is that SGTK does not seem to reload module B if I don’t completly close and restart maya. Even ‘Reload engine and apps’ in Maya Shotgun’s Work Area Info does not seem to do the trick.

  • I am using a decentralised setup (descriptor pointing to a folder on our sever)

  • module B is outside config folder, in a cross project directory

  • all init.py are setuped to reload submodules:

    try:
        # First time the module imports, loaded is undefined and will trigger the exception.
        # loaded is then defined in the exception block
        # when reloaded, loaded is defined as true, so reload your submodules:
        if loaded:
            reload(subModule)
    except:
        loaded = True

Do you know any way to force python / sgtk to reload all submodules without exiting maya? Right now, everytime I make a syntaxError, it takes me several minutes juste to relaunch maya…

2 Likes

Hi Ben

I just mocked up an example on my side, and whilst Toolkit won’t reload any modules that it didn’t import (I’m not actually a 100% on what the requirements are there), you should be able to reload them with a reload statement, but you would need to handle that your self.

import sys
import os
from sgtk.platform import Application

# Append path to message helper module location
sys.path.append("/Users/philips1/sg_toolkit/temp")

import message_helper
if "MESSAGE_HELPER_PRELOADED" in os.environ:
    print ("Reloading message helper")
    reload(message_helper)

os.environ["MESSAGE_HELPER_PRELOADED"] = "1"

class StgkStarterApp(Application):
    def init_app(self):
        self.engine.register_command("Show Starter Template App...", self.show_app)

    def show_app(self):
        print(message_helper.get_message())

I think the problem with your code is that due to scope, the loaded variable is reset when you run the Toolkit restart, so it is never True? The environment variables should survive the reload.
Though I appreciate that is not very pretty.
I’ll mark this to run past the team.

1 Like

Maybe you could move your code into a Framework and import it that way? Though I appreciate that may not be suitable in your situation…

2 Likes

I’m not sure I quite understand your first message.
How would I use the code you posted to reload a module imported in a maya publish hook called by tk-multi-publish2, for instance?

Thanks!

1 Like

Hi Ben –

We talked this over with the team earlier in the week, and it’s actually more of a Python question – how to reload Python modules at runtime. As it stands, Toolkit doesn’t do it, as you saw, though admittedly, I can see how you’d expect imported modules to be updated when you use “Reload Engines and Apps”.

As Phil allluded to in his last message, if you did want to capitalize on Toolkit’s Reload, your best bet would be to move your logic from a regular Python module into a Toolkit framework, then it will be reloaded when you choose “Reload Apps and Engines”.

Take a look at this doc for more info on developing a framework.

1 Like

You would put something like this at the top of the hook file:

You would post the `import message_helper
if "MODULES_PRELOADED" in os.environ:
    reload(the_module_I_want_to_reload)

os.environ["MODULES_PRELOADED"] = "1"

You could even store a list of modules you want to reload, and loop over them, and reload.

3 Likes

Thanbks for your help, I’ll keep you posted.

3 Likes