Is there a way to let users use their mouse to click on UI that is placed in a scene via WidgetComponent and UiContainers? When UI is clicked on in the editor it just selects the ui object that has the bufferview material applied to it.
I am looking to use the XR worldspace UI and not the overlay UI as overlays cannot be occluded by prims.
[dependencies]
“omni.kit.xr.scene_view.core” = {}
“omni.kit.xr.scene_view.utils” = {}
Extension Code
kv.clickable_ui.zip (114.2 KB)
"""Short example on how clickable worldspace UI can be configured and manipulated in scene."""
import carb
import omni.ext
from typing import Optional
from omni import ui
from omni.ui import color as cl
from omni.kit.xr.scene_view.utils import UiContainer, WidgetComponent
DARK_BLUE = cl("#022752FF")
WHITE = 0xFFFFFFFF
UI_HEIGHT = 100
class SimpleTextWidget(ui.Widget):
def __init__(
self,
**kwargs,
):
super().__init__(**kwargs)
self._ui_button: Optional[ui.Button] = None
self._build_ui()
def on_user_clicked_ui(
self, x: float, y: float, button: int, modifier: int
) -> None:
print("Clicked Text!")
self.set_mouse_pressed_fn(on_user_clicked_ui)
def on_my_button_click(self):
print("My button was clicked!")
def _build_ui(self):
"""Function that is called every frame to draw UI in scene."""
with ui.ZStack():
ui.Rectangle(style={"background_color": DARK_BLUE})
with ui.VStack(spacing=0, height=UI_HEIGHT, alignment=ui.Alignment.CENTER):
ui.Spacer(height=5)
self._ui_button = ui.Button(
"Click Me!",
clicked_fn=self.on_my_button_click,
alignment=ui.Alignment.CENTER,
height=0,
style={
"color": WHITE,
"font_size": 16,
},
)
ui.Spacer(height=5)
class MyExtension(omni.ext.IExt):
"""Example clickable worldspace (scene space) ui usage."""
def on_startup(self, _ext_id):
"""This is called every time the extension is activated."""
print("[kv.clickable_ui] Extension startup")
# Create widget to contain UI elements
self.static_text_widget_component = WidgetComponent(
SimpleTextWidget,
width=200,
height=UI_HEIGHT,
resolution_scale=10,
unit_to_pixel_scale=1,
)
# Create UI container to hold widgets
self._ui_container = UiContainer(self.static_text_widget_component)
# Configure Orientation
_manipulator = self._ui_container.manipulator
_manipulator.rotation_degrees = carb.Float3(90, -200, 0)
_manipulator.scale = carb.Float3(1, 1, 1)
_manipulator.translation = carb.Float3(0, 0, UI_HEIGHT * 4)
def on_shutdown(self):
"""This is called every time the extension is deactivated. It is used
to clean up the extension state."""
self._ui_container = None
self.static_text_widget_component = None
print("[kv.clickable_ui] Extension shutdown")
Even the XR examples are not clickable either using a mouse in editor: https://github.com/NVIDIA-Omniverse/kit-xr-samples

