Skip to content

Commit 8e83a7d

Browse files
committed
use clear plugin for both scene and scene object
1 parent 322ebd9 commit 8e83a7d

File tree

11 files changed

+49
-63
lines changed

11 files changed

+49
-63
lines changed

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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,17 @@ 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-
sceneobject.clear()
5661
drawn_objects += sceneobject.draw()
5762

5863
if drawn_objects:

src/compas/scene/sceneobject.py

Lines changed: 3 additions & 2 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):
@@ -55,7 +56,7 @@ def draw(self):
5556
"""The main drawing method."""
5657
raise NotImplementedError
5758

58-
@abstractmethod
5959
def clear(self):
6060
"""The main clearing method."""
61-
raise NotImplementedError
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555

5656

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

6161

6262
@plugin(category="drawing-utils", pluggable_name="redraw", requires=["bpy"])

src/compas_blender/scene/sceneobject.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ def __init__(self, **kwargs: Any):
3333
super().__init__(**kwargs)
3434
self.objects = []
3535

36-
def clear(self):
37-
for obj in self.guids:
38-
self.delete_object(obj)
39-
4036
# many of the methods below will be added to a general scene object in the future
4137
# to make them universaly accessible they are added here for now
4238

@@ -187,21 +183,6 @@ def set_object_tranformation(self, obj: bpy.types.Object, transformation: Option
187183
elif self.transformation:
188184
obj.matrix_world = conversions.transformation_to_blender(self.transformation)
189185

190-
def delete_object(self, obj) -> None:
191-
"""Delete a Blender object.
192-
193-
Parameters
194-
----------
195-
obj : :class:`bpy.types.Object`
196-
The Blender object to delete.
197-
198-
Returns
199-
-------
200-
None
201-
202-
"""
203-
bpy.data.objects.remove(obj, do_unlink=True)
204-
205186
# =============================================================================
206187
# Collections
207188
# =============================================================================

src/compas_ghpython/scene/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@
5656
from .brepobject import BrepObject
5757

5858

59+
@plugin(category="drawing-utils", pluggable_name="clear", requires=["Grasshopper"])
60+
def clear_GH(guids=None):
61+
pass
62+
63+
64+
@plugin(category="drawing-utils", pluggable_name="redraw", requires=["Grasshopper"])
65+
def redraw_GH():
66+
pass
67+
68+
5969
@plugin(category="factories", requires=["Rhino"])
6070
def register_scene_objects():
6171
register(Box, BoxObject, context="Grasshopper")

src/compas_ghpython/scene/sceneobject.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,3 @@ class GHSceneObject(SceneObject):
1010

1111
def __init__(self, **kwargs):
1212
super(GHSceneObject, self).__init__(**kwargs)
13-
14-
def clear(self):
15-
"""Clear the object from the Rhino scene. In GH, this is not necessary."""
16-
pass

src/compas_rhino/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@
5757
# =============================================================================
5858

5959

60-
def clear():
61-
guids = get_objects() # noqa: F405
60+
def clear(guids=None):
61+
if guids is None:
62+
guids = get_objects() # noqa: F405
6263
delete_objects(guids, purge=True) # noqa: F405
6364

6465

src/compas_rhino/scene/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@
6464

6565

6666
@plugin(category="drawing-utils", pluggable_name="clear", requires=["Rhino"])
67-
def clear_rhino():
68-
compas_rhino.clear()
67+
def clear_rhino(guids=None):
68+
compas_rhino.clear(guids=guids)
6969

7070

7171
@plugin(category="drawing-utils", pluggable_name="redraw", requires=["Rhino"])

0 commit comments

Comments
 (0)