diff --git a/nicegui/elements/scene.js b/nicegui/elements/scene.js index 5e2a929fd..721ce5df8 100644 --- a/nicegui/elements/scene.js +++ b/nicegui/elements/scene.js @@ -413,7 +413,13 @@ export default { const geometry = this.objects.get(object_id).geometry; set_point_cloud_data(position, color, geometry); }, - ungroup(object_id) { + attach(object_id, parent_id) { + if (!this.objects.has(object_id)) return; + const object = this.objects.get(object_id); + const parent = this.objects.get(parent_id); + parent.add(object); + }, + detach(object_id) { if (!this.objects.has(object_id)) return; const object = this.objects.get(object_id); object.removeFromParent(); diff --git a/nicegui/elements/scene_object3d.py b/nicegui/elements/scene_object3d.py index c1462f8e2..9f7a7541d 100644 --- a/nicegui/elements/scene_object3d.py +++ b/nicegui/elements/scene_object3d.py @@ -196,10 +196,15 @@ def draggable(self, value: bool = True) -> Self: self._draggable() return self - def ungroup(self) -> None: + def attach(self, parent: Object3D) -> None: + """Attach the object to a parent object.""" + self.parent = parent + self.scene.run_method('attach', self.id, parent.id) + + def detach(self) -> None: """Remove the object from its parent group object.""" self.parent = self.scene.stack[0] - self.scene.run_method('ungroup', self.id) + self.scene.run_method('detach', self.id) @property def children(self) -> List[Object3D]: diff --git a/website/documentation/content/scene_documentation.py b/website/documentation/content/scene_documentation.py index 52e924f1a..210169c1f 100644 --- a/website/documentation/content/scene_documentation.py +++ b/website/documentation/content/scene_documentation.py @@ -268,10 +268,10 @@ def __init__(self, name: str, *, length: float = 1.0) -> None: CoordinateSystem('custom frame').move(-2, -2, 1).rotate(0.1, 0.2, 0.3) -@doc.demo('Ungrouping objects', ''' - To remove an object from its parent group objects, you can use the `ungroup` method. +@doc.demo('Attaching/detaching objects', ''' + To add or remove objects from groups you can use the `attach` and `detach` methods. ''') -def ungroup() -> None: +def attach_detach() -> None: import math import time @@ -282,7 +282,8 @@ def ungroup() -> None: c = scene.sphere().move(2) ui.timer(0.1, lambda: group.move(y=math.sin(time.time()))) - ui.button('Ungroup', on_click=a.ungroup) + ui.button('Detach', on_click=a.detach) + ui.button('Attach', on_click=lambda: a.attach(group)) doc.reference(ui.scene)