Quickstart

Quickstart

Before we move on, let me throw you into the deep end and show you a full example.
Don't worry too much if it doesn't make sense just now, the rest of the examples are dedicated to explaining each feature in great detail.
The examples will be written primarily for Autodesk Maya, but should be easily readable and it's concepts applicable to any 3d content creation software. There is a slight simplification involved, for more clarity, but overall this represents a real-world implementation of a fully featured publishing pipeline with Pyblish.

The Deep End

The example uses the 2 available superclasses in Pyblish - ContextPlugin and InstancePlugin. The order in which these plug-ins are run is controlled by an integer attribute called order, with 4 default values.
  1. 1.
    Collection
  2. 2.
    Validation
  3. 3.
    Extraction
  4. 4.
    Integration
image
1. collect_rig.py
Gather information to validate and export.
1
import pyblish.api
2
from maya import cmds
3
4
class CollectRig(pyblish.api.ContextPlugin):
5
"""Discover and collect available rigs into the context"""
6
7
order = pyblish.api.CollectorOrder
8
9
def process(self, context):
10
for node in cmds.ls(sets=True):
11
if not node.endswith("_RIG"):
12
continue
13
14
name = node.rsplit("_", 1)[0]
15
instance = context.create_instance(name, family="rig")
16
17
# Collect associated nodes
18
members = cmds.sets(node, query=True)
19
cmds.select([node] + members, noExpand=True)
20
instance[:] = cmds.file(
21
constructionHistory=True,
22
exportSelected=True,
23
preview=True,
24
force=True)
Copied!
2. validate_rig.py
Ensure the correctness of collected information.
1
import pyblish.api
2
3
class ValidateRigContents(pyblish.api.InstancePlugin):
4
"""Ensure rig has the appropriate object sets"""
5
6
order = pyblish.api.ValidatorOrder
7
families = ["rig"]
8
9
def process(self, instance):
10
assert "controls_SEL" in instance, "%s is missing a controls set" % instance
11
assert "pointcache_SEL" in instance, "%s is missing a pointcache set" % instance
Copied!
3. extract_rig.py
Write to disk.
1
import os
2
import shutil
3
from datetime import datetime
4
5
import pyblish.api
6
from maya import cmds
7
8
class ExtractRig(pyblish.api.InstancePlugin):
9
"""Serialise valid rig"""
10
11
order = pyblish.api.ExtractorOrder
12
families = ["rig"]
13
hosts = ["maya"]
14
15
def process(self, instance):
16
context = instance.context
17
dirname = os.path.dirname(context.data["currentFile"])
18
name, family = instance.data["name"], instance.data["family"]
19
date = datetime.now().strftime("%Y%m%dT%H%M%SZ")
20
21
# Find a temporary directory with support for publishing multiple times.
22
tempdir = os.path.join(dirname, "temp", date, family, name)
23
tempfile = os.path.join(tempdir, name + ".ma")
24
25
self.log.info("Exporting %s to %s" % (instance, tempfile))
26
27
if not os.path.exists(tempdir):
28
os.makedirs(tempdir)
29
30
cmds.select(instance, noExpand=True) # `instance` a list
31
cmds.file(tempfile,
32
type="mayaAscii",
33
exportSelected=True,
34
constructionHistory=False,
35
force=True)
36
37
# Store reference for integration
38
instance.set_data("tempdir", tempdir)
Copied!
4. integrate_rig.py
Integrate written information into the pipeline, as per convention.
1
import os
2
import shutil
3
4
import pyblish.api
5
6
class IntegrateRig(pyblish.api.InstancePlugin):
7
"""Copy files to an appropriate location where others may reach it"""
8
9
order = pyblish.api.IntegratorOrder
10
families = ["rig"]
11
12
def process(self, instance):
13
assert instance.data("tempdir"), "Can't find rig on disk, aborting.."
14
15
self.log.info("Computing output directory..")
16
context = instance.context
17
dirname = os.path.dirname(context.data("currentFile"))
18
root = os.path.join(dirname, "public")
19
20
if not os.path.exists(root):
21
os.makedirs(root)
22
23
version = "v%03d" % (len(os.listdir(root)) + 1)
24
25
src = instance.data("tempdir")
26
dst = os.path.join(root, version)
27
28
self.log.info("Copying %s to %s.." % (src, dst))
29
30
shutil.copytree(src, dst)
31
self.log.info("Copied successfully!")
Copied!

That's a lot

I'm sure you have lots of questions, but don't worry. This is the part where we dig into exactly how we get to this point, and what all of this really means.

Test it out

It isn't necessary to run this on your own, but if you want to give it a try, here's what you do.
  1. 1.
    Install Pyblish for Maya
  2. 2.
    Copy/paste the full source code from here into your Maya script editor
  3. 3.
    Run it
This is what you should expect.
1
...
2
import pyblish.util
3
pyblish.util.publish()
4
pyblish: Registered C:\pythonpath\pyblish_maya\plugins
5
Pyblish loaded successfully.
6
# pyblish.ExtractRig : Exporting Bruce to C:\...\maya\scenes\temp\20180318T091805Z\rig\Bruce\Bruce.ma #
7
# pyblish.IntegrateRig : Computing output directory.. #
8
# pyblish.IntegrateRig : Copying C:\...\maya\scenes\temp\20180318T091805Z\rig\Bruce to C:\...\maya\scenes\public\v002.. #
9
# pyblish.IntegrateRig : Copied successfully! #
Copied!
If not, let us know!

Alternative examples

Looking for a full example for your DCC?
These will turn into links as they become available, watch this space!
  • 3ds Max
  • Softimage
  • Houdini
  • Nuke
  • Fusion
  • Clarisse
  • Modo
  • Blender
Last modified 1yr ago