Skip to content

Commit 0c8f79e

Browse files
authored
Merge pull request #1223 from compas-dev/scene-object
Track guids in drawn objects
2 parents af2a231 + 8e83a7d commit 0c8f79e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+555
-272
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
15751575
### Changed
15761576

15771577
* Fixed bug in `compas.geometry.Box.vertices`.
1578+
* `compas.scene.SceneObject` will now track a list of drawn Objects/GUIDs.
15781579

15791580
### Removed
15801581

src/compas/scene/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
@pluggable(category="drawing-utils")
13-
def clear():
13+
def clear(guids=None):
1414
raise NotImplementedError
1515

1616

src/compas/scene/scene.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,18 @@ def _get_node(self, sceneobject):
4747
raise Exception("Scene object not in scene")
4848

4949
def clear(self):
50-
clear()
50+
guids = []
51+
for sceneobject in self.sceneobjects:
52+
guids += sceneobject.guids
53+
sceneobject._guids = None
54+
clear(guids=guids)
5155

5256
def redraw(self):
57+
self.clear()
58+
5359
drawn_objects = []
5460
for sceneobject in self.sceneobjects:
55-
drawn_object = sceneobject.draw()
56-
57-
# TODO: unify output of draw(), so we don't have to do this
58-
if isinstance(drawn_object, (list, tuple)):
59-
for item in drawn_object:
60-
if isinstance(item, (list, tuple)):
61-
drawn_objects.extend(item)
62-
else:
63-
drawn_objects.append(item)
64-
else:
65-
drawn_objects.append(drawn_object)
61+
drawn_objects += sceneobject.draw()
6662

6763
if drawn_objects:
6864
redraw()

src/compas/scene/sceneobject.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from abc import abstractmethod
66
from .descriptors.protocol import DescriptorProtocol
7+
from .context import clear
78

89

910
class SceneObject(object):
@@ -13,14 +14,12 @@ class SceneObject(object):
1314
----------
1415
item : Any
1516
The item which should be visualized using the created SceneObject.
16-
context : str, optional
17-
Explicit context to pick the SceneObject from.
18-
If not specified, an attempt will be made to automatically detect the appropriate context.
17+
1918
2019
Attributes
2120
----------
22-
ITEM_SCENEOBJECT : dict[str, dict[Type[:class:`~compas.data.Data`], Type[:class:`~compas.scene.SceneObject`]]]
23-
Dictionary mapping data types to the corresponding scene objects types per visualization context.
21+
guids : list[object]
22+
The GUIDs of the items drawn in the visualization context.
2423
2524
"""
2625

@@ -30,6 +29,11 @@ class SceneObject(object):
3029
def __init__(self, item, **kwargs):
3130
self._item = item
3231
self._transformation = None
32+
self._guids = None
33+
34+
@property
35+
def guids(self):
36+
return self._guids or []
3337

3438
@property
3539
def transformation(self):
@@ -52,7 +56,7 @@ def draw(self):
5256
"""The main drawing method."""
5357
raise NotImplementedError
5458

55-
@staticmethod
56-
def draw_collection(collection):
57-
"""Drawing method for drawing an entire collection of objects."""
58-
raise NotImplementedError
59+
def clear(self):
60+
"""The main clearing method."""
61+
clear(guids=self.guids)
62+
self._guids = None

src/compas_blender/__init__.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,28 @@
1010
from .utilities import * # noqa: F401 F403
1111

1212

13-
def clear():
13+
def clear(guids=None):
1414
"""Clear all scene objects."""
15-
# delete all objects
16-
bpy.ops.object.select_all(action="SELECT")
17-
bpy.ops.object.delete(use_global=True, confirm=False)
18-
# delete data
19-
delete_unused_data() # noqa: F405
20-
# delete collections
21-
for collection in bpy.context.scene.collection.children:
22-
bpy.context.scene.collection.children.unlink(collection)
23-
for block in bpy.data.collections:
24-
objects = [o for o in block.objects if o.users]
25-
while objects:
26-
bpy.data.objects.remove(objects.pop())
27-
for collection in block.children:
28-
block.children.unlink(collection)
29-
if block.users == 0:
30-
bpy.data.collections.remove(block)
15+
if guids is None:
16+
# delete all objects
17+
bpy.ops.object.select_all(action="SELECT")
18+
bpy.ops.object.delete(use_global=True, confirm=False)
19+
# delete data
20+
delete_unused_data() # noqa: F405
21+
# delete collections
22+
for collection in bpy.context.scene.collection.children:
23+
bpy.context.scene.collection.children.unlink(collection)
24+
for block in bpy.data.collections:
25+
objects = [o for o in block.objects if o.users]
26+
while objects:
27+
bpy.data.objects.remove(objects.pop())
28+
for collection in block.children:
29+
block.children.unlink(collection)
30+
if block.users == 0:
31+
bpy.data.collections.remove(block)
32+
else:
33+
for obj in guids:
34+
bpy.data.objects.remove(obj, do_unlink=True)
3135

3236

3337
def redraw():

src/compas_blender/scene/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from compas.geometry import Cylinder
1717
from compas.geometry import Frame
1818
from compas.geometry import Line
19+
from compas.geometry import Plane
1920
from compas.geometry import Point
2021
from compas.geometry import Pointcloud
2122
from compas.geometry import Polygon
@@ -40,6 +41,7 @@
4041
from .lineobject import LineObject
4142
from .meshobject import MeshObject
4243
from .networkobject import NetworkObject
44+
from .planeobject import PlaneObject
4345
from .pointobject import PointObject
4446
from .pointcloudobject import PointcloudObject
4547
from .polygonobject import PolygonObject
@@ -53,8 +55,8 @@
5355

5456

5557
@plugin(category="drawing-utils", pluggable_name="clear", requires=["bpy"])
56-
def clear_blender():
57-
compas_blender.clear()
58+
def clear_blender(guids=None):
59+
compas_blender.clear(guids=guids)
5860

5961

6062
@plugin(category="drawing-utils", pluggable_name="redraw", requires=["bpy"])
@@ -74,6 +76,7 @@ def register_scene_objects():
7476
register(Line, LineObject, context="Blender")
7577
register(Mesh, MeshObject, context="Blender")
7678
register(Network, NetworkObject, context="Blender")
79+
register(Plane, PlaneObject, context="Blender")
7780
register(Point, PointObject, context="Blender")
7881
register(Pointcloud, PointcloudObject, context="Blender")
7982
register(Polygon, PolygonObject, context="Blender")
@@ -99,6 +102,7 @@ def register_scene_objects():
99102
"LineObject",
100103
"MeshObject",
101104
"NetworkObject",
105+
"PlaneObject",
102106
"PointObject",
103107
"PointcloudObject",
104108
"PolygonObject",

src/compas_blender/scene/boxobject.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def draw(
3333
color: Optional[Color] = None,
3434
collection: Optional[Union[str, bpy.types.Collection]] = None,
3535
show_wire: bool = True,
36-
) -> bpy.types.Object:
36+
) -> list[bpy.types.Object]:
3737
"""Draw the box associated with the scene object.
3838
3939
Parameters
@@ -47,7 +47,7 @@ def draw(
4747
4848
Returns
4949
-------
50-
:blender:`bpy.types.Object`
50+
list[:blender:`bpy.types.Object`]
5151
The object(s) created in Blender to represent the box.
5252
5353
"""
@@ -61,4 +61,5 @@ def draw(
6161
obj = self.create_object(mesh, name=name)
6262
self.update_object(obj, color=color, collection=collection, show_wire=show_wire)
6363

64-
return obj
64+
self._guids = [obj]
65+
return self.guids

src/compas_blender/scene/capsuleobject.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def draw(
3535
v: int = 16,
3636
show_wire: bool = False,
3737
shade_smooth: bool = True,
38-
) -> bpy.types.Object:
38+
) -> list[bpy.types.Object]:
3939
"""Draw the capsule associated with the scene object.
4040
4141
Parameters
@@ -55,7 +55,7 @@ def draw(
5555
5656
Returns
5757
-------
58-
:blender:`bpy.types.Object`
58+
list[:blender:`bpy.types.Object`]
5959
The objects created in Blender.
6060
6161
"""
@@ -71,4 +71,5 @@ def draw(
7171
obj = self.create_object(mesh, name=name)
7272
self.update_object(obj, color=color, collection=collection, show_wire=show_wire)
7373

74-
return obj
74+
self._guids = [obj]
75+
return self.guids

src/compas_blender/scene/circleobject.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CircleObject(BlenderSceneObject, GeometryObject):
2525
def __init__(self, circle: Circle, **kwargs: Any):
2626
super().__init__(geometry=circle, **kwargs)
2727

28-
def draw(self, color: Optional[Color] = None, collection: Optional[str] = None) -> bpy.types.Object:
28+
def draw(self, color: Optional[Color] = None, collection: Optional[str] = None) -> list[bpy.types.Object]:
2929
"""Draw the circle.
3030
3131
Parameters
@@ -37,7 +37,7 @@ def draw(self, color: Optional[Color] = None, collection: Optional[str] = None)
3737
3838
Returns
3939
-------
40-
:blender:`bpy.types.Object`
40+
list[:blender:`bpy.types.Object`]
4141
The object created in Blender.
4242
4343
"""
@@ -49,4 +49,5 @@ def draw(self, color: Optional[Color] = None, collection: Optional[str] = None)
4949
self.objects.append(obj)
5050
self.update_object(obj, color=color, collection=collection, transformation=self.geometry.transformation)
5151

52-
return obj
52+
self._guids = [obj]
53+
return self.guids

src/compas_blender/scene/coneobject.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ def draw(
3333
collection: Optional[str] = None,
3434
u: int = 16,
3535
show_wire: bool = False,
36-
shade_smooth: bool = True,
37-
) -> bpy.types.Object:
36+
shade_smooth: bool = False,
37+
) -> list[bpy.types.Object]:
3838
"""Draw the cone associated with the scene object.
3939
4040
Parameters
@@ -52,7 +52,7 @@ def draw(
5252
5353
Returns
5454
-------
55-
:blender:`bpy.types.Object`
55+
list[:blender:`bpy.types.Object`]
5656
The objects created in Blender.
5757
5858
"""
@@ -67,4 +67,5 @@ def draw(
6767
obj = self.create_object(mesh, name=name)
6868
self.update_object(obj, color=color, collection=collection, show_wire=show_wire)
6969

70-
return obj
70+
self._guids = [obj]
71+
return self.guids

0 commit comments

Comments
 (0)