scene_documentation.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. from nicegui import ui
  2. from ..documentation_tools import text_demo
  3. def main_demo() -> None:
  4. with ui.scene().classes('w-full h-64') as scene:
  5. scene.sphere().material('#4488ff')
  6. scene.cylinder(1, 0.5, 2, 20).material('#ff8800', opacity=0.5).move(-2, 1)
  7. scene.extrusion([[0, 0], [0, 1], [1, 0.5]], 0.1).material('#ff8888').move(-2, -2)
  8. with scene.group().move(z=2):
  9. scene.box().move(x=2)
  10. scene.box().move(y=2).rotate(0.25, 0.5, 0.75)
  11. scene.box(wireframe=True).material('#888888').move(x=2, y=2)
  12. scene.line([-4, 0, 0], [-4, 2, 0]).material('#ff0000')
  13. scene.curve([-4, 0, 0], [-4, -1, 0], [-3, -1, 0], [-3, -2, 0]).material('#008800')
  14. logo = 'https://avatars.githubusercontent.com/u/2843826'
  15. scene.texture(logo, [[[0.5, 2, 0], [2.5, 2, 0]],
  16. [[0.5, 0, 0], [2.5, 0, 0]]]).move(1, -2)
  17. teapot = 'https://upload.wikimedia.org/wikipedia/commons/9/93/Utah_teapot_(solid).stl'
  18. scene.stl(teapot).scale(0.2).move(-3, 4)
  19. scene.text('2D', 'background: rgba(0, 0, 0, 0.2); border-radius: 5px; padding: 5px').move(z=2)
  20. scene.text3d('3D', 'background: rgba(0, 0, 0, 0.2); border-radius: 5px; padding: 5px').move(y=-2).scale(.05)
  21. def more() -> None:
  22. @text_demo('Handling Click Events', '''
  23. You can use the `on_click` argument to `ui.scene` to handle click events.
  24. The callback receives a `SceneClickEventArguments` object with the following attributes:
  25. - `click_type`: the type of click ("click" or "dblclick").
  26. - `button`: the button that was clicked (1, 2, or 3).
  27. - `alt`, `ctrl`, `meta`, `shift`: whether the alt, ctrl, meta, or shift key was pressed.
  28. - `hits`: a list of `SceneClickEventHit` objects, sorted by distance from the camera.
  29. The `SceneClickEventHit` object has the following attributes:
  30. - `object_id`: the id of the object that was clicked.
  31. - `object_name`: the name of the object that was clicked.
  32. - `x`, `y`, `z`: the x, y and z coordinates of the click.
  33. ''')
  34. def click_events() -> None:
  35. from nicegui import events
  36. def handle_click(e: events.SceneClickEventArguments):
  37. hit = e.hits[0]
  38. name = hit.object_name or hit.object_id
  39. ui.notify(f'You clicked on the {name} at ({hit.x:.2f}, {hit.y:.2f}, {hit.z:.2f})')
  40. with ui.scene(width=285, height=220, on_click=handle_click) as scene:
  41. scene.sphere().move(x=-1, z=1).with_name('sphere')
  42. scene.box().move(x=1, z=1).with_name('box')
  43. @text_demo('Rendering point clouds', '''
  44. You can render point clouds using the `point_cloud` method.
  45. The `points` argument is a list of point coordinates, and the `colors` argument is a list of RGB colors (0..1).
  46. ''')
  47. def point_clouds() -> None:
  48. import numpy as np
  49. with ui.scene().classes('w-full h-64') as scene:
  50. x, y = np.meshgrid(np.linspace(-3, 3), np.linspace(-3, 3))
  51. z = np.sin(x) * np.cos(y) + 1
  52. points = np.dstack([x, y, z]).reshape(-1, 3)
  53. scene.point_cloud(points=points, colors=points, point_size=0.1)