How can I load a Toolkit framework in my hook?

Toolkit frameworks can be useful for writing code that can be then reused in a number of different Apps or Engines.

Normally if you are writing a custom App or Engine, you would import the framework using sgtk.platform.import_framework(), and this requires that the App sets the framework as a dependency in the app’s info.yml.

Frameworks in App hooks
However, if you want to use a framework, in an App hook you can do so using the Hook class’s load_framework() .
This method allows you to import a framework that hasn’t been explicitly set as a dependency for the App. Note the framework must still be included in your configuration environment settings.

Frameworks in core hooks
It is not possible to import a framework into a core hook. In order to load a framework within a hook, an app or engine must be running. This is because the manifest for that app or engine is used to find the framework dependencies and determine which version of the framework to load. Since in a core hook there is no app or engine running that can be used to determine version dependencies, this is not possible.

This is also noted in the documentation for the load_framework method.

2 Likes

Contrary to the documentation here, a hook called from an engine cannot call self.load_framework.

The load_framework method checks for self.__parent.engine, which assumes the parent is an app.

Hooks run from an engine must use self.parent.frameworks["<framework_name>"]

Hook.load_framework version source

But the documentation is not wrong, specifies the load_framework only works on Hooks executed from apps and frameworks:

This method only works for hooks that are executed from apps and frameworks.

or am I missing something? :thinking:

2 Likes

I stand corrected. Could swear the doc was corrected too :grinning: