Instantly share code, notes, and snippets.

Embed
What would you like to do?
Learn Pyblish By Example - Quickstart 1.4
"""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()
@juggernate

This comment has been minimized.

juggernate commented Apr 8, 2016

When I try to run this on Win7 x64 in Maya2106 I get:

#   File "...\pyblish-win\lib\pyblish\modules\pyblish-base\pyblish\plugin.py", line 844, in register_plugin
#     "with this host" % plugin)
# TypeError: Plug-in <class '__main__.ExtractRig'> is not compatible with this host //
@mottosso

This comment has been minimized.

Owner

mottosso commented Apr 8, 2016

Thanks for reporting this.

This is the intended behaviour, and the reason this is happening is because the plug-in requires Maya but Maya has not been registered with Pyblish.

The registration happens automatically with the Pyblish Maya integration, when calling the pyblish_maya.setup() function, and it doesn't look like it has been properly set up. You can work around it by registering the host yourself, using pyblish.api.register_host("maya"), but it is recommended that you use the integration because it also has a few fundamental plug-ins that external Pyblish projects may come to assume, such as Pyblish Magenta.

Here's a guide for this attribute.


Note that though gists support comments, they don't support notifications. So feel free to make an issue in the book GitHub project for future problems.

I openend up an issue to make this problem more discoverable.

@mottosso

This comment has been minimized.

Owner

mottosso commented Jun 7, 2016

I've updated the file to drop the dependency on the graphical user interface, it now works with just pyblish-base.

@davidlatwe

This comment has been minimized.

davidlatwe commented Aug 5, 2017

Hi, after run this, I got

Can't find rig on disk, aborting..

and I print out the context result, I found

{'action': None,
'duration': 0.0,
'error': KeyError('currentFile',),
'instance': pyblish.plugin.Instance("Bruce"),
'plugin': <class 'pyblish.plugin.ExtractRig'>,
'records': [],
'success': False}

seems lost one line in CollectRig plugin

context.data["currentFile"] = cmds.file(q= 1, sn= 1)

after I add this line it worked.

Is this fix correct ? Thanks.

@mottosso

This comment has been minimized.

Owner

mottosso commented Aug 5, 2017

Thanks for reporting this. The reason you are seeing this is because you need Pyblish Maya in order to run this example. currentFile is added by one of the plugins included in that integration.

@davidlatwe

This comment has been minimized.

davidlatwe commented Aug 5, 2017

Oh I see that, Thanks!

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