Skip to content

Commit e435461

Browse files
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.
1 parent 3db9168 commit e435461

4 files changed

Lines changed: 43 additions & 0 deletions

File tree

python/mmSolver/_api/constant.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@
224224
EVENT_NAME_NODE_NAME_CHANGED = 'node_name_changed'
225225
EVENT_NAME_NODE_DELETED = 'node_deleted'
226226
EVENT_NAME_MEMBERSHIP_CHANGED = 'node_membership_changed'
227+
EVENT_NAME_MAYA_SCENE_CLOSING = 'maya_scene_closing'
227228
EVENT_NAME_LIST = [
228229
EVENT_NAME_MARKER_CREATED,
229230
EVENT_NAME_BUNDLE_CREATED,
@@ -235,6 +236,7 @@
235236
EVENT_NAME_NODE_NAME_CHANGED,
236237
EVENT_NAME_NODE_DELETED,
237238
EVENT_NAME_MEMBERSHIP_CHANGED,
239+
EVENT_NAME_MAYA_SCENE_CLOSING,
238240
]
239241

240242

python/mmSolver/api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@
185185
EVENT_NAME_NODE_NAME_CHANGED,
186186
EVENT_NAME_NODE_DELETED,
187187
EVENT_NAME_MEMBERSHIP_CHANGED,
188+
EVENT_NAME_MAYA_SCENE_CLOSING,
188189
EVENT_NAME_LIST,
189190

190191
ATTRIBUTE_USED_HINT_DEFAULT_VALUE,
@@ -303,6 +304,7 @@
303304
'EVENT_NAME_NODE_NAME_CHANGED',
304305
'EVENT_NAME_NODE_DELETED',
305306
'EVENT_NAME_MEMBERSHIP_CHANGED',
307+
'EVENT_NAME_MAYA_SCENE_CLOSING',
306308
'EVENT_NAME_LIST',
307309
'ATTRIBUTE_USED_HINT_DEFAULT_VALUE',
308310
'ATTRIBUTE_USED_HINT_USED_VALUE',

python/mmSolver/tools/registerevents/lib.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,17 @@ def run_update_output_attributes_in_solver_ui(**kwargs):
8484
e = time.time()
8585
LOG.debug("run_update_output_attributes_in_solver_ui: time=%s", e - s)
8686
return
87+
88+
89+
def run_close_all_windows(**kwargs):
90+
"""
91+
Find and close all tool UIs sub-classed from BaseMayaWindow.
92+
"""
93+
LOG.debug("run_close_all_windows: %r", kwargs)
94+
s = time.time()
95+
import mmSolver.ui.base_maya_window
96+
cls = mmSolver.ui.base_maya_window.BaseMayaWindow
97+
cls.close_all_instances()
98+
e = time.time()
99+
LOG.debug("run_close_all_windows: time=%s", e - s)
100+
return

python/mmSolver/tools/registerevents/tool.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ def _register_changed_attribute_update_solver_ui():
7979
return
8080

8181

82+
def _register_closing_maya_scene():
83+
"""When the current Maya scene is closing, close all windows, because
84+
the windows might have pointers/references to objects in the scene
85+
file and we cannot allow the pointers to dangle.
86+
"""
87+
import mmSolver.api as mmapi
88+
event_utils.add_function_to_event(
89+
mmapi.EVENT_NAME_MAYA_SCENE_CLOSING,
90+
lib.run_close_all_windows,
91+
deferred=False)
92+
return
93+
94+
8295
def register_events():
8396
"""
8497
Initialises the registry of events for mmSolver.
@@ -90,4 +103,16 @@ def register_events():
90103
_register_created_marker_connect_to_collection()
91104
_register_changed_collection_update_solver_ui()
92105
_register_changed_attribute_update_solver_ui()
106+
_register_closing_maya_scene()
107+
108+
# Maya callback when Maya scene is "flushing" from memory ( AKA
109+
# the scene is closing).
110+
def flushing_scene_func():
111+
LOG.debug('MM Solver Flushing Scene...')
112+
event_name = mmapi.EVENT_NAME_MAYA_SCENE_CLOSING
113+
event_utils.trigger_event(event_name)
114+
115+
import maya.cmds
116+
maya.cmds.scriptJob(
117+
conditionTrue=('flushingScene', flushing_scene_func))
93118
return

0 commit comments

Comments
 (0)