Script execution when Project change

Hello,

When you launch SG Desktop the tank_init.py is executed which allows you to setup environment variables and other pipeline customizations. I was wondering, is there something similar that is executed every time a user changes the Project context in SG Desktop?

2 Likes

I think engine_init is what you’re after.

3 Likes

If I am in Project A and I switch over to Project B, via SG Desktop, is engine_init.py fired off? Are there any other situations, under the hood, that would cause engine_init.py to execute?

2 Likes

I am running into an issue with engine_init and Qt. Here is my code:

from sgtk.platform.qt import QtGui

from tank import Hook


class EngineInit(Hook):
    def execute(self, engine, **kwargs):
        """
        Executed when a Toolkit engine has been fully initialized.
        At this point, all apps and frameworks have been loaded,
        and the engine is fully operational.
        The default implementation does nothing.
        :param engine: Engine that has been initialized.
        :type engine: :class:`~sgtk.platform.Engine`
        """

        self.engine = engine

        # Store a handy reference to the Shotgun API
        self.sg = self.parent.shotgun

        if self.engine.context.project:
            # QtGui.QMessageBox.information(self, "Greetings", "Hello!")
            self.logger.info("*** Project Id: {}".format(self.engine.context.project.get("id")))
            self.logger.info("*** User Id: {}".format(self.engine.context.user.get("id")))

If I run this ‘as-is’ everything works fine. I get the info lines logged with my project and user id numbers. But, if I uncomment the QtGui line, the code never gets to the logging and I get an error (see below) about the parent being passed in to QMessageBox. If I am running this from a Toolkit app I am able to use QtGui.QApplication.activeWindow() as the parent. But, what should I be using for the parent when it is coming from engine_init as self does not seem to work because that tries to use EngineInit as the parent to the message box. What should that be using as the parent?

In case it helps, here is the error I am getting:

2020-08-27 11:29:57,465 [   ERROR] [PROXY] Exception raised while executing hook '/Users/mharris/work/sandbox/Shotgun/toolkit/configs/tk-config-default2/core/hooks/engine_init.py'
Traceback (most recent call last):
  File "/Users/mharris/Library/Caches/Shotgun/pipetd-dev/p122c1.basic.desktop/cfg/install/core/python/tank/pipelineconfig.py", line 1218, in execute_core_hook_internal
    return_value = hook.execute_hook(hook_path, parent, **kwargs)
  File "/Users/mharris/Library/Caches/Shotgun/pipetd-dev/p122c1.basic.desktop/cfg/install/core/python/tank/hook.py", line 577, in execute_hook
    return execute_hook_method([hook_path], parent, None, **kwargs)
  File "/Users/mharris/Library/Caches/Shotgun/pipetd-dev/p122c1.basic.desktop/cfg/install/core/python/tank/hook.py", line 631, in execute_hook_method
    ret_val = hook_method(**kwargs)
  File "/Users/mharris/work/sandbox/Shotgun/toolkit/configs/tk-config-default2/core/hooks/engine_init.py", line 37, in execute
    QtGui.QMessageBox.information(self, "Greetings", "Hello!")
TypeError: 'PySide.QtGui.QMessageBox.information' called with wrong argument types:
  PySide.QtGui.QMessageBox.information(EngineInit, str, str)
Supported signatures:
  PySide.QtGui.QMessageBox.information(PySide.QtGui.QWidget, unicode, unicode, PySide.QtGui.QMessageBox.StandardButtons = QMessageBox.Ok, PySide.QtGui.QMessageBox.StandardButton = NoButton)
  PySide.QtGui.QMessageBox.information(PySide.QtGui.QWidget, unicode, unicode, PySide.QtGui.QMessageBox.StandardButton, PySide.QtGui.QMessageBox.StandardButton = NoButton)
2 Likes

Any Qt-lovin’ on a Forum Friday?

1 Like

@mharris, checking with the team on this…

3 Likes

Hey Mike!

We took a look at this today, and have some thoughts.

  • As you saw, self isn’t going to work, as it’s just the hook itself, not anything Qt knows about.
  • One thing you might try is setting the parent to none – does it need to have a parent?
  • Beyond that, we’re thinking tank.platform.qt.tankqdialog.TankQDialog should probably work:

To get there, we ran the following in Desktop’s python console:

from PySide.QtGui import QApplication
print QApplication.instance()
QApplication.instance().topLevelWidgets()

the output from the last line listed tank.platform.qt.tankqdialog.TankQDialog – we believe this is Desktop itself.

Give that a try and let us know if it works.

1 Like

This is the error I got output when using that in my code:

2020-08-31 14:45:37,518 [   ERROR] [PROXY] Exception raised while executing hook '/Users/mharris/work/sandbox/Shotgun/toolkit/configs/tk-config-default2/core/hooks/engine_init.py'
Traceback (most recent call last):
  File "/Users/mharris/Library/Caches/Shotgun/pipetd-dev/p122c1.basic.desktop/cfg/install/core/python/tank/pipelineconfig.py", line 1218, in execute_core_hook_internal
    return_value = hook.execute_hook(hook_path, parent, **kwargs)
  File "/Users/mharris/Library/Caches/Shotgun/pipetd-dev/p122c1.basic.desktop/cfg/install/core/python/tank/hook.py", line 577, in execute_hook
    return execute_hook_method([hook_path], parent, None, **kwargs)
  File "/Users/mharris/Library/Caches/Shotgun/pipetd-dev/p122c1.basic.desktop/cfg/install/core/python/tank/hook.py", line 631, in execute_hook_method
    ret_val = hook_method(**kwargs)
  File "/Users/mharris/work/sandbox/Shotgun/toolkit/configs/tk-config-default2/core/hooks/engine_init.py", line 37, in execute
    QtGui.QMessageBox.information(tank.platform.qt.tankqdialog.TankQDialog, "Greetings", "Hello!")
AttributeError: 'module' object has no attribute 'tankqdialog'

But, when I run the 3 lines you showed in the console I got the same output you got.

1 Like

Not sure if this helps shed light on anything, but I tried a slight modification to my script:

from sgtk.platform.qt import QtGui
from tank.platform.qt import tankqdialog
from tank import Hook


    class EngineInit(Hook):
        def execute(self, engine, **kwargs):
            """
            Executed when a Toolkit engine has been fully initialized.
            At this point, all apps and frameworks have been loaded,
            and the engine is fully operational.
            The default implementation does nothing.
            :param engine: Engine that has been initialized.
            :type engine: :class:`~sgtk.platform.Engine`
            """

            self.engine = engine

            # Store a handy reference to the Shotgun API
            self.sg = self.parent.shotgun

            if self.engine.context.project:
                QtGui.QMessageBox.information(tankqdialog.TankQDialog, "Greetings", "Hello!")
                self.logger.info("*** Project Id: {}".format(self.engine.context.project.get("id")))
                self.logger.info("*** User Id: {}".format(self.engine.context.user.get("id")))

…and I got a slightly different error:

2020-09-02 07:02:33,882 [   ERROR] [PROXY] Exception raised while executing hook '/Users/mharris/work/sandbox/Shotgun/toolkit/configs/tk-config-default2/core/hooks/engine_init.py'
Traceback (most recent call last):
  File "/Users/mharris/Library/Caches/Shotgun/pipetd-dev/p122c1.basic.desktop/cfg/install/core/python/tank/pipelineconfig.py", line 1218, in execute_core_hook_internal
    return_value = hook.execute_hook(hook_path, parent, **kwargs)
  File "/Users/mharris/Library/Caches/Shotgun/pipetd-dev/p122c1.basic.desktop/cfg/install/core/python/tank/hook.py", line 577, in execute_hook
    return execute_hook_method([hook_path], parent, None, **kwargs)
  File "/Users/mharris/Library/Caches/Shotgun/pipetd-dev/p122c1.basic.desktop/cfg/install/core/python/tank/hook.py", line 631, in execute_hook_method
    ret_val = hook_method(**kwargs)
  File "/Users/mharris/work/sandbox/Shotgun/toolkit/configs/tk-config-default2/core/hooks/engine_init.py", line 23, in execute
    QtGui.QMessageBox.information(tankqdialog.TankQDialog, "Greetings", "Hello!")
TypeError: 'PySide.QtGui.QMessageBox.information' called with wrong argument types:
  PySide.QtGui.QMessageBox.information(Shiboken.ObjectType, str, str)
Supported signatures:
  PySide.QtGui.QMessageBox.information(PySide.QtGui.QWidget, unicode, unicode, PySide.QtGui.QMessageBox.StandardButtons = QMessageBox.Ok, PySide.QtGui.QMessageBox.StandardButton = NoButton)
  PySide.QtGui.QMessageBox.information(PySide.QtGui.QWidget, unicode, unicode, PySide.QtGui.QMessageBox.StandardButton, PySide.QtGui.QMessageBox.StandardButton = NoButton)
1 Like

When clicking on a DCC icon in SG Desktop, is there a hook file to insert code to test a condition before the DCC (Maya specifically) launches?

Thanks,

Mark

1 Like

Hey @mdthielen,

Would the before_app_launch hook work for you?

-David