Skip to content

Instantly share code, notes, and snippets.

@kporangehat
Last active April 8, 2021 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kporangehat/7db396a0a14f8537fd15d90ce74ea2d6 to your computer and use it in GitHub Desktop.
Save kporangehat/7db396a0a14f8537fd15d90ce74ea2d6 to your computer and use it in GitHub Desktop.
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(
publish["sg_publish"]["path"]["local_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 = (
publish["sg_publish"]["task"]
or publish["sg_publish"]["entity"]
or publish["sg_publish"]["project"]
)
app.sgtk.create_filesystem_structure(
ctx_entity["type"], ctx_entity["id"], app.engine.name
)
app.logger.debug(
"[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