Pyblish Callbacks


Just giving a heads-up to some of @tokejepsen’s awesome work on implementing callbacks in Pyblish!

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!


I saw a lot of discussion and closing of issues going on with @marcus and @tokejepsen. Just wanted to say thanks and top notch collaboration guys!


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))["instanceToggled"] = instanceToggled

def instance_toggled(instance, new_value, old_value):["instanceToggled"](instance, new_value)

api.register_callback("instanceToggled", instance_toggled)


Thought I would share it :slight_smile:




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