Skip to content

Commit

Permalink
Trigger event to close all windows when opening a new Maya scene file.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
david-cattermole committed Jul 4, 2021
1 parent 3db9168 commit e435461
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/mmSolver/_api/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -235,6 +236,7 @@
EVENT_NAME_NODE_NAME_CHANGED,
EVENT_NAME_NODE_DELETED,
EVENT_NAME_MEMBERSHIP_CHANGED,
EVENT_NAME_MAYA_SCENE_CLOSING,
]


Expand Down
2 changes: 2 additions & 0 deletions python/mmSolver/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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',
Expand Down
14 changes: 14 additions & 0 deletions python/mmSolver/tools/registerevents/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 25 additions & 0 deletions python/mmSolver/tools/registerevents/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

0 comments on commit e435461

Please sign in to comment.