Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2846 from pypeclub/deature/OP-2839_Basic-event-sy…
Browse files Browse the repository at this point in the history
…stem

General: Basic event system
  • Loading branch information
iLLiCiTiT authored Mar 11, 2022
2 parents 4844402 + c274e64 commit e635d81
Show file tree
Hide file tree
Showing 20 changed files with 379 additions and 145 deletions.
7 changes: 4 additions & 3 deletions openpype/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
Anatomy,
filter_pyblish_plugins,
set_plugin_attributes_from_settings,
change_timer_to_current_context
change_timer_to_current_context,
register_event_callback,
)

pyblish = avalon = _original_discover = None
Expand Down Expand Up @@ -128,10 +129,10 @@ def modified_emit(obj, record):
avalon.discover = patched_discover
pipeline.discover = patched_discover

avalon.on("taskChanged", _on_task_change)
register_event_callback("taskChanged", _on_task_change)


def _on_task_change(*args):
def _on_task_change():
change_timer_to_current_context()


Expand Down
3 changes: 2 additions & 1 deletion openpype/hosts/aftereffects/api/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from openpype.api import Logger
from openpype.pipeline import LegacyCreator
import openpype.hosts.aftereffects
from openpype.lib import register_event_callback

from .launch_logic import get_stub

Expand Down Expand Up @@ -74,7 +75,7 @@ def install():
"instanceToggled", on_pyblish_instance_toggled
)

avalon.api.on("application.launched", application_launch)
register_event_callback("application.launched", application_launch)


def uninstall():
Expand Down
25 changes: 15 additions & 10 deletions openpype/hosts/blender/api/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

from openpype.pipeline import LegacyCreator
from openpype.api import Logger
from openpype.lib import (
register_event_callback,
emit_event
)
import openpype.hosts.blender

HOST_DIR = os.path.dirname(os.path.abspath(openpype.hosts.blender.__file__))
Expand Down Expand Up @@ -51,8 +55,9 @@ def install():

lib.append_user_scripts()

avalon.api.on("new", on_new)
avalon.api.on("open", on_open)
register_event_callback("new", on_new)
register_event_callback("open", on_open)

_register_callbacks()
_register_events()

Expand Down Expand Up @@ -114,32 +119,32 @@ def set_start_end_frames():
scene.render.resolution_y = resolution_y


def on_new(arg1, arg2):
def on_new():
set_start_end_frames()


def on_open(arg1, arg2):
def on_open():
set_start_end_frames()


@bpy.app.handlers.persistent
def _on_save_pre(*args):
avalon.api.emit("before_save", args)
emit_event("before.save")


@bpy.app.handlers.persistent
def _on_save_post(*args):
avalon.api.emit("save", args)
emit_event("save")


@bpy.app.handlers.persistent
def _on_load_post(*args):
# Detect new file or opening an existing file
if bpy.data.filepath:
# Likely this was an open operation since it has a filepath
avalon.api.emit("open", args)
emit_event("open")
else:
avalon.api.emit("new", args)
emit_event("new")

ops.OpenFileCacher.post_load()

Expand Down Expand Up @@ -170,7 +175,7 @@ def _remove_handler(handlers: List, callback: Callable):
log.info("Installed event handler _on_load_post...")


def _on_task_changed(*args):
def _on_task_changed():
"""Callback for when the task in the context is changed."""

# TODO (jasper): Blender has no concept of projects or workspace.
Expand All @@ -187,7 +192,7 @@ def _on_task_changed(*args):
def _register_events():
"""Install callbacks for specific events."""

avalon.api.on("taskChanged", _on_task_changed)
register_event_callback("taskChanged", _on_task_changed)
log.info("Installed event callback for 'taskChanged'...")


Expand Down
5 changes: 3 additions & 2 deletions openpype/hosts/harmony/api/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from avalon.pipeline import AVALON_CONTAINER_ID

from openpype import lib
from openpype.lib import register_event_callback
from openpype.pipeline import LegacyCreator
import openpype.hosts.harmony
import openpype.hosts.harmony.api as harmony
Expand Down Expand Up @@ -130,7 +131,7 @@ def check_inventory():
harmony.send({"function": "PypeHarmony.message", "args": msg})


def application_launch():
def application_launch(event):
"""Event that is executed after Harmony is launched."""
# FIXME: This is breaking server <-> client communication.
# It is now moved so it it manually called.
Expand Down Expand Up @@ -188,7 +189,7 @@ def install():
"instanceToggled", on_pyblish_instance_toggled
)

avalon.api.on("application.launched", application_launch)
register_event_callback("application.launched", application_launch)


def uninstall():
Expand Down
6 changes: 3 additions & 3 deletions openpype/hosts/hiero/api/events.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import os
import hiero.core.events
import avalon.api as avalon
from openpype.api import Logger
from .lib import (
sync_avalon_data_to_workfile,
launch_workfiles_app,
selection_changed_timeline,
before_project_save
before_project_save,
register_event_callback
)
from .tags import add_tags_to_workfile
from .menu import update_menu_task_label
Expand Down Expand Up @@ -126,5 +126,5 @@ def register_events():
"""

# if task changed then change notext of hiero
avalon.on("taskChanged", update_menu_task_label)
register_event_callback("taskChanged", update_menu_task_label)
log.info("Installed event callback for 'taskChanged'..")
2 changes: 1 addition & 1 deletion openpype/hosts/hiero/api/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
self._change_context_menu = None


def update_menu_task_label(*args):
def update_menu_task_label():
"""Update the task label in Avalon menu to current session"""

object_name = self._change_context_menu
Expand Down
30 changes: 16 additions & 14 deletions openpype/hosts/houdini/api/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
from openpype.hosts.houdini.api import lib

from openpype.lib import (
any_outdated
register_event_callback,
emit_event,
any_outdated,
)

from .lib import get_asset_fps
Expand Down Expand Up @@ -52,11 +54,11 @@ def install():
avalon.api.register_plugin_path(LegacyCreator, CREATE_PATH)

log.info("Installing callbacks ... ")
# avalon.on("init", on_init)
avalon.api.before("save", before_save)
avalon.api.on("save", on_save)
avalon.api.on("open", on_open)
avalon.api.on("new", on_new)
# register_event_callback("init", on_init)
register_event_callback("before.save", before_save)
register_event_callback("save", on_save)
register_event_callback("open", on_open)
register_event_callback("new", on_new)

pyblish.api.register_callback(
"instanceToggled", on_pyblish_instance_toggled
Expand Down Expand Up @@ -102,13 +104,13 @@ def _register_callbacks():

def on_file_event_callback(event):
if event == hou.hipFileEventType.AfterLoad:
avalon.api.emit("open", [event])
emit_event("open")
elif event == hou.hipFileEventType.AfterSave:
avalon.api.emit("save", [event])
emit_event("save")
elif event == hou.hipFileEventType.BeforeSave:
avalon.api.emit("before_save", [event])
emit_event("before.save")
elif event == hou.hipFileEventType.AfterClear:
avalon.api.emit("new", [event])
emit_event("new")


def get_main_window():
Expand Down Expand Up @@ -230,11 +232,11 @@ def ls():
yield data


def before_save(*args):
def before_save():
return lib.validate_fps()


def on_save(*args):
def on_save():

log.info("Running callback on save..")

Expand All @@ -243,7 +245,7 @@ def on_save(*args):
lib.set_id(node, new_id, overwrite=False)


def on_open(*args):
def on_open():

if not hou.isUIAvailable():
log.debug("Batch mode detected, ignoring `on_open` callbacks..")
Expand Down Expand Up @@ -280,7 +282,7 @@ def _on_show_inventory():
dialog.show()


def on_new(_):
def on_new():
"""Set project resolution and fps when create a new file"""

if hou.hipFile.isLoadingHipFile():
Expand Down
47 changes: 27 additions & 20 deletions openpype/hosts/maya/api/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

import openpype.hosts.maya
from openpype.tools.utils import host_tools
from openpype.lib import any_outdated
from openpype.lib import (
any_outdated,
register_event_callback,
emit_event
)
from openpype.lib.path_tools import HostDirmap
from openpype.pipeline import LegacyCreator
from openpype.hosts.maya.lib import copy_workspace_mel
Expand Down Expand Up @@ -55,7 +59,7 @@ def install():
log.info(PUBLISH_PATH)

log.info("Installing callbacks ... ")
avalon.api.on("init", on_init)
register_event_callback("init", on_init)

# Callbacks below are not required for headless mode, the `init` however
# is important to load referenced Alembics correctly at rendertime.
Expand All @@ -69,12 +73,12 @@ def install():

menu.install()

avalon.api.on("save", on_save)
avalon.api.on("open", on_open)
avalon.api.on("new", on_new)
avalon.api.before("save", on_before_save)
avalon.api.on("taskChanged", on_task_changed)
avalon.api.on("before.workfile.save", before_workfile_save)
register_event_callback("save", on_save)
register_event_callback("open", on_open)
register_event_callback("new", on_new)
register_event_callback("before.save", on_before_save)
register_event_callback("taskChanged", on_task_changed)
register_event_callback("workfile.save.before", before_workfile_save)


def _set_project():
Expand Down Expand Up @@ -137,7 +141,7 @@ def _register_callbacks():


def _on_maya_initialized(*args):
avalon.api.emit("init", args)
emit_event("init")

if cmds.about(batch=True):
log.warning("Running batch mode ...")
Expand All @@ -148,15 +152,15 @@ def _on_maya_initialized(*args):


def _on_scene_new(*args):
avalon.api.emit("new", args)
emit_event("new")


def _on_scene_save(*args):
avalon.api.emit("save", args)
emit_event("save")


def _on_scene_open(*args):
avalon.api.emit("open", args)
emit_event("open")


def _before_scene_save(return_code, client_data):
Expand All @@ -166,7 +170,10 @@ def _before_scene_save(return_code, client_data):
# in order to block the operation.
OpenMaya.MScriptUtil.setBool(return_code, True)

avalon.api.emit("before_save", [return_code, client_data])
emit_event(
"before.save",
{"return_code": return_code}
)


def uninstall():
Expand Down Expand Up @@ -343,7 +350,7 @@ def containerise(name,
return container


def on_init(_):
def on_init():
log.info("Running callback on init..")

def safe_deferred(fn):
Expand Down Expand Up @@ -384,12 +391,12 @@ def _fn():
safe_deferred(override_toolbox_ui)


def on_before_save(return_code, _):
def on_before_save():
"""Run validation for scene's FPS prior to saving"""
return lib.validate_fps()


def on_save(_):
def on_save():
"""Automatically add IDs to new nodes
Any transform of a mesh, without an existing ID, is given one
Expand All @@ -407,7 +414,7 @@ def on_save(_):
lib.set_id(node, new_id, overwrite=False)


def on_open(_):
def on_open():
"""On scene open let's assume the containers have changed."""

from Qt import QtWidgets
Expand Down Expand Up @@ -455,7 +462,7 @@ def _on_show_inventory():
dialog.show()


def on_new(_):
def on_new():
"""Set project resolution and fps when create a new file"""
log.info("Running callback on new..")
with lib.suspended_refresh():
Expand All @@ -471,7 +478,7 @@ def on_new(_):
lib.set_context_settings()


def on_task_changed(*args):
def on_task_changed():
"""Wrapped function of app initialize and maya's on task changed"""
# Run
menu.update_menu_task_label()
Expand Down Expand Up @@ -509,7 +516,7 @@ def on_task_changed(*args):


def before_workfile_save(event):
workdir_path = event.workdir_path
workdir_path = event["workdir_path"]
if workdir_path:
copy_workspace_mel(workdir_path)

Expand Down
Loading

0 comments on commit e635d81

Please sign in to comment.