Skip to content

Instantly share code, notes, and snippets.

@mottosso
Last active August 29, 2015 14:26
Show Gist options
  • Save mottosso/43ae098312ef40b3f3c8 to your computer and use it in GitHub Desktop.
Save mottosso/43ae098312ef40b3f3c8 to your computer and use it in GitHub Desktop.
Learn Pyblish By Example - Quickstart
"""Full Example
From http://forums.pyblish.com/t/learning-pyblish-by-example/108/3
Usage:
Copy and run this entire file in your Maya Script Editor
"""
import os
import shutil
import pyblish.api
from maya import cmds
# ----------------------------------------------------------------------------
#
# Generate Scene
#
# ----------------------------------------------------------------------------
cmds.file(new=True, force=True)
cmds.polyCube(name="geometry")
cmds.sets(name="pointcache_SEL")
cmds.circle(name="control")
cmds.sets(name="controls_SEL")
cmds.select(["geometry", "control"])
cmds.group(name="Bruce_GRP")
cmds.select(["Bruce_GRP", "pointcache_SEL", "controls_SEL"], noExpand=True)
cmds.sets(name="Bruce_RIG")
# Save the scene
cmds.file(rename="scene.ma")
cmds.file(save=True, type="mayaAscii")
# ----------------------------------------------------------------------------
#
# Publish
#
# ----------------------------------------------------------------------------
class CollectRig(pyblish.api.Collector):
"""Discover and collect available rigs into the context"""
def process(self, context):
for node in cmds.ls(sets=True):
if not node.endswith("_RIG"):
continue
name = node.rsplit("_", 1)[0]
asset = context.create_asset(name, family="rig")
# Collect associated nodes
members = cmds.sets(node, query=True)
cmds.select([node] + members, noExpand=True)
asset[:] = cmds.file(
constructionHistory=True,
exportSelected=True,
preview=True,
force=True)
class ValidateRigContents(pyblish.api.Validator):
"""Ensure rig has the appropriate object sets"""
families = ["rig"]
def process(self, asset):
assert "controls_SEL" in asset, "%s is missing a controls set" % asset
assert "pointcache_SEL" in asset, "%s is missing a pointcache set" % asset
class ExtractRig(pyblish.api.Extractor):
"""Serialise valid rig"""
families = ["rig"]
hosts = ["maya"]
def process(self, context, asset):
dirname = os.path.dirname(context.data("currentFile"))
name, family = asset.data("name"), asset.data("family")
date = pyblish.api.format_filename(context.data("date"))
# Find a temporary directory with support for publishing multiple times.
tempdir = os.path.join(dirname, "temp", date, family, name)
tempfile = os.path.join(tempdir, name + ".ma")
self.log.info("Exporting %s to %s" % (asset, tempfile))
if not os.path.exists(tempdir):
os.makedirs(tempdir)
cmds.select(asset, noExpand=True) # `asset` a list
cmds.file(tempfile,
type="mayaAscii",
exportSelected=True,
constructionHistory=False,
force=True)
# Store reference for integration
asset.set_data("tempdir", tempdir)
class IntegrateRig(pyblish.api.Integrator):
"""Copy files to an appropriate location where others may reach it"""
families = ["rig"]
def process(self, context, asset):
assert asset.data("tempdir"), "Can't find rig on disk, aborting.."
self.log.info("Computing output directory..")
dirname = os.path.dirname(context.data("currentFile"))
tempdir = asset.data("tempdir")
root = os.path.join(dirname, "public")
if not os.path.exists(root):
os.makedirs(root)
version = "v%03d" % (len(os.listdir(root)) + 1)
src = asset.data("tempdir")
dst = os.path.join(root, version)
self.log.info("Copying %s to %s.." % (src, dst))
shutil.copytree(src, dst)
self.log.info("Copied successfully!")
pyblish.api.register_plugin(CollectRig)
pyblish.api.register_plugin(ValidateRigContents)
pyblish.api.register_plugin(ExtractRig)
pyblish.api.register_plugin(IntegrateRig)
import pyblish_maya
pyblish_maya.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment