|
9 | 9 | import matplotlib.pyplot as plt |
10 | 10 | from compas.geometry import allclose |
11 | 11 |
|
| 12 | +from compas_plotters.scene.plotterobject import PlotterSceneObject |
| 13 | +from compas_plotters.scene.plotterscene import PlotterScene |
| 14 | + |
12 | 15 | if TYPE_CHECKING: |
13 | 16 | from compas.data import Data |
14 | 17 | from matplotlib.axes import Axes |
15 | 18 | from matplotlib.figure import Figure |
16 | 19 |
|
17 | | - from compas_plotters.scene.plotterobject import PlotterSceneObject |
18 | | - |
19 | 20 | Viewbox = tuple[tuple[float, float], tuple[float, float]] |
20 | 21 |
|
21 | 22 |
|
@@ -70,7 +71,7 @@ def __init__( |
70 | 71 | self._show_axes = show_axes |
71 | 72 | self._viewbox: Viewbox | None = None |
72 | 73 | self._axes: Axes | None = None |
73 | | - self._sceneobjects: list[PlotterSceneObject] = [] |
| 74 | + self.scene = PlotterScene(plotter=self) |
74 | 75 | self.viewbox = view |
75 | 76 | self.figsize = figsize |
76 | 77 | self.dpi = dpi |
@@ -125,7 +126,12 @@ def figure(self) -> Figure: |
125 | 126 | @property |
126 | 127 | def sceneobjects(self) -> list[PlotterSceneObject]: |
127 | 128 | """The scene objects currently included in the plot.""" |
128 | | - return self._sceneobjects |
| 129 | + return [obj for obj in self.scene.objects if isinstance(obj, PlotterSceneObject)] |
| 130 | + |
| 131 | + @property |
| 132 | + def is_live(self) -> bool: |
| 133 | + """Whether the matplotlib figure has been created yet.""" |
| 134 | + return self._axes is not None |
129 | 135 |
|
130 | 136 | @property |
131 | 137 | def title(self) -> str: |
@@ -163,7 +169,7 @@ def zoom_extents(self, padding: float | None = None) -> None: |
163 | 169 | fig_aspect = width / height |
164 | 170 |
|
165 | 171 | data: list[list[float]] = [] |
166 | | - for obj in self._sceneobjects: |
| 172 | + for obj in self.sceneobjects: |
167 | 173 | data += obj.viewdata() |
168 | 174 | if not data: |
169 | 175 | return |
@@ -226,17 +232,7 @@ def add(self, item: Data, **kwargs) -> PlotterSceneObject: |
226 | 232 | ------- |
227 | 233 | The scene object created for the item. |
228 | 234 | """ |
229 | | - # Imported lazily to avoid a circular import at module load time. |
230 | | - from compas.scene import SceneObject |
231 | | - |
232 | | - if self.zstack == "natural": |
233 | | - zorder = 1000 + len(self._sceneobjects) * 100 |
234 | | - kwargs.setdefault("zorder", zorder) |
235 | | - |
236 | | - sceneobject: PlotterSceneObject = SceneObject(item=item, context="Plotter", plotter=self, **kwargs) # type: ignore[assignment] |
237 | | - sceneobject.draw() |
238 | | - self._sceneobjects.append(sceneobject) |
239 | | - return sceneobject |
| 235 | + return self.scene.add(item, **kwargs) # type: ignore[return-value] |
240 | 236 |
|
241 | 237 | def add_from_list(self, items: Iterable[Data], **kwargs) -> list[PlotterSceneObject]: |
242 | 238 | """Add multiple COMPAS objects, all with the same options. |
@@ -266,7 +262,7 @@ def find(self, item: Data) -> PlotterSceneObject | None: |
266 | 262 | ------- |
267 | 263 | The matching scene object, or None if the item is not in the plot. |
268 | 264 | """ |
269 | | - for obj in self._sceneobjects: |
| 265 | + for obj in self.sceneobjects: |
270 | 266 | if item is obj.item: |
271 | 267 | return obj |
272 | 268 | return None |
@@ -302,7 +298,7 @@ def redraw(self, pause: float | None = None) -> None: |
302 | 298 | pause |
303 | 299 | If provided, pause for this many seconds after redrawing. |
304 | 300 | """ |
305 | | - for obj in self._sceneobjects: |
| 301 | + for obj in self.sceneobjects: |
306 | 302 | obj.redraw() |
307 | 303 | self.figure.canvas.draw() |
308 | 304 | self.figure.canvas.flush_events() |
|
0 commit comments