Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

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

### Removed

Expand Down
2 changes: 1 addition & 1 deletion src/compas/scene/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


@pluggable(category="drawing-utils")
def clear():
def clear(guids=None):
raise NotImplementedError


Expand Down
20 changes: 8 additions & 12 deletions src/compas/scene/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,18 @@ def _get_node(self, sceneobject):
raise Exception("Scene object not in scene")

def clear(self):
clear()
guids = []
for sceneobject in self.sceneobjects:
guids += sceneobject.guids
sceneobject._guids = None
clear(guids=guids)

def redraw(self):
self.clear()

Comment on lines 49 to +58
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomvanmele How about now? we collect the all guids of scene then clear all together. Also we add guids as argument for clear, so it does not delete the other stuff in context that are not managed by the scene.

drawn_objects = []
for sceneobject in self.sceneobjects:
drawn_object = sceneobject.draw()

# TODO: unify output of draw(), so we don't have to do this
if isinstance(drawn_object, (list, tuple)):
for item in drawn_object:
if isinstance(item, (list, tuple)):
drawn_objects.extend(item)
else:
drawn_objects.append(item)
else:
drawn_objects.append(drawn_object)
drawn_objects += sceneobject.draw()

if drawn_objects:
redraw()
Expand Down
22 changes: 13 additions & 9 deletions src/compas/scene/sceneobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from abc import abstractmethod
from .descriptors.protocol import DescriptorProtocol
from .context import clear


class SceneObject(object):
Expand All @@ -13,14 +14,12 @@ class SceneObject(object):
----------
item : Any
The item which should be visualized using the created SceneObject.
context : str, optional
Explicit context to pick the SceneObject from.
If not specified, an attempt will be made to automatically detect the appropriate context.


Attributes
----------
ITEM_SCENEOBJECT : dict[str, dict[Type[:class:`~compas.data.Data`], Type[:class:`~compas.scene.SceneObject`]]]
Dictionary mapping data types to the corresponding scene objects types per visualization context.
guids : list[object]
The GUIDs of the items drawn in the visualization context.

"""

Expand All @@ -30,6 +29,11 @@ class SceneObject(object):
def __init__(self, item, **kwargs):
self._item = item
self._transformation = None
self._guids = None

@property
def guids(self):
return self._guids or []

@property
def transformation(self):
Expand All @@ -52,7 +56,7 @@ def draw(self):
"""The main drawing method."""
raise NotImplementedError

@staticmethod
def draw_collection(collection):
"""Drawing method for drawing an entire collection of objects."""
raise NotImplementedError
def clear(self):
"""The main clearing method."""
clear(guids=self.guids)
self._guids = None
Comment on lines -55 to +62
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we also use same same clear function for individual object. In this way the clear function of the object doesn't need to be re-implemented anymore

38 changes: 21 additions & 17 deletions src/compas_blender/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@
from .utilities import * # noqa: F401 F403


def clear():
def clear(guids=None):
"""Clear all scene objects."""
# delete all objects
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete(use_global=True, confirm=False)
# delete data
delete_unused_data() # noqa: F405
# delete collections
for collection in bpy.context.scene.collection.children:
bpy.context.scene.collection.children.unlink(collection)
for block in bpy.data.collections:
objects = [o for o in block.objects if o.users]
while objects:
bpy.data.objects.remove(objects.pop())
for collection in block.children:
block.children.unlink(collection)
if block.users == 0:
bpy.data.collections.remove(block)
if guids is None:
# delete all objects
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete(use_global=True, confirm=False)
# delete data
delete_unused_data() # noqa: F405
# delete collections
for collection in bpy.context.scene.collection.children:
bpy.context.scene.collection.children.unlink(collection)
for block in bpy.data.collections:
objects = [o for o in block.objects if o.users]
while objects:
bpy.data.objects.remove(objects.pop())
for collection in block.children:
block.children.unlink(collection)
if block.users == 0:
bpy.data.collections.remove(block)
else:
for obj in guids:
bpy.data.objects.remove(obj, do_unlink=True)


def redraw():
Expand Down
8 changes: 6 additions & 2 deletions src/compas_blender/scene/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from compas.geometry import Cylinder
from compas.geometry import Frame
from compas.geometry import Line
from compas.geometry import Plane
from compas.geometry import Point
from compas.geometry import Pointcloud
from compas.geometry import Polygon
Expand All @@ -40,6 +41,7 @@
from .lineobject import LineObject
from .meshobject import MeshObject
from .networkobject import NetworkObject
from .planeobject import PlaneObject
from .pointobject import PointObject
from .pointcloudobject import PointcloudObject
from .polygonobject import PolygonObject
Expand All @@ -53,8 +55,8 @@


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


@plugin(category="drawing-utils", pluggable_name="redraw", requires=["bpy"])
Expand All @@ -74,6 +76,7 @@ def register_scene_objects():
register(Line, LineObject, context="Blender")
register(Mesh, MeshObject, context="Blender")
register(Network, NetworkObject, context="Blender")
register(Plane, PlaneObject, context="Blender")
register(Point, PointObject, context="Blender")
register(Pointcloud, PointcloudObject, context="Blender")
register(Polygon, PolygonObject, context="Blender")
Expand All @@ -99,6 +102,7 @@ def register_scene_objects():
"LineObject",
"MeshObject",
"NetworkObject",
"PlaneObject",
"PointObject",
"PointcloudObject",
"PolygonObject",
Expand Down
7 changes: 4 additions & 3 deletions src/compas_blender/scene/boxobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def draw(
color: Optional[Color] = None,
collection: Optional[Union[str, bpy.types.Collection]] = None,
show_wire: bool = True,
) -> bpy.types.Object:
) -> list[bpy.types.Object]:
"""Draw the box associated with the scene object.

Parameters
Expand All @@ -47,7 +47,7 @@ def draw(

Returns
-------
:blender:`bpy.types.Object`
list[:blender:`bpy.types.Object`]
The object(s) created in Blender to represent the box.

"""
Expand All @@ -61,4 +61,5 @@ def draw(
obj = self.create_object(mesh, name=name)
self.update_object(obj, color=color, collection=collection, show_wire=show_wire)

return obj
self._guids = [obj]
return self.guids
7 changes: 4 additions & 3 deletions src/compas_blender/scene/capsuleobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def draw(
v: int = 16,
show_wire: bool = False,
shade_smooth: bool = True,
) -> bpy.types.Object:
) -> list[bpy.types.Object]:
"""Draw the capsule associated with the scene object.

Parameters
Expand All @@ -55,7 +55,7 @@ def draw(

Returns
-------
:blender:`bpy.types.Object`
list[:blender:`bpy.types.Object`]
The objects created in Blender.

"""
Expand All @@ -71,4 +71,5 @@ def draw(
obj = self.create_object(mesh, name=name)
self.update_object(obj, color=color, collection=collection, show_wire=show_wire)

return obj
self._guids = [obj]
return self.guids
7 changes: 4 additions & 3 deletions src/compas_blender/scene/circleobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CircleObject(BlenderSceneObject, GeometryObject):
def __init__(self, circle: Circle, **kwargs: Any):
super().__init__(geometry=circle, **kwargs)

def draw(self, color: Optional[Color] = None, collection: Optional[str] = None) -> bpy.types.Object:
def draw(self, color: Optional[Color] = None, collection: Optional[str] = None) -> list[bpy.types.Object]:
"""Draw the circle.

Parameters
Expand All @@ -37,7 +37,7 @@ def draw(self, color: Optional[Color] = None, collection: Optional[str] = None)

Returns
-------
:blender:`bpy.types.Object`
list[:blender:`bpy.types.Object`]
The object created in Blender.

"""
Expand All @@ -49,4 +49,5 @@ def draw(self, color: Optional[Color] = None, collection: Optional[str] = None)
self.objects.append(obj)
self.update_object(obj, color=color, collection=collection, transformation=self.geometry.transformation)

return obj
self._guids = [obj]
return self.guids
9 changes: 5 additions & 4 deletions src/compas_blender/scene/coneobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def draw(
collection: Optional[str] = None,
u: int = 16,
show_wire: bool = False,
shade_smooth: bool = True,
) -> bpy.types.Object:
shade_smooth: bool = False,
) -> list[bpy.types.Object]:
"""Draw the cone associated with the scene object.

Parameters
Expand All @@ -52,7 +52,7 @@ def draw(

Returns
-------
:blender:`bpy.types.Object`
list[:blender:`bpy.types.Object`]
The objects created in Blender.

"""
Expand All @@ -67,4 +67,5 @@ def draw(
obj = self.create_object(mesh, name=name)
self.update_object(obj, color=color, collection=collection, show_wire=show_wire)

return obj
self._guids = [obj]
return self.guids
11 changes: 7 additions & 4 deletions src/compas_blender/scene/curveobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def draw(
self,
color: Optional[Color] = None,
collection: Optional[str] = None,
) -> bpy.types.Object:
) -> list[bpy.types.Object]:
"""Draw the curve.

Parameters
Expand All @@ -42,14 +42,17 @@ def draw(

Returns
-------
:blender:`bpy.types.Object`
list[:blender:`bpy.types.Object`]
The objects created in Blender.

"""
name = self.geometry.name
color = Color.coerce(color) or self.color
curve = conversions.nurbscurve_to_blender_curve(self.geometry)
# TODO: add support for NurbsCurve
curve = conversions.polyline_to_blender_curve(self.geometry.to_polyline(), name=name)

obj = self.create_object(curve, name=name)
self.update_object(obj, color=color, collection=collection)

return obj
self._guids = [obj]
return self.guids
9 changes: 5 additions & 4 deletions src/compas_blender/scene/cylinderobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def draw(
collection: Optional[str] = None,
u: int = 16,
show_wire: bool = False,
shade_smooth: bool = True,
) -> bpy.types.Object:
shade_smooth: bool = False,
) -> list[bpy.types.Object]:
"""Draw the cylinder associated with the scene object.

Parameters
Expand All @@ -52,7 +52,7 @@ def draw(

Returns
-------
:blender:`bpy.types.Object`
list[:blender:`bpy.types.Object`]
The objects created in Blender.

"""
Expand All @@ -67,4 +67,5 @@ def draw(
obj = self.create_object(mesh, name=name)
self.update_object(obj, color=color, collection=collection, show_wire=show_wire)

return obj
self._guids = [obj]
return self.guids
Loading