From e435461e9a993a08a18b83ed86cac882553c57fe Mon Sep 17 00:00:00 2001 From: David Cattermole Date: Sun, 4 Jul 2021 13:42:09 +0100 Subject: [PATCH] Trigger event to close all windows when opening a new Maya scene file. This stops crashes from happening caused when opening a new Maya scene while the window(s) (internally) still hold a pointer to some deallocated memory (from the previously open Maya scene). Closing all windows clears the memory and fixes the problem. Issue #222. --- python/mmSolver/_api/constant.py | 2 ++ python/mmSolver/api.py | 2 ++ python/mmSolver/tools/registerevents/lib.py | 14 +++++++++++ python/mmSolver/tools/registerevents/tool.py | 25 ++++++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/python/mmSolver/_api/constant.py b/python/mmSolver/_api/constant.py index 9d7c2ff6f..b45ae7f5a 100644 --- a/python/mmSolver/_api/constant.py +++ b/python/mmSolver/_api/constant.py @@ -224,6 +224,7 @@ EVENT_NAME_NODE_NAME_CHANGED = 'node_name_changed' EVENT_NAME_NODE_DELETED = 'node_deleted' EVENT_NAME_MEMBERSHIP_CHANGED = 'node_membership_changed' +EVENT_NAME_MAYA_SCENE_CLOSING = 'maya_scene_closing' EVENT_NAME_LIST = [ EVENT_NAME_MARKER_CREATED, EVENT_NAME_BUNDLE_CREATED, @@ -235,6 +236,7 @@ EVENT_NAME_NODE_NAME_CHANGED, EVENT_NAME_NODE_DELETED, EVENT_NAME_MEMBERSHIP_CHANGED, + EVENT_NAME_MAYA_SCENE_CLOSING, ] diff --git a/python/mmSolver/api.py b/python/mmSolver/api.py index 37911d4e2..5b8b4f03d 100644 --- a/python/mmSolver/api.py +++ b/python/mmSolver/api.py @@ -185,6 +185,7 @@ EVENT_NAME_NODE_NAME_CHANGED, EVENT_NAME_NODE_DELETED, EVENT_NAME_MEMBERSHIP_CHANGED, + EVENT_NAME_MAYA_SCENE_CLOSING, EVENT_NAME_LIST, ATTRIBUTE_USED_HINT_DEFAULT_VALUE, @@ -303,6 +304,7 @@ 'EVENT_NAME_NODE_NAME_CHANGED', 'EVENT_NAME_NODE_DELETED', 'EVENT_NAME_MEMBERSHIP_CHANGED', + 'EVENT_NAME_MAYA_SCENE_CLOSING', 'EVENT_NAME_LIST', 'ATTRIBUTE_USED_HINT_DEFAULT_VALUE', 'ATTRIBUTE_USED_HINT_USED_VALUE', diff --git a/python/mmSolver/tools/registerevents/lib.py b/python/mmSolver/tools/registerevents/lib.py index 790647ce9..b7f93764e 100644 --- a/python/mmSolver/tools/registerevents/lib.py +++ b/python/mmSolver/tools/registerevents/lib.py @@ -84,3 +84,17 @@ def run_update_output_attributes_in_solver_ui(**kwargs): e = time.time() LOG.debug("run_update_output_attributes_in_solver_ui: time=%s", e - s) return + + +def run_close_all_windows(**kwargs): + """ + Find and close all tool UIs sub-classed from BaseMayaWindow. + """ + LOG.debug("run_close_all_windows: %r", kwargs) + s = time.time() + import mmSolver.ui.base_maya_window + cls = mmSolver.ui.base_maya_window.BaseMayaWindow + cls.close_all_instances() + e = time.time() + LOG.debug("run_close_all_windows: time=%s", e - s) + return diff --git a/python/mmSolver/tools/registerevents/tool.py b/python/mmSolver/tools/registerevents/tool.py index e4a3ce48a..d18c94590 100644 --- a/python/mmSolver/tools/registerevents/tool.py +++ b/python/mmSolver/tools/registerevents/tool.py @@ -79,6 +79,19 @@ def _register_changed_attribute_update_solver_ui(): return +def _register_closing_maya_scene(): + """When the current Maya scene is closing, close all windows, because + the windows might have pointers/references to objects in the scene + file and we cannot allow the pointers to dangle. + """ + import mmSolver.api as mmapi + event_utils.add_function_to_event( + mmapi.EVENT_NAME_MAYA_SCENE_CLOSING, + lib.run_close_all_windows, + deferred=False) + return + + def register_events(): """ Initialises the registry of events for mmSolver. @@ -90,4 +103,16 @@ def register_events(): _register_created_marker_connect_to_collection() _register_changed_collection_update_solver_ui() _register_changed_attribute_update_solver_ui() + _register_closing_maya_scene() + + # Maya callback when Maya scene is "flushing" from memory ( AKA + # the scene is closing). + def flushing_scene_func(): + LOG.debug('MM Solver Flushing Scene...') + event_name = mmapi.EVENT_NAME_MAYA_SCENE_CLOSING + event_utils.trigger_event(event_name) + + import maya.cmds + maya.cmds.scriptJob( + conditionTrue=('flushingScene', flushing_scene_func)) return