Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Shotgun Toolkit filter_publishes hook for tk-multi-workfiles2. This hook ensures path cache entries are generated for any publishes that were generated using `register_publish()` for entities that may not have folders created yet.
import os
import re
import sgtk
HookClass = sgtk.get_hook_baseclass()
class FilterPublishes(HookClass):
Hook that can be used to filter the list of publishes returned from Shotgun for the current
Work area
def execute(self, publishes, **kwargs):
Main hook entry point
:param publishes: List of dictionaries
A list of dictionaries for the current work area within the app. Each
item in the list is a Dictionary of the form:
"sg_publish" : {Shotgun entity dictionary for a PublishedFile}
:returns: The filtered list of dictionaries of the same form as the input
'publishes' list
app = self.parent
# For files that are published manually using register_publish(), there are no path
# cache entries created. Typically any apps/scripts would ensure the target folder exists,
# copy the file, and then create a PublishedFile record using Toolkit's `register_publish()`
# command. However, the File Manager relies on the path cache to be able to resolve context
# for PublishedFiles (for some reason?). So here we also check to see if the file's context
# has a folder registered, and if not, register it so it will show up in the File Manager.
if publishes:
publish = publishes[0]
# Try and get the context from the Publish path of the first publish
ctx = app.sgtk.context_from_path(
# If we don't get at least an entity from the context, then folders need to be created.
# NOTE: This assumes no work is published on the Project level. The path will default
# to the Project context if there are no path cache entries for this Publish.
if not ctx.entity:
# Get the most specific entity for this context (Task > Entity > Project)
ctx_entity = (
or publish["sg_publish"]["entity"]
or publish["sg_publish"]["project"]
ctx_entity["type"], ctx_entity["id"],
"[filter_publishes] Path cache entries missing. Generated folders for %s"
% (ctx_entity)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment