Open from publish in tk-multi-workfiles2 scene_operation hooks

OK I’ve got another idea in addition to what I suggested previously.

What you could do is take over the copy_file.py hook, as this will be called when the file gets copied during the open. Then in this hook you could store an env var with the source and dest paths, something like:

import os
# combine them into one env var or store them as separate env vars.
os.envion["PUBTOWORK"] = os.path.pathsep.join([source_path, target_path])

Then in the scene operations hook you can retrieve the env var, check if the target_path matches the file you’re going to open and then you know which publish it came from.

pub_to_work = os.envion.get("PUBTOWORK")

if pub_to_work:

    source, target = combined.split(os.path.pathsep)

    if file_path == target:
        # The file we are opening matches that which was previously copied,
        # now find which publish comes from the source

       # We need to grab the publish template settings so we can check the source path against 
       # the publish template, to ensure it is a publish. We're not using self.parent.get_template
       # as we might be in the middle of switching environments and the current settings
       # might not be reflective of the environment we are going to into.
       app_settings = sgtk.platform.find_app_settings(self.parent.engine.name,
                                                       "tk-multi-workfiles2",
                                                       self.parent.sgtk,
                                                       context)

        if app_settings:
            # its possible to have multiple instances of an app in a given environment
            # but we are assuming here that there is only one instance of workfiles2, 
            # so we take the first one.
            publish_template_string = app_settings[0]["settings"]["template_publish"]
            publish_template = self.sgtk.templates[publish_template_string]
            self.logger.info("Publish template found: %s" % publish_template)

            publish_template = self.parent.get_template("template_publish")
            if publish_template.validate(file_path):
                # we have a published file
                ...

I think that would work?

Thanks
Phil

2 Likes