Recommended approach for combining context menu with 3D scene click? #3451
Replies: 3 comments 1 reply
-
Here's a code snippet that almost does the above. On left click it updates a context menu based on clicked element in 3D scene. On right click it opens the context menu. I'm interested in performing both these steps together on right click only. from nicegui import events, ui
def update_context_menu(e: events.SceneClickEventArguments):
for hit in e.hits:
if hit.object_name == "":
continue
context_menu.clear()
if hit.object_name == "my_sphere":
with context_menu:
ui.menu_item("SPHERE", auto_close=False)
ui.menu_item("inspect")
ui.menu_item("open")
if hit.object_name == "my_box":
with context_menu:
ui.menu_item("BOX", auto_close=False)
ui.menu_item("rotate")
ui.menu_item("move")
ui.notify(f"Left clicked, updated context_menu to {hit.object_name}")
with ui.element("div"):
with ui.context_menu() as context_menu:
ui.menu_item("NOTHING SELECTED", auto_close=False)
with ui.scene(width=1500, height=900, on_click=update_context_menu) as scene:
sphere = scene.sphere().move(z=1)
sphere.name = "my_sphere"
box = scene.box().move(x=3)
box.name = "my_box"
scene.on("contextmenu", lambda: ui.notify("right clicked"))
ui.run() Screen.Recording.2024-08-08-v2.mov |
Beta Was this translation helpful? Give feedback.
-
Interesting problem, @h0uter! this.$el.onclick = click_handler;
this.$el.ondblclick = click_handler;
this.$el.oncontextmenu = click_handler; // this line is new we can handle right clicks in Python: def handle_click(e: events.SceneClickEventArguments) -> None:
context_menu.clear()
name = next((hit.object_name for hit in e.hits if hit.object_name), None)
if e.button == 2 and name:
with context_menu:
if name == "my_sphere":
ui.menu_item("SPHERE", auto_close=False)
ui.menu_item("inspect")
ui.menu_item("open")
if name == "my_box":
ui.menu_item("BOX", auto_close=False)
ui.menu_item("rotate")
ui.menu_item("move")
with ui.element():
context_menu = ui.context_menu()
with ui.scene(on_click=handle_click) as scene:
scene.sphere().move(z=1).with_name("my_sphere")
scene.box().move(x=3).with_name("my_box") I'll need to think about how to introduce this new feature without breaking existing apps. Maybe we should introduce a new parameter |
Beta Was this translation helpful? Give feedback.
-
With PR #3463 being released in version 1.4.35, I'll close this feature request as completed. |
Beta Was this translation helpful? Give feedback.
-
Is it possible to make the ui.context_menu use the 3D scene element the user clicked on to adapt itself?
It is clear how the context_menu can be adapted to regular ui elements, I am asking specifically for 3D scene objects and 3D sceneClickEvents.
see example image (Sims) for rough idea of how this would enable 3D context-aware menus for 3D applications:
Beta Was this translation helpful? Give feedback.
All reactions