Configuring scene settings on open

Being able to set up a scene automatically with the right settings within your creative software can speed up your artists and help reduce the margin of error.
For example, you may wish to ensure that the frame rate or size is set automatically so the user doesn’t have to remember to do it.

Toolkit provides the framework to help you achieve these types of customizations. There are a couple of different approaches you can use to use, but essentially it comes down to being able to run some custom code containing your scene setting logic at the appropriate time.

(To do this you first need have taken over your config).

When you open a file through the Workfiles app, it runs the scene_operations hook for the appropriate integration. This hook gets passed an operation variable which contains a string stating which operation Workfiles is wishing to run. You could expand upon the open operation code to add additional steps such as checking what the frame rate should be and then modifying the scene accordingly.

However, this only works if you open the scene in the Workfiles app. Another approach would be to take over the context_changed.py core hook, which gets called whenever the current context changes.
Be aware that as this is a core hook, it will be run in all engines, so you must first check your in the right engine before running any code that is specific to that integration.

import sgtk

from tank import get_hook_baseclass
class ContextChange(get_hook_baseclass()):

    def pre_context_change(self, current_context, next_context):
        pass

    def post_context_change(self, previous_context, current_context):
        # Get the engine we are currently running in
        engine = sgtk.platform.current_engine()
        # Check we are in the right engine
        if engine and engine.name == "tk-maya":
            if current_context.entity and context.entity["type"] == "Shot":
                # Now perform any Maya specific logic
                # For example we could query Shotgun for some specific data
                project = engine.shotgun.find_one("Project",
                                                  ["id","is",context.project["id"], 
                                                  ["sg_frame_rate"])

                # Import the Maya API now we are sure we are running in the Maya engine.
                import maya.cmds as cmds
                # Convert the Shotgun value into a format that Maya understands.
                frame_rate = "{0}fps".format(project["sg_frame_rate"])
                # Now set the Maya framerate based on the data in Shotgun
                cmds.currentUnit(time=frame_rate)
                

In the example above I’ve created a custom field in Shotgun on the Project entity called Frame rate and I’m querying that data and using it to drive my Maya frame rate.

5 Likes

Really useful post, @philip.scadding!

Could you please explain in further detail where you override the default context_changed.py tk-core hook in your config?

1 Like

Sure!

Core hooks are overridden by copying them from the tk-core bundle to the config/core/hooks folder.
Thats all there is to it when taking over a core hook.

Distributed config example:

Centralized config example:

1 Like

That’s great to know, thanks @philip.scadding !

1 Like

I have a question about this. I took over the scene_operation_tk-houdini hook and added some logic after hou.hipFile.load call. When I use File Open and open a scene the first time, it doesn’t seem to make the call I added. However, on every subsequent use of File Open, it works perfectly. What am I missing?