Last active
May 17, 2023 05:48
-
-
Save mottosso/93399862c94f0ab4314f to your computer and use it in GitHub Desktop.
Learn Pyblish By Example - Quickstart 1.4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Full Example | |
From https://pyblish.gitbooks.io/pyblish-by-example/content/chapters/02-quickstart.html | |
Usage: | |
Copy and run this entire file in your Maya Script Editor | |
""" | |
import os | |
import shutil | |
from datetime import datetime | |
import pyblish.api | |
import pyblish_maya | |
from maya import cmds | |
# Install Pyblish for Autodesk Maya | |
pyblish_maya.setup() | |
# ---------------------------------------------------------------------------- | |
# | |
# 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.ContextPlugin): | |
"""Discover and collect available rigs into the context""" | |
order = pyblish.api.CollectorOrder | |
def process(self, context): | |
for node in cmds.ls(sets=True): | |
if not node.endswith("_RIG"): | |
continue | |
name = node.rsplit("_", 1)[0] | |
instance = context.create_instance(name, family="rig") | |
# Collect associated nodes | |
members = cmds.sets(node, query=True) | |
cmds.select([node] + members, noExpand=True) | |
instance[:] = cmds.file( | |
constructionHistory=True, | |
exportSelected=True, | |
preview=True, | |
force=True) | |
class ValidateRigContents(pyblish.api.InstancePlugin): | |
"""Ensure rig has the appropriate object sets""" | |
order = pyblish.api.ValidatorOrder | |
families = ["rig"] | |
def process(self, instance): | |
assert "controls_SEL" in instance, "%s is missing a controls set" % instance | |
assert "pointcache_SEL" in instance, "%s is missing a pointcache set" % instance | |
class ExtractRig(pyblish.api.InstancePlugin): | |
"""Serialise valid rig""" | |
order = pyblish.api.ExtractorOrder | |
families = ["rig"] | |
hosts = ["maya"] | |
def process(self, instance): | |
context = instance.context | |
dirname = os.path.dirname(context.data["currentFile"]) | |
name, family = instance.data["name"], instance.data["family"] | |
date = datetime.now().strftime("%Y%m%dT%H%M%SZ") | |
# 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" % (instance, tempfile)) | |
if not os.path.exists(tempdir): | |
os.makedirs(tempdir) | |
cmds.select(instance, noExpand=True) # `instance` a list | |
cmds.file(tempfile, | |
type="mayaAscii", | |
exportSelected=True, | |
constructionHistory=False, | |
force=True) | |
# Store reference for integration | |
instance.set_data("tempdir", tempdir) | |
class IntegrateRig(pyblish.api.InstancePlugin): | |
"""Copy files to an appropriate location where others may reach it""" | |
order = pyblish.api.IntegratorOrder | |
families = ["rig"] | |
def process(self, instance): | |
assert instance.data("tempdir"), "Can't find rig on disk, aborting.." | |
self.log.info("Computing output directory..") | |
context = instance.context | |
dirname = os.path.dirname(context.data("currentFile")) | |
root = os.path.join(dirname, "public") | |
if not os.path.exists(root): | |
os.makedirs(root) | |
version = "v%03d" % (len(os.listdir(root)) + 1) | |
src = instance.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) | |
# Run | |
import pyblish.util | |
pyblish.util.publish() |
This looks like a Python 3 issue, and I would double-check you've got the latest current version of pyblish-maya, because this one does appear to work.
You can install directly from GitHub with..
pip install git+https://github.com/pyblish/pyblish-maya.git
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I should note that simply attempting to import pyblish-maya yields the same result.