From the pull request.
I’ve rushed into this chain of plug-ins, assuming a few too many things were obvious, but they are not.
There are a few things happening here that makes this whole process work smoothly that I will now try to illustrate.
TLDR; it’s about collecting via constructionHistory=True
and extracting via constructionHistory=False
. Together they provide symmetry and consistency.
Problem
Let’s first nail down the problem.
import pyblish.api
cmds.polyCube(name="myCube")
cmds.polySphere(name="mySphere")
cmds.group("myCube", name="cube_GRP")
# myCube now *depends* on mySphere
cmds.connectAttr("mySphere.outMesh", "myCube.inMesh", force=True)
instance = pyblish.api.Instance("MyInstance")
instance.add("cube_GRP")
def validate(nodes):
"""Only meshes and transforms are valid"""
for node in nodes:
if cmds.nodeType(node) not in ("transform", "mesh"):
raise Exception()
try:
validate(instance)
except:
raise Exception("Validation failed before extraction!")
finally:
cmds.file(new=True, force=True)
# Now we can safely export!
cmds.select(instance)
preview = cmds.file(exportSelected=True,
preview=True,
force=True,
constructionHistory=True)
# Make sure exported nodes are still valid
try:
validate(preview)
except:
raise Exception("Validation failed after extraction!")
finally:
cmds.file(new=True, force=True)
And here’s the result.
# Validation failed after extraction!
Take a moment to consider what is happening here. Nodes are added ad-hoc to an instance, based on what we think will be considered during extraction, but really is not.
We would of course like to catch the problem before extraction.
Solution
Ensure relevant nodes are really collected, and don’t allow Maya to collect anything else during export.
import pyblish.api
cmds.polyCube(name="myCube")
cmds.polySphere(name="mySphere")
cmds.group("myCube", name="cube_GRP")
# myCube now *depends* on mySphere
cmds.connectAttr("mySphere.outMesh", "myCube.inMesh", force=True)
nodes = cmds.file(exportSelected=True,
preview=True,
force=True,
constructionHistory=True)
instance = pyblish.api.Instance("MyInstance")
instance[:] = nodes
def validate(nodes):
"""Only meshes and transforms are valid"""
for node in nodes:
if cmds.nodeType(node) not in ("transform", "mesh"):
raise Exception()
try:
validate(instance)
except:
raise Exception("Validation failed before extraction!")
finally:
cmds.file(new=True, force=True)
# Now we can safely export!
cmds.select(instance)
preview = cmds.file(exportSelected=True,
preview=True,
force=True,
constructionHistory=False)
# Make sure exported nodes are still valid
try:
validate(preview)
except:
raise Exception("Validation failed after extraction!")
finally:
cmds.file(new=True, force=True)
And here’s the new result.
# Validation failed before extraction!