The goal is to be able to “connect” to events occurring throughout Pyblish, foremost the event of an item being toggled in the GUI such that an appropriate action can be made in the host - like, persisting the information until the next time the GUI refreshes. But there are lots of events worth connecting to!
One of the main use cases for the callbacks has been for me, persisting the publish state to the DCC scene. This is being done so you can potentially publish headless, but its also a convenience of not having to enable the same instances every time you want to publish.
One of my biggest issues with this system has been the separated code of collecting (creating) instances and how to persist the data to the DCC.
Lately I have been using the method of embedding the toggling logic into the instance, so all code is together.
Example in Maya:
from pyblish import api
import pyblish_maya
class collect(api.ContextPlugin):
order = api.CollectorOrder
def process(self, context):
instance = context.create_instance(name="A")
def instanceToggled(instance, value):
print("Toggling the instance with value: \"{0}\"".format(value))
instance.data["instanceToggled"] = instanceToggled
def instance_toggled(instance, new_value, old_value):
instance.data["instanceToggled"](instance, new_value)
api.deregister_all_plugins()
api.deregister_all_callbacks()
api.register_callback("instanceToggled", instance_toggled)
api.register_plugin(collect)
pyblish_maya.show()
You could make this a standardized way of defining how to toggling an instance, with a built-in callback that looks for the instanceToggled data member.
I forgot to say that you can persist the publish state of each instance with this plugin:
from pyblish import api
class ExtractPublishState(api.ContextPlugin):
"""Extracts the publish state of all instances
Instances needs to have a "instanceToggled" method stored as data member.
"""
order = api.ExtractorOrder
label = "Publish State"
def process(self, context):
for instance in context:
if "instanceToggled" not in instance.data.keys():
continue
instance.data["instanceToggled"](
instance, instance.data["publish"]
)