Last active
August 29, 2015 14:06
-
-
Save mottosso/124d376c46853a574c0a to your computer and use it in GitHub Desktop.
An example in how to mock instances and plugins with Pyblish
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
""" | |
This example is part of a comment from a Pyblish pull-request, #71 | |
https://github.com/abstractfactory/pyblish/pull/72 | |
""" | |
import os | |
import pyblish.backend.plugin | |
context = pyblish.backend.plugin.Context() | |
instance = context.create_instance(name='MyInstance') | |
instance.set_data('family', value='my_family') | |
instance.set_data('document_content', 'Hello World!') | |
instance.set_data('document_name', 'MyDocument.txt') | |
class ValidateMyInstance(pyblish.backend.plugin.Validator): | |
families = ['my_family'] | |
hosts = ['*'] | |
def process_instance(self, instance): | |
print "Validating instance: %s" % instance | |
assert instance.data('family') == 'my_family' | |
class ExtractDocument(pyblish.backend.plugin.Extractor): | |
families = ['my_family'] | |
hosts = ['*'] | |
def process_instance(self, instance): | |
content = instance.data('document_content') | |
name = instance.data('document_name') | |
# Since we aren't in Maya or anything, let's use the Current | |
# Working Directory as parent to our document. | |
parent_dir = instance.context.data('cwd') | |
# The current working directory is being added to the context by | |
# one of the included selector plugins. Now let's write the | |
# document to disk. | |
path = os.path.join(parent_dir, name) | |
with open(path, 'w') as f: | |
print "Writing message to %s" % path | |
f.write(content) | |
for type in ('selectors', 'validators', 'extractors', 'conforms'): | |
for plugin in pyblish.backend.plugin.discover(type): | |
plugin().process_all(context) | |
if type == 'validators': | |
# Run our mocked up plugin once validators kick in | |
ValidateMyInstance().process_all(context) | |
if type == 'extractors': | |
ExtractDocument().process_all(context) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment