Closing mayavi figure

Dear support,

I am facing issues closing mayavi figures opened with the mne command mne.viz.plot_alignment. My system specs are those:

  • MNE-Python version: 0.23.0
  • operating system: linux/windows

I am working with ecog data and need to open a lot of different mayavi 3D plots, as many as I have participants, all within the same script. And because I haven’t figured out how to close them within a script, so many plots remain open and my computers end up freezing.

I am sure there must exist an easy command to close mayavi 3D figures, but none of the ones I could find in their documentation seems to work. Has anyone figured out a way to do so and if yes how?

Kind regards,

Alex

PS: I haven’t included any code, ass it is a very simple issue: after calling the plot_alignment function, how do I close the generated figure. If you would like some code, I am happy to provide some.

Hello @AlexLepauvre,

in mne.Report, we are basically doing the following (I hope I got this right, I made some untested modifications…):

from mne.viz import create_3d_figure, plot_alignment
from mne.viz.backends.renderer import backend

fig = create_3d_figure((800, 800), bgcolor=(0.5, 0.5, 0.5))

try:
    try:
        plot_alignment(fig=fig, surfaces=['head-dense'], ...)
    except IOError:
        plot_alignment(fig=fig, surfaces=['head'], ...)
finally:
    backend._close_3d_figure(fig)

I think the key is the backend._close_3d_figure(fig) call.

also cc’ing @GuillaumeFavelier here, our 3D viz expert!

Best wishes,
Richard

1 Like

Exactly @richard ! Either _close_3d_figure(fig) or _close_all() should do the job :+1:

1 Like

Since we now have a user request, do you think it would make sense to make these functions public?

Thanks a lot everyone, this is immensely helpful :slight_smile:

1 Like

If this is useful, I don’t see why it can’t be exposed. The implementation of _close_all() and _close_3d_figure() did not change in a while.

set_3d_title() and set_3d_view() were exposed in the same way.

Thanks a lot everyone, this is immensely helpful

Glad that it works for you!

1 Like

I opened a feature request to make those close_ functions public