Skip to content

Commit

Permalink
introduce attach and detach methods
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Nov 21, 2024
1 parent 4a860f1 commit ff3425c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
8 changes: 7 additions & 1 deletion nicegui/elements/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 7 additions & 2 deletions nicegui/elements/scene_object3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
9 changes: 5 additions & 4 deletions website/documentation/content/scene_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

0 comments on commit ff3425c

Please sign in to comment.