From 4e023d61293c6333bbdb5bd3a023ab32285212fe Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sat, 7 Dec 2019 15:03:59 +0100 Subject: [PATCH] Simplify 3d axes callback setup. Always setup the 3d mouse rotate/zoom callbacks and don't store their cids; instead, handle their disconnection by disabling all mouse buttons (which has the same observable effect). This is in preparation of restoring the pan/zoom callbacks upon unpickling: after a pickle/unpickle cycle, even if we restore the callbacks, we can't guarantee that the callback numeric ids will stay the same, and so self._cids would become incorrect. --- lib/mpl_toolkits/mplot3d/axes3d.py | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/lib/mpl_toolkits/mplot3d/axes3d.py b/lib/mpl_toolkits/mplot3d/axes3d.py index 14a94a744cce..ea901c671192 100644 --- a/lib/mpl_toolkits/mplot3d/axes3d.py +++ b/lib/mpl_toolkits/mplot3d/axes3d.py @@ -79,7 +79,6 @@ def __init__( if rect is None: rect = [0.0, 0.0, 1.0, 1.0] - self._cids = [] self.initial_azim = azim self.initial_elev = elev @@ -115,6 +114,12 @@ def __init__( self._zcid = None self.mouse_init() + self.figure.canvas.mpl_connect( + 'motion_notify_event', self._on_move), + self.figure.canvas.mpl_connect( + 'button_press_event', self._button_press), + self.figure.canvas.mpl_connect( + 'button_release_event', self._button_release), self.set_top_view() self.patch.set_linewidth(0) @@ -1004,8 +1009,7 @@ def get_proj(self): def mouse_init(self, rotate_btn=1, zoom_btn=3): """ - Initializes mouse button callbacks to enable 3D rotation of the axes. - Also optionally sets the mouse buttons for 3D rotation and zooming. + Set the mouse buttons for 3D rotation and zooming. Parameters ---------- @@ -1015,20 +1019,16 @@ def mouse_init(self, rotate_btn=1, zoom_btn=3): The mouse button or buttons to use to zoom the 3D axes. """ self.button_pressed = None - self._cids = [ - self.figure.canvas.mpl_connect( - 'motion_notify_event', self._on_move), - self.figure.canvas.mpl_connect( - 'button_press_event', self._button_press), - self.figure.canvas.mpl_connect( - 'button_release_event', self._button_release), - ] # coerce scalars into array-like, then convert into # a regular list to avoid comparisons against None # which breaks in recent versions of numpy. self._rotate_btn = np.atleast_1d(rotate_btn).tolist() self._zoom_btn = np.atleast_1d(zoom_btn).tolist() + def disable_mouse_rotation(self): + """Disable mouse buttons for 3D rotation and zooming.""" + self.mouse_init(rotate_btn=[], zoom_btn=[]) + def can_zoom(self): """ Return *True* if this axes supports the zoom box button functionality. @@ -1069,13 +1069,6 @@ def cla(self): self.grid(rcParams['axes3d.grid']) - def disable_mouse_rotation(self): - """Disable mouse button callbacks.""" - # Disconnect the various events we set. - for cid in self._cids: - self.figure.canvas.mpl_disconnect(cid) - self._cids = [] - def _button_press(self, event): if event.inaxes == self: self.button_pressed = event.button