Skip to content

Commit 8b6497d

Browse files
committed
Merge remote-tracking branch 'origin/main' into scene
2 parents 2d771d6 + 8fb5ecb commit 8b6497d

File tree

6 files changed

+56
-4
lines changed

6 files changed

+56
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
* Added `Group` to `compas.scene`.
13+
1214
### Changed
1315

1416
* Changed `SceneObject.worldtransformation` to the multiplication of all transformations from the scene object to the root of the scene tree.

src/compas/scene/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .graphobject import GraphObject
1515
from .geometryobject import GeometryObject
1616
from .volmeshobject import VolMeshObject
17+
from .group import Group
1718

1819
from .context import clear
1920
from .context import before_draw
@@ -53,4 +54,5 @@ def register_scene_objects_base():
5354
"register_scene_objects",
5455
"get_sceneobject_cls",
5556
"register",
57+
"Group",
5658
]

src/compas/scene/group.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from .sceneobject import SceneObject
2+
3+
4+
class Group(SceneObject):
5+
"""A group of scene objects.
6+
7+
Parameters
8+
----------
9+
name : str, optional
10+
The name of the group.
11+
12+
Examples
13+
--------
14+
>>> from compas.scene import Scene
15+
>>> from compas.scene import Group
16+
>>> from compas.geometry import Point
17+
>>> scene = Scene()
18+
>>> group = Group(name="My Group")
19+
>>> scene.add(group)
20+
>>> point = Point(0, 0, 0)
21+
>>> group.add(point)
22+
>>> print(scene)
23+
<Tree with 3 nodes>
24+
└── <TreeNode: ROOT>
25+
└── <Group: My Group>
26+
└── <GeometryObject: Point>
27+
28+
"""
29+
30+
def __new__(cls, *args, **kwargs):
31+
# overwriting __new__ to revert to the default behavior of normal object, So an instance can be created directly without providing a registered item.
32+
return object.__new__(cls)
33+
34+
@property
35+
def __data__(self):
36+
# type: () -> dict
37+
data = {
38+
"settings": self.settings,
39+
"children": [child.__data__ for child in self.children],
40+
}
41+
return data

src/compas/scene/scene.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from .context import before_draw
99
from .context import clear
1010
from .context import detect_current_context
11+
from .group import Group
1112
from .sceneobject import SceneObject
1213

1314

@@ -42,7 +43,7 @@ class Scene(Tree):
4243
@property
4344
def __data__(self):
4445
# type: () -> dict
45-
items = {str(object.item.guid): object.item for object in self.objects}
46+
items = {str(object.item.guid): object.item for object in self.objects if object.item is not None}
4647
return {
4748
"name": self.name,
4849
"root": self.root.__data__, # type: ignore
@@ -57,9 +58,12 @@ def __from_data__(cls, data):
5758

5859
def add(node, parent, items):
5960
for child_node in node.get("children", []):
60-
guid = child_node["item"]
6161
settings = child_node["settings"]
62-
sceneobject = parent.add(items[guid], **settings)
62+
if "item" in child_node:
63+
guid = child_node["item"]
64+
sceneobject = parent.add(items[guid], **settings)
65+
else:
66+
sceneobject = parent.add(Group(**settings))
6367
add(child_node, sceneobject, items)
6468

6569
add(data["root"], scene, items)

src/compas/scene/sceneobject.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def __init__(
100100
**kwargs # type: dict
101101
): # fmt: skip
102102
# type: (...) -> None
103-
if not isinstance(item, Data):
103+
if item and not isinstance(item, Data):
104104
raise ValueError("The item assigned to this scene object should be a data object: {}".format(type(item)))
105105

106106
name = name or item.name

tests/compas/scene/test_scene_serialisation.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from compas.datastructures import Mesh
2424
from compas.datastructures import Graph
2525
from compas.datastructures import VolMesh
26+
from compas.scene import Group
2627

2728
@pytest.fixture
2829
def items():
@@ -45,6 +46,7 @@ def items():
4546
mesh = Mesh.from_polyhedron(8)
4647
graph = Graph.from_nodes_and_edges([(0, 0, 0), (0, -1.5, 0), (-1, 1, 0), (1, 1, 0)], [(0, 1), (0, 2), (0, 3)])
4748
volmesh = VolMesh.from_meshgrid(1, 1, 1, 2, 2, 2)
49+
group = Group(name="My Group")
4850

4951
return [
5052
box,
@@ -66,6 +68,7 @@ def items():
6668
mesh,
6769
graph,
6870
volmesh,
71+
group,
6972
]
7073

7174
def assert_is_data_equal(obj1, obj2, path=""):

0 commit comments

Comments
 (0)