Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Created February 27, 2016 12:32
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 BigRoy/e5c6a73fa2d6e09fa8a1 to your computer and use it in GitHub Desktop.
Save BigRoy/e5c6a73fa2d6e09fa8a1 to your computer and use it in GitHub Desktop.
A Pyblish integrator draft for Pyblish Magenta that integrates files into a "publish" folder next to the current file.
import os
import shutil
import pyblish.api
import pyblish.util
import pyblish_magenta.api
class IntegrateAssetsSimple(pyblish.api.Integrator):
"""Name and position instances on disk for shots
Integrates to a "publish" folder next to the current file.
It formats to
publish/{subset}/{instance}/{version}/..
Assumptions:
- Each extracted instance is 1 file (no directories)
"""
label = "Assets (Simple)"
def process(self, context, instance):
self.log.info("Integrating..")
data = {
"subset": instance.data.get("subset", "default"),
"instance": instance.data["name"]
}
current_file = context.data["currentFile"]
root = os.path.dirname(current_file)
if not data["instance"] or not data["subset"]:
# if instance or data is empty
raise RuntimeError("Instance name and Subset must not be empty: "
"{0} and {1}".format(data["instance"],
data["subset"]))
path = os.path.join(
root,
"publish",
"{subset}",
"{instance}").format(**data)
# Ensure unique version
try:
versions = os.listdir(path)
except:
versions = []
next_version = pyblish_magenta.api.find_next_version(versions)
path = os.path.join(
path, pyblish_magenta.api.format_version(next_version))
# Store reference for upcoming plug-ins
instance.data["integrationDir"] = path
instance.data["integrationVersion"] = next_version # 001
data["version"] = pyblish_magenta.api.format_version(next_version)
try:
if not os.path.exists(path):
os.makedirs(path)
self.log.info("Moving files to %s" % path)
tmp = instance.data["extractDir"]
for src in (os.path.join(tmp, f) for f in os.listdir(tmp)):
# TODO(marcus): Consider files without extension
self.log.warning("Integrating %s" % src)
data["ext"] = src.split(".", 1)[-1]
# Subset may contain subdirectories in its name
data["subsetName"] = data["subset"].replace("/", "_")
dst = os.path.join(path, "{subsetName}_"
"{instance}_"
"{version}.{ext}".format(
**data))
self.log.info("\"%s\" -> \"%s\"" % (src, dst))
shutil.copyfile(src, dst)
except OSError as e:
# If, for whatever reason, this instance did not get written.
instance.data.pop("integrationDir")
raise e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment