Skip to content

Commit 3e7a43d

Browse files
committed
get_sceneobject_node
1 parent 55dcfae commit 3e7a43d

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

src/compas/scene/scene.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def add(self, item, parent=None, **kwargs):
131131
else:
132132
if not isinstance(parent, SceneObject):
133133
raise ValueError("Parent is not a SceneObject.", parent)
134-
parent_node = self.tree.get_node_by_name(parent.guid)
134+
parent_node = self.get_sceneobject_node(parent)
135135
if parent_node is None:
136136
raise ValueError("Parent is not part of the scene.", parent)
137137

@@ -148,13 +148,12 @@ def remove(self, sceneobject):
148148
The scene object to remove.
149149
"""
150150
# type: (SceneObject) -> None
151-
guid = str(sceneobject.guid)
152-
self.objectstore.pop(guid, None)
153-
node = self.tree.get_node_by_name(guid)
151+
node = self.get_sceneobject_node(sceneobject)
154152
if node:
155153
for descendant in node.descendants:
156154
self.objectstore.pop(descendant.name, None)
157155
self.tree.remove(node)
156+
self.objectstore.pop(str(sceneobject.guid), None)
158157

159158
def clear_context(self, guids=None):
160159
# type: (list | None) -> None
@@ -265,7 +264,7 @@ def find_by_name(self, name):
265264
:class:`SceneObject`
266265
267266
"""
268-
return self.get_node_by_name(name=name)
267+
return next((obj for obj in self.objects if obj.name == name), None)
269268

270269
def find_by_itemtype(self, itemtype):
271270
# type: (type) -> SceneObject | None
@@ -304,3 +303,30 @@ def find_all_by_itemtype(self, itemtype):
304303
if isinstance(obj.item, itemtype):
305304
sceneobjects.append(obj)
306305
return sceneobjects
306+
307+
def get_sceneobject_node(self, sceneobject):
308+
# type: (SceneObject) -> TreeNode
309+
"""Get the TreeNode that corresponds to a scene object.
310+
311+
Parameters
312+
----------
313+
sceneobject : :class:`compas.scene.SceneObject`
314+
315+
Returns
316+
-------
317+
:class:`compas.datastructures.TreeNode`
318+
319+
Raises
320+
------
321+
TypeError
322+
If the scene object is not a :class:`compas.scene.SceneObject`.
323+
ValueError
324+
If the scene object is not part of this scene.
325+
"""
326+
327+
if not isinstance(sceneobject, SceneObject):
328+
raise TypeError("SceneObject expected.", sceneobject)
329+
if sceneobject.scene is not self:
330+
raise ValueError("SceneObject not part of this scene.", sceneobject)
331+
332+
return self.tree.get_node_by_name(sceneobject.guid)

src/compas/scene/sceneobject.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def item(self):
186186
@property
187187
def node(self):
188188
# type: () -> compas.datastructures.TreeNode
189-
return self.scene.tree.get_node_by_name(self.guid)
189+
return self.scene.get_sceneobject_node(self)
190190

191191
@property
192192
def is_root(self):

0 commit comments

Comments
 (0)