Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Created November 8, 2023 19:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save BigRoy/c4d2d04a799513e60a36cb2d3e91d04b to your computer and use it in GitHub Desktop.
OpenPype Extractor Mixin
class ExtractorMixin:
"""OpenPype Extractor helper methods"""
def add_representation(self, instance, name,
files, staging_dir, ext=None,
output_name=None,
frame_start=None,
frame_end=None,
colorspace=False):
"""Add a representation to publish and integrate.
A representation must exist of either a single file or a
single file sequence. It can *not* contain multiple files.
For the integration to succeed the instance must provide the context
for asset, frame range, etc. even though the representation can
override some parts of it.
Arguments:
instance (pyblish.api.Instance): Publish instance
name (str): The representation name
ext (Optional[str]): Explicit extension for the output
output_name (Optional[str]): Output name suffix for the
destination file to ensure the file is unique if
multiple representations share the same extension.
Returns:
dict: Representation data for integration.
"""
if ext is None:
# TODO: Use filename
ext = name
representation = {
"name": name,
"ext": ext,
"stagingDir": staging_dir,
"files": files
}
if output_name:
representation["outputName"] = output_name
if frame_start is not None:
representation["frameStart"] = int(frame_start)
if frame_end is not None:
representation["frameEnd"] = int(frame_end)
if colorspace is not False:
self.set_representation_colorspace(representation,
colorspace=colorspace)
instance.data.setdefault("representations", []).append(
representation
)
return representation
def add_transfer(self, instance, source, destination):
"""Add a file transfer to the integration
This can be used e.g. for resource files to a publish that
do not need to be a representation of its own and avoids
any publish template renaming of the files at destination.
Arguments
instance (pyblish.api.Instance): Publish instance
source (str): Full path to source file
destination (str): Full or relative path to destination
If destination is relative path, then it will
be transfered relative to instance's `publishDir`
Returns:
tuple: The registered transfer
"""
if not os.path.isabs(destination):
destination = os.path.join(instance.data["publishDir"], destination)
transfer = src, destination
instance.data.setdefault("transfers", []).append(transfer)
return transfer
@BigRoy
Copy link
Author

BigRoy commented Nov 8, 2023

Usage example:

class Extractor(pyblish.api.InstancePlugin, ExtractorMixin):
    families = ["usd"]
    label = "Extractor"
    order = pyblish.api.ExtractorOrder

    def process(self, instance):
        staging_dir = self.staging_dir()
        files = export()
        self.add_representation(instance, "usd", files, staging_dir)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment