From cd8e8f9d3b655707132f9ba39b780fbd63c9b75f Mon Sep 17 00:00:00 2001 From: Alex Kaszynski Date: Fri, 15 Sep 2023 11:46:33 -0700 Subject: [PATCH] Make composite mapper dataset optional (#4879) * make composite mapper dataset optional * add test * Explicit is better than implicit. (The Zen of Python) --------- Co-authored-by: Tetsuo Koyama --- pyvista/plotting/composite_mapper.py | 14 +++++++++----- tests/plotting/test_actor.py | 13 +++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/pyvista/plotting/composite_mapper.py b/pyvista/plotting/composite_mapper.py index 38f38d816b..b7aec0d0a0 100644 --- a/pyvista/plotting/composite_mapper.py +++ b/pyvista/plotting/composite_mapper.py @@ -521,7 +521,7 @@ class CompositePolyDataMapper(_vtk.vtkCompositePolyDataMapper2, _BaseMapper): Parameters ---------- - dataset : pyvista.MultiBlock + dataset : pyvista.MultiBlock, optional Multiblock dataset. theme : pyvista.plotting.themes.Theme, optional @@ -541,16 +541,14 @@ class CompositePolyDataMapper(_vtk.vtkCompositePolyDataMapper2, _BaseMapper): """ def __init__( - self, dataset, theme=None, color_missing_with_nan=None, interpolate_before_map=None + self, dataset=None, theme=None, color_missing_with_nan=None, interpolate_before_map=None ): """Initialize this composite mapper.""" super().__init__(theme=theme) - self.SetInputDataObject(dataset) - # this must be added to set the color, opacity, and visibility of # individual blocks self._attr = CompositeAttributes(self, dataset) - self._dataset = dataset + self.dataset = dataset if color_missing_with_nan is not None: self.color_missing_with_nan = color_missing_with_nan @@ -579,6 +577,12 @@ def dataset(self) -> 'pv.MultiBlock': # numpydoc ignore=RT01 """ return self._dataset + @dataset.setter + def dataset(self, obj: 'pv.MultiBlock'): # numpydoc ignore=GL08 + self.SetInputDataObject(obj) + self._dataset = obj + self._attr._dataset = obj + @property def block_attr(self) -> CompositeAttributes: # numpydoc ignore=RT01 """Return the block attributes. diff --git a/tests/plotting/test_actor.py b/tests/plotting/test_actor.py index 4b70d951b6..6bd4dfc80e 100644 --- a/tests/plotting/test_actor.py +++ b/tests/plotting/test_actor.py @@ -17,6 +17,11 @@ def actor(): return pv.Plotter().add_mesh(pv.Plane()) +@pytest.fixture() +def actor_from_multi_block(): + return pv.Plotter().add_mesh(pv.MultiBlock([pv.Plane()])) + + @pytest.fixture() def vol_actor(): vol = pv.ImageData(dimensions=(10, 10, 10)) @@ -68,6 +73,14 @@ def test_actor_copy_shallow(actor): assert actor_copy.mapper is actor.mapper +def test_actor_mblock_copy_shallow(actor_from_multi_block): + actor_copy = actor_from_multi_block.copy(deep=False) + assert actor_copy is not actor_from_multi_block + assert actor_copy.prop is actor_from_multi_block.prop + assert actor_copy.mapper is actor_from_multi_block.mapper + assert actor_copy.mapper.dataset is actor_from_multi_block.mapper.dataset + + @skip_mac def test_actor_texture(actor): texture = examples.download_masonry_texture()