From 4ae8f277b99298018b999c1539e051fd82eb4de7 Mon Sep 17 00:00:00 2001 From: MustafaJafar Date: Wed, 29 May 2024 22:29:13 +0300 Subject: [PATCH 1/7] add 'pyblish_debug_stepper' tool --- .../pyblish_debug_stepper.py | 269 ++++++++++++++++++ 1 file changed, 269 insertions(+) create mode 100644 client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py diff --git a/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py b/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py new file mode 100644 index 0000000000..e6cf3c69d6 --- /dev/null +++ b/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py @@ -0,0 +1,269 @@ +""" +Brought from https://gist.github.com/BigRoy/1972822065e38f8fae7521078e44eca2 +Code Credits: [BigRoy](https://github.com/BigRoy) + +Requirement: + This tool requires some modification in ayon-core. + Add the following two lines in sa similar fashion to this commit + https://github.com/ynput/OpenPype/commit/6a0ce21aa1f8cb17452fe066aa15134d22fda440 + i.e. Add them just after + https://github.com/ynput/ayon-core/blob/8366d2e8b4003a252b8da822f7e38c6db08292b4/client/ayon_core/tools/publisher/control.py#L2483-L2487 + + ``` + result["context"] = self._publish_context + pyblish.api.emit("pluginProcessedCustom", result=result) + ``` + + This modification should be temporary till the following PR get merged and released. + https://github.com/pyblish/pyblish-base/pull/401 + +How it works: + It registers a callback function `on_plugin_processed` + when event `pluginProcessedCustom` is emitted. + The logic of this function is roughly: + 1. Pauses the publishing. + 2. Collects some info about the plugin. + 3. Shows that info to the tool's window. + 4. Continues publishing on clicking `step` button. + +How to use it: + 1. Launch the tool from AYON experimental tools window. + 2. Launch the publisher tool and click validate. + 3. Click Step to run plugins one by one. + +Note: + It won't work when triggering validation from code as our custom event lives inside ayon-core. + But, It should work when the mentioned PR above (#401) get merged and released. + +""" + +import copy +import json +from qtpy import QtWidgets, QtCore, QtGui + +import pyblish.api +from ayon_core import style + +TAB = 4* " " +HEADER_SIZE = "15px" + +KEY_COLOR = QtGui.QColor("#ffffff") +NEW_KEY_COLOR = QtGui.QColor("#00ff00") +VALUE_TYPE_COLOR = QtGui.QColor("#ffbbbb") +NEW_VALUE_TYPE_COLOR = QtGui.QColor("#ff4444") +VALUE_COLOR = QtGui.QColor("#777799") +NEW_VALUE_COLOR = QtGui.QColor("#DDDDCC") +CHANGED_VALUE_COLOR = QtGui.QColor("#CCFFCC") + +MAX_VALUE_STR_LEN = 100 + + +def failsafe_deepcopy(data): + """Allow skipping the deepcopy for unsupported types""" + try: + return copy.deepcopy(data) + except TypeError: + if isinstance(data, dict): + return { + key: failsafe_deepcopy(value) + for key, value in data.items() + } + elif isinstance(data, list): + return data.copy() + return data + + +class DictChangesModel(QtGui.QStandardItemModel): + # TODO: Replace this with a QAbstractItemModel + def __init__(self, *args, **kwargs): + super(DictChangesModel, self).__init__(*args, **kwargs) + self._data = {} + + columns = ["Key", "Type", "Value"] + self.setColumnCount(len(columns)) + for i, label in enumerate(columns): + self.setHeaderData(i, QtCore.Qt.Horizontal, label) + + def _update_recursive(self, data, parent, previous_data): + for key, value in data.items(): + + # Find existing item or add new row + parent_index = parent.index() + for row in range(self.rowCount(parent_index)): + # Update existing item if it exists + index = self.index(row, 0, parent_index) + if index.data() == key: + item = self.itemFromIndex(index) + type_item = self.itemFromIndex(self.index(row, 1, parent_index)) + value_item = self.itemFromIndex(self.index(row, 2, parent_index)) + break + else: + item = QtGui.QStandardItem(key) + type_item = QtGui.QStandardItem() + value_item = QtGui.QStandardItem() + parent.appendRow([item, type_item, value_item]) + + # Key + key_color = NEW_KEY_COLOR if key not in previous_data else KEY_COLOR + item.setData(key_color, QtCore.Qt.ForegroundRole) + + # Type + type_str = type(value).__name__ + type_color = VALUE_TYPE_COLOR + if key in previous_data and type(previous_data[key]).__name__ != type_str: + type_color = NEW_VALUE_TYPE_COLOR + + type_item.setText(type_str) + type_item.setData(type_color, QtCore.Qt.ForegroundRole) + + # Value + value_changed = False + if key not in previous_data or previous_data[key] != value: + value_changed = True + value_color = NEW_VALUE_COLOR if value_changed else VALUE_COLOR + + value_item.setData(value_color, QtCore.Qt.ForegroundRole) + if value_changed: + value_str = str(value) + if len(value_str) > MAX_VALUE_STR_LEN: + value_str = value_str[:MAX_VALUE_STR_LEN] + "..." + value_item.setText(value_str) + # Preferably this is deferred to only when the data gets requested + # since this formatting can be slow for very large data sets like + # project settings and system settings + # This will also be MUCH MUCH faster if we don't clear the items on each update + # but only updated/add/remove changed items so that this also runs much less often + value_item.setData(json.dumps(value, default=str, indent=4), QtCore.Qt.ToolTipRole) + + + if isinstance(value, dict): + previous_value = previous_data.get(key, {}) + if previous_data.get(key) != value: + # Update children if the value is not the same as before + self._update_recursive(value, parent=item, previous_data=previous_value) + else: + # TODO: Ensure all children are updated to be not marked as 'changed' + # in the most optimal way possible + self._update_recursive(value, parent=item, previous_data=previous_value) + + self._data = data + + def update(self, data): + parent = self.invisibleRootItem() + + data = failsafe_deepcopy(data) + previous_data = self._data + self._update_recursive(data, parent, previous_data) + self._data = data # store previous data for next update + + +class DebugUI(QtWidgets.QDialog): + + def __init__(self, parent=None): + super(DebugUI, self).__init__(parent=parent) + self.setStyleSheet(style.load_stylesheet()) + + self._set_window_title() + self.setWindowFlags( + QtCore.Qt.Window + | QtCore.Qt.CustomizeWindowHint + | QtCore.Qt.WindowTitleHint + | QtCore.Qt.WindowMinimizeButtonHint + | QtCore.Qt.WindowCloseButtonHint + | QtCore.Qt.WindowStaysOnTopHint + ) + + layout = QtWidgets.QVBoxLayout(self) + text_edit = QtWidgets.QTextEdit() + text_edit.setFixedHeight(65) + font = QtGui.QFont("NONEXISTENTFONT") + font.setStyleHint(font.TypeWriter) + text_edit.setFont(font) + text_edit.setLineWrapMode(text_edit.NoWrap) + + step = QtWidgets.QPushButton("Step") + step.setEnabled(False) + + model = DictChangesModel() + proxy = QtCore.QSortFilterProxyModel() + proxy.setSourceModel(model) + view = QtWidgets.QTreeView() + view.setModel(proxy) + view.setSortingEnabled(True) + + layout.addWidget(text_edit) + layout.addWidget(view) + layout.addWidget(step) + + step.clicked.connect(self.on_step) + + self._pause = False + self.model = model + self.proxy = proxy + self.view = view + self.text = text_edit + self.step = step + self.resize(700, 500) + + self._previous_data = {} + + + + def _set_window_title(self, plugin=None): + title = "Pyblish Debug Stepper" + if plugin is not None: + plugin_label = plugin.label or plugin.__name__ + title += f" | {plugin_label}" + self.setWindowTitle(title) + + def pause(self, state): + self._pause = state + self.step.setEnabled(state) + + def on_step(self): + self.pause(False) + + def showEvent(self, event): + print("Registering callback..") + pyblish.api.register_callback("pluginProcessedCustom", # "pluginProcessed" + self.on_plugin_processed) + + def hideEvent(self, event): + self.pause(False) + print("Deregistering callback..") + pyblish.api.deregister_callback("pluginProcessedCustom", # "pluginProcessed" + self.on_plugin_processed) + + def on_plugin_processed(self, result): + self.pause(True) + + self._set_window_title(plugin=result["plugin"]) + + print(10*"<" ,result["plugin"].__name__, 10*">") + + plugin_order = result["plugin"].order + plugin_name = result["plugin"].__name__ + duration = result['duration'] + plugin_instance = result["instance"] + context = result["context"] + + msg = "" + msg += f"Order: {plugin_order}
" + msg += f"Plugin: {plugin_name}" + if plugin_instance is not None: + msg += f" -> instance: {plugin_instance}" + msg += "
" + msg += f"Duration: {duration} ms
" + self.text.setHtml(msg) + + data = { + "context": context.data + } + for instance in context: + data[instance.name] = instance.data + self.model.update(data) + + app = QtWidgets.QApplication.instance() + while self._pause: + # Allow user interaction with the UI + app.processEvents() From 3de28f870d54415fc0d14e454f770772cf11d554 Mon Sep 17 00:00:00 2001 From: MustafaJafar Date: Mon, 24 Jun 2024 23:12:23 +0300 Subject: [PATCH 2/7] Add DebugExprimentalTool to ExperimentalTools --- .../tools/experimental_tools/tools_def.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/client/ayon_core/tools/experimental_tools/tools_def.py b/client/ayon_core/tools/experimental_tools/tools_def.py index 7def3551de..e3acfa72ab 100644 --- a/client/ayon_core/tools/experimental_tools/tools_def.py +++ b/client/ayon_core/tools/experimental_tools/tools_def.py @@ -1,4 +1,5 @@ import os +from .pyblish_debug_stepper import DebugUI # Constant key under which local settings are stored LOCAL_EXPERIMENTAL_KEY = "experimental_tools" @@ -95,6 +96,12 @@ def __init__(self, parent_widget=None, refresh=True): "hiero", "resolve", ] + ), + ExperimentalHostTool( + "DebugExprimentalTool", + "Pyblish Debug Stepper", + "Debug Pyblish plugins step by step.", + self._show_pyblish_debugger, ) ] @@ -162,6 +169,13 @@ def refresh_availability(self): local_settings.get(LOCAL_EXPERIMENTAL_KEY) ) or {} + # Enable the following tools by default. + # Because they will always be disabled due + # to the fact their settings don't exist. + experimental_settings.update({ + "DebugExprimentalTool": True, + }) + for identifier, eperimental_tool in self.tools_by_identifier.items(): enabled = experimental_settings.get(identifier, False) eperimental_tool.set_enabled(enabled) @@ -175,3 +189,7 @@ def _show_publisher(self): ) self._publisher_tool.show() + + def _show_pyblish_debugger(self): + window = DebugUI(parent=self._parent_widget) + window.show() From 36caeba8dbd5ed3da3f736bebed52330f90b12a9 Mon Sep 17 00:00:00 2001 From: MustafaJafar Date: Mon, 24 Jun 2024 23:26:34 +0300 Subject: [PATCH 3/7] Update pyblish debug stepper docstring --- .../pyblish_debug_stepper.py | 38 +++++++------------ 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py b/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py index e6cf3c69d6..51b7ba2c06 100644 --- a/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py +++ b/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py @@ -3,37 +3,25 @@ Code Credits: [BigRoy](https://github.com/BigRoy) Requirement: - This tool requires some modification in ayon-core. - Add the following two lines in sa similar fashion to this commit - https://github.com/ynput/OpenPype/commit/6a0ce21aa1f8cb17452fe066aa15134d22fda440 - i.e. Add them just after - https://github.com/ynput/ayon-core/blob/8366d2e8b4003a252b8da822f7e38c6db08292b4/client/ayon_core/tools/publisher/control.py#L2483-L2487 - - ``` - result["context"] = self._publish_context - pyblish.api.emit("pluginProcessedCustom", result=result) - ``` - - This modification should be temporary till the following PR get merged and released. - https://github.com/pyblish/pyblish-base/pull/401 + It requires pyblish version >= 1.8.12 How it works: - It registers a callback function `on_plugin_processed` - when event `pluginProcessedCustom` is emitted. - The logic of this function is roughly: - 1. Pauses the publishing. - 2. Collects some info about the plugin. - 3. Shows that info to the tool's window. - 4. Continues publishing on clicking `step` button. + This tool makes use of pyblish event `pluginProcessed` to: + 1. Pause the publishing. + 2. Collect some info about the plugin. + 3. Show that info to the tool's window. + 4. Continue publishing on clicking `step` button. How to use it: 1. Launch the tool from AYON experimental tools window. 2. Launch the publisher tool and click validate. 3. Click Step to run plugins one by one. -Note: - It won't work when triggering validation from code as our custom event lives inside ayon-core. - But, It should work when the mentioned PR above (#401) get merged and released. +Note : + Pyblish debugger also works when triggering the validation or + publishing from code. + Here's an example about validating from code: + https://github.com/MustafaJafar/ayon-recipes/blob/main/validate_from_code.py """ @@ -225,13 +213,13 @@ def on_step(self): def showEvent(self, event): print("Registering callback..") - pyblish.api.register_callback("pluginProcessedCustom", # "pluginProcessed" + pyblish.api.register_callback("pluginProcessed", self.on_plugin_processed) def hideEvent(self, event): self.pause(False) print("Deregistering callback..") - pyblish.api.deregister_callback("pluginProcessedCustom", # "pluginProcessed" + pyblish.api.deregister_callback("pluginProcessed", self.on_plugin_processed) def on_plugin_processed(self, result): From f5006273ae69f6cb6404522488ae3141ed376200 Mon Sep 17 00:00:00 2001 From: MustafaJafar Date: Fri, 5 Jul 2024 19:33:23 +0300 Subject: [PATCH 4/7] fix typo --- client/ayon_core/tools/experimental_tools/tools_def.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/ayon_core/tools/experimental_tools/tools_def.py b/client/ayon_core/tools/experimental_tools/tools_def.py index e3acfa72ab..30e5211b41 100644 --- a/client/ayon_core/tools/experimental_tools/tools_def.py +++ b/client/ayon_core/tools/experimental_tools/tools_def.py @@ -98,7 +98,7 @@ def __init__(self, parent_widget=None, refresh=True): ] ), ExperimentalHostTool( - "DebugExprimentalTool", + "pyblish_debug_stepper", "Pyblish Debug Stepper", "Debug Pyblish plugins step by step.", self._show_pyblish_debugger, @@ -173,12 +173,12 @@ def refresh_availability(self): # Because they will always be disabled due # to the fact their settings don't exist. experimental_settings.update({ - "DebugExprimentalTool": True, + "pyblish_debug_stepper": True, }) - for identifier, eperimental_tool in self.tools_by_identifier.items(): + for identifier, experimental_tool in self.tools_by_identifier.items(): enabled = experimental_settings.get(identifier, False) - eperimental_tool.set_enabled(enabled) + experimental_tool.set_enabled(enabled) def _show_publisher(self): if self._publisher_tool is None: From b905dfe4bbc68e32cd76bb9fdea226c9c68e2a29 Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Tue, 24 Sep 2024 02:41:30 +0200 Subject: [PATCH 5/7] Support PySide6 --- .../tools/experimental_tools/pyblish_debug_stepper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py b/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py index 51b7ba2c06..1db0a3d9f4 100644 --- a/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py +++ b/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py @@ -165,9 +165,9 @@ def __init__(self, parent=None): text_edit = QtWidgets.QTextEdit() text_edit.setFixedHeight(65) font = QtGui.QFont("NONEXISTENTFONT") - font.setStyleHint(font.TypeWriter) + font.setStyleHint(QtGui.QFont.TypeWriter) text_edit.setFont(font) - text_edit.setLineWrapMode(text_edit.NoWrap) + text_edit.setLineWrapMode(QtWidgets.QTextEdit.NoWrap) step = QtWidgets.QPushButton("Step") step.setEnabled(False) From c9ea3dddd06dcdb15714f977095f49b513ea3f66 Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Tue, 24 Sep 2024 02:44:36 +0200 Subject: [PATCH 6/7] Cosmetics --- .../pyblish_debug_stepper.py | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py b/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py index 1db0a3d9f4..0f1120b8f6 100644 --- a/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py +++ b/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py @@ -82,8 +82,8 @@ def _update_recursive(self, data, parent, previous_data): index = self.index(row, 0, parent_index) if index.data() == key: item = self.itemFromIndex(index) - type_item = self.itemFromIndex(self.index(row, 1, parent_index)) - value_item = self.itemFromIndex(self.index(row, 2, parent_index)) + type_item = self.itemFromIndex(self.index(row, 1, parent_index)) # noqa + value_item = self.itemFromIndex(self.index(row, 2, parent_index)) # noqa break else: item = QtGui.QStandardItem(key) @@ -92,13 +92,16 @@ def _update_recursive(self, data, parent, previous_data): parent.appendRow([item, type_item, value_item]) # Key - key_color = NEW_KEY_COLOR if key not in previous_data else KEY_COLOR + key_color = NEW_KEY_COLOR if key not in previous_data else KEY_COLOR # noqa item.setData(key_color, QtCore.Qt.ForegroundRole) # Type type_str = type(value).__name__ type_color = VALUE_TYPE_COLOR - if key in previous_data and type(previous_data[key]).__name__ != type_str: + if ( + key in previous_data + and type(previous_data[key]).__name__ != type_str + ): type_color = NEW_VALUE_TYPE_COLOR type_item.setText(type_str) @@ -116,23 +119,31 @@ def _update_recursive(self, data, parent, previous_data): if len(value_str) > MAX_VALUE_STR_LEN: value_str = value_str[:MAX_VALUE_STR_LEN] + "..." value_item.setText(value_str) - # Preferably this is deferred to only when the data gets requested - # since this formatting can be slow for very large data sets like - # project settings and system settings - # This will also be MUCH MUCH faster if we don't clear the items on each update - # but only updated/add/remove changed items so that this also runs much less often - value_item.setData(json.dumps(value, default=str, indent=4), QtCore.Qt.ToolTipRole) + # Preferably this is deferred to only when the data gets + # requested since this formatting can be slow for very large + # data sets like project settings and system settings + # This will also be MUCH faster if we don't clear the + # items on each update but only updated/add/remove changed + # items so that this also runs much less often + value_item.setData( + json.dumps(value, default=str, indent=4), + QtCore.Qt.ToolTipRole + ) if isinstance(value, dict): previous_value = previous_data.get(key, {}) if previous_data.get(key) != value: # Update children if the value is not the same as before - self._update_recursive(value, parent=item, previous_data=previous_value) + self._update_recursive(value, + parent=item, + previous_data=previous_value) else: - # TODO: Ensure all children are updated to be not marked as 'changed' - # in the most optimal way possible - self._update_recursive(value, parent=item, previous_data=previous_value) + # TODO: Ensure all children are updated to be not marked + # as 'changed' in the most optimal way possible + self._update_recursive(value, + parent=item, + previous_data=previous_value) self._data = data @@ -195,8 +206,6 @@ def __init__(self, parent=None): self._previous_data = {} - - def _set_window_title(self, plugin=None): title = "Pyblish Debug Stepper" if plugin is not None: @@ -227,7 +236,7 @@ def on_plugin_processed(self, result): self._set_window_title(plugin=result["plugin"]) - print(10*"<" ,result["plugin"].__name__, 10*">") + print(10*"<", result["plugin"].__name__, 10*">") plugin_order = result["plugin"].order plugin_name = result["plugin"].__name__ From b946ed64f33f3c1e19c351b44f8c1d55cf395738 Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Tue, 24 Sep 2024 02:51:49 +0200 Subject: [PATCH 7/7] Add simple filter field to quickly filter to certain keys only --- .../tools/experimental_tools/pyblish_debug_stepper.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py b/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py index 0f1120b8f6..33de4bf036 100644 --- a/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py +++ b/client/ayon_core/tools/experimental_tools/pyblish_debug_stepper.py @@ -185,12 +185,18 @@ def __init__(self, parent=None): model = DictChangesModel() proxy = QtCore.QSortFilterProxyModel() + proxy.setRecursiveFilteringEnabled(True) proxy.setSourceModel(model) view = QtWidgets.QTreeView() view.setModel(proxy) view.setSortingEnabled(True) + filter_field = QtWidgets.QLineEdit() + filter_field.setPlaceholderText("Filter keys...") + filter_field.textChanged.connect(proxy.setFilterFixedString) + layout.addWidget(text_edit) + layout.addWidget(filter_field) layout.addWidget(view) layout.addWidget(step) @@ -198,6 +204,7 @@ def __init__(self, parent=None): self._pause = False self.model = model + self.filter = filter_field self.proxy = proxy self.view = view self.text = text_edit