From c8d5174d73aceb48f05336a7ee54f94802ec067c Mon Sep 17 00:00:00 2001 From: Li Date: Tue, 14 May 2024 16:53:50 +0200 Subject: [PATCH] fix collection --- CHANGELOG.md | 1 + scripts/collection.py | 24 +++++++ .../components/renderer/renderer.py | 6 +- src/compas_viewer/scene/__init__.py | 5 ++ src/compas_viewer/scene/collectionobject.py | 64 ++++++------------- src/compas_viewer/scene/sceneobject.py | 1 - 6 files changed, 52 insertions(+), 49 deletions(-) create mode 100644 scripts/collection.py diff --git a/CHANGELOG.md b/CHANGELOG.md index aa6718bf5e..ce611acc8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Changed `NurbsSurfaceObject` to use tessellation function of `OCCBrep`, show boundary curves instead of control curves. * Renamed all lazy setup functions to `lazy_init`. * Fixed camera initialization issue. +* Fixed and brought back `CollectionObject`. ### Removed diff --git a/scripts/collection.py b/scripts/collection.py new file mode 100644 index 0000000000..ffd16e4d10 --- /dev/null +++ b/scripts/collection.py @@ -0,0 +1,24 @@ +from compas.geometry import Box +from compas.geometry import Line +from compas.geometry import Frame +from compas_viewer.viewer import Viewer +from compas_viewer.scene import Collection +from random import random + +viewer = Viewer() + +boxes = [] +for i in range(20): + for j in range(20): + box = Box(0.5, 0.5, 0.5, Frame([i, j, 0], [1, 0, 0], [0, 1, 0])) + boxes.append(box) + +viewer.scene.add(Collection(boxes), name="Boxes") + +lines = [] +for i in range(1000): + lines.append(Line([random() * 20, random() * 20, random() * 20], [random() * 20, random() * 20, random() * 20])) + +viewer.scene.add(Collection(lines), name="Lines") + +viewer.show() diff --git a/src/compas_viewer/components/renderer/renderer.py b/src/compas_viewer/components/renderer/renderer.py index 0bad7583f7..1481514118 100644 --- a/src/compas_viewer/components/renderer/renderer.py +++ b/src/compas_viewer/components/renderer/renderer.py @@ -16,7 +16,6 @@ from compas_viewer.base import Base from compas_viewer.configurations import RendererConfig from compas_viewer.scene import TagObject -from compas_viewer.scene.collectionobject import CollectionObject from compas_viewer.scene.vectorobject import VectorObject from .camera import Camera @@ -576,10 +575,7 @@ def sort(obj): mesh_objs.append(obj) for obj in objs: - if isinstance(obj, CollectionObject): - [sort(item) for item in obj.objects] - else: - sort(obj) + sort(obj) return tag_objs, vector_objs, mesh_objs diff --git a/src/compas_viewer/scene/__init__.py b/src/compas_viewer/scene/__init__.py index 7f30e1d00f..b54eb3ecb3 100644 --- a/src/compas_viewer/scene/__init__.py +++ b/src/compas_viewer/scene/__init__.py @@ -49,6 +49,8 @@ from .geometryobject import GeometryObject from .groupobject import Group from .groupobject import GroupObject +from .collectionobject import Collection +from .collectionobject import CollectionObject @plugin(category="drawing-utils", requires=["compas_viewer"]) @@ -84,6 +86,7 @@ def register_scene_objects(): register(Cone, ConeObject, context="Viewer") register(Capsule, CapsuleObject, context="Viewer") register(list, GroupObject, context="Viewer") + register(Collection, CollectionObject, context="Viewer") try: from compas_occ.brep import OCCBrep @@ -138,4 +141,6 @@ def register_scene_objects(): "GeometryObject", "Group", "GroupObject", + "Collection", + "CollectionObject", ] diff --git a/src/compas_viewer/scene/collectionobject.py b/src/compas_viewer/scene/collectionobject.py index 74d5d7dc1f..2ff89cfed2 100644 --- a/src/compas_viewer/scene/collectionobject.py +++ b/src/compas_viewer/scene/collectionobject.py @@ -15,22 +15,22 @@ class Collection(Data): """Viewer scene object for displaying a collection of COMPAS geometries.""" - def __init__(self, items: list[Union[Geometry, Mesh]], **kwargs): + def __init__(self, items: list[Union[Geometry, Mesh]] = None, **kwargs): super().__init__(**kwargs) self.items = items @property def __data__(self): - return self.items + return {"items": self.items} class CollectionObject(ViewerSceneObject, GeometryObject): """Viewer scene object for displaying a collection of COMPAS geometries.""" - def __init__(self, items: list[Union[Geometry, Mesh]], **kwargs): - self.collection = Collection(items) + def __init__(self, collection: Collection, **kwargs): + self.collection = collection super().__init__(geometry=self.collection, **kwargs) - self.objects = [ViewerSceneObject(item=item, **kwargs) for item in self.collection.items] + self.objects = [ViewerSceneObject(item, **kwargs) for item in self.collection.items] def _read_points_data(self) -> ShaderDataType: positions = [] @@ -43,6 +43,7 @@ def _read_points_data(self) -> ShaderDataType: colors += c elements += (array(e) + count).tolist() count += len(p) + return positions, colors, elements def _read_lines_data(self) -> ShaderDataType: @@ -56,56 +57,33 @@ def _read_lines_data(self) -> ShaderDataType: colors += c elements += (array(e) + count).tolist() count += len(p) + return positions, colors, elements def _read_frontfaces_data(self) -> ShaderDataType: positions = [] colors = [] - opacities = [] elements = [] count = 0 for obj in self.objects: - if obj.use_rgba: - p, c, o, e = obj._read_frontfaces_data() or ([], [], []) - positions += p - colors += c - opacities += o - elements += (np.array(e) + count).tolist() - count += len(p) - else: - p, c, e = obj._read_frontfaces_data() or ([], [], []) - positions += p - colors += c - elements += (np.array(e) + count).tolist() - count += len(p) - - if opacities: - return positions, colors, opacities, elements - else: - return positions, colors, elements + p, c, e = obj._read_frontfaces_data() or ([], [], []) + positions += p + colors += c + elements += (np.array(e) + count).tolist() + count += len(p) + + return positions, colors, elements def _read_backfaces_data(self) -> ShaderDataType: positions = [] colors = [] - opacities = [] elements = [] count = 0 for obj in self.objects: - if obj.use_rgba: - p, c, o, e = obj._read_backfaces_data() or ([], [], []) - positions += p - colors += c - opacities += o - elements += (np.array(e) + count).tolist() - count += len(p) - else: - p, c, e = obj._read_backfaces_data() or ([], [], []) - positions += p - colors += c - elements += (np.array(e) + count).tolist() - count += len(p) - - if opacities: - return positions, colors, opacities, elements - else: - return positions, colors, elements + p, c, e = obj._read_backfaces_data() or ([], [], []) + positions += p + colors += c + elements += (np.array(e) + count).tolist() + count += len(p) + + return positions, colors, elements diff --git a/src/compas_viewer/scene/sceneobject.py b/src/compas_viewer/scene/sceneobject.py index d402120d67..fe8679ed4a 100644 --- a/src/compas_viewer/scene/sceneobject.py +++ b/src/compas_viewer/scene/sceneobject.py @@ -126,7 +126,6 @@ def __init__( self._matrix_buffer: Optional[list[list[float]]] = None self._bounding_box: Optional[list[float]] = None self._bounding_box_center: Optional[Point] = None - self._is_collection = False # Primitive self._points_data: Optional[ShaderDataType] = None