1
0

scene_documentation.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.scene)
  4. def main_demo() -> None:
  5. with ui.scene().classes('w-full h-64') as scene:
  6. scene.axes_helper()
  7. scene.sphere().material('#4488ff').move(2, 2)
  8. scene.cylinder(1, 0.5, 2, 20).material('#ff8800', opacity=0.5).move(-2, 1)
  9. scene.extrusion([[0, 0], [0, 1], [1, 0.5]], 0.1).material('#ff8888').move(2, -1)
  10. with scene.group().move(z=2):
  11. scene.box().move(x=2)
  12. scene.box().move(y=2).rotate(0.25, 0.5, 0.75)
  13. scene.box(wireframe=True).material('#888888').move(x=2, y=2)
  14. scene.line([-4, 0, 0], [-4, 2, 0]).material('#ff0000')
  15. scene.curve([-4, 0, 0], [-4, -1, 0], [-3, -1, 0], [-3, 0, 0]).material('#008800')
  16. logo = 'https://avatars.githubusercontent.com/u/2843826'
  17. scene.texture(logo, [[[0.5, 2, 0], [2.5, 2, 0]],
  18. [[0.5, 0, 0], [2.5, 0, 0]]]).move(1, -3)
  19. teapot = 'https://upload.wikimedia.org/wikipedia/commons/9/93/Utah_teapot_(solid).stl'
  20. scene.stl(teapot).scale(0.2).move(-3, 4)
  21. avocado = 'https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/Avocado/glTF-Binary/Avocado.glb'
  22. scene.gltf(avocado).scale(40).move(-2, -3, 0.5)
  23. scene.text('2D', 'background: rgba(0, 0, 0, 0.2); border-radius: 5px; padding: 5px').move(z=2)
  24. scene.text3d('3D', 'background: rgba(0, 0, 0, 0.2); border-radius: 5px; padding: 5px').move(y=-2).scale(.05)
  25. @doc.demo('Handling Click Events', '''
  26. You can use the `on_click` argument to `ui.scene` to handle click events.
  27. The callback receives a `SceneClickEventArguments` object with the following attributes:
  28. - `click_type`: the type of click ("click" or "dblclick").
  29. - `button`: the button that was clicked (1, 2, or 3).
  30. - `alt`, `ctrl`, `meta`, `shift`: whether the alt, ctrl, meta, or shift key was pressed.
  31. - `hits`: a list of `SceneClickEventHit` objects, sorted by distance from the camera.
  32. The `SceneClickEventHit` object has the following attributes:
  33. - `object_id`: the id of the object that was clicked.
  34. - `object_name`: the name of the object that was clicked.
  35. - `x`, `y`, `z`: the x, y and z coordinates of the click.
  36. ''')
  37. def click_events() -> None:
  38. from nicegui import events
  39. def handle_click(e: events.SceneClickEventArguments):
  40. hit = e.hits[0]
  41. name = hit.object_name or hit.object_id
  42. ui.notify(f'You clicked on the {name} at ({hit.x:.2f}, {hit.y:.2f}, {hit.z:.2f})')
  43. with ui.scene(width=285, height=220, on_click=handle_click) as scene:
  44. scene.sphere().move(x=-1, z=1).with_name('sphere')
  45. scene.box().move(x=1, z=1).with_name('box')
  46. @doc.demo('Context menu for 3D objects', '''
  47. This demo shows how to create a context menu for 3D objects.
  48. By setting the `click_events` argument to `['contextmenu']`, the `handle_click` function will be called on right-click.
  49. It clears the context menu and adds items based on the object that was clicked.
  50. ''')
  51. def context_menu_for_3d_objects():
  52. from nicegui import events
  53. def handle_click(e: events.SceneClickEventArguments) -> None:
  54. context_menu.clear()
  55. name = next((hit.object_name for hit in e.hits if hit.object_name), None)
  56. with context_menu:
  57. if name == 'sphere':
  58. ui.item('SPHERE').classes('font-bold')
  59. ui.menu_item('inspect')
  60. ui.menu_item('open')
  61. if name == 'box':
  62. ui.item('BOX').classes('font-bold')
  63. ui.menu_item('rotate')
  64. ui.menu_item('move')
  65. with ui.element():
  66. context_menu = ui.context_menu()
  67. with ui.scene(width=285, height=220, on_click=handle_click,
  68. click_events=['contextmenu']) as scene:
  69. scene.sphere().move(x=-1, z=1).with_name('sphere')
  70. scene.box().move(x=1, z=1).with_name('box')
  71. @doc.demo('Draggable objects', '''
  72. You can make objects draggable using the `.draggable` method.
  73. There is an optional `on_drag_start` and `on_drag_end` argument to `ui.scene` to handle drag events.
  74. The callbacks receive a `SceneDragEventArguments` object with the following attributes:
  75. - `type`: the type of drag event ("dragstart" or "dragend").
  76. - `object_id`: the id of the object that was dragged.
  77. - `object_name`: the name of the object that was dragged.
  78. - `x`, `y`, `z`: the x, y and z coordinates of the dragged object.
  79. You can also use the `drag_constraints` argument to set comma-separated JavaScript expressions
  80. for constraining positions of dragged objects.
  81. ''')
  82. def draggable_objects() -> None:
  83. from nicegui import events
  84. def handle_drag(e: events.SceneDragEventArguments):
  85. ui.notify(f'You dropped the sphere at ({e.x:.2f}, {e.y:.2f}, {e.z:.2f})')
  86. with ui.scene(width=285, height=220,
  87. drag_constraints='z = 1', on_drag_end=handle_drag) as scene:
  88. sphere = scene.sphere().move(z=1).draggable()
  89. ui.switch('draggable sphere',
  90. value=sphere.draggable_,
  91. on_change=lambda e: sphere.draggable(e.value))
  92. @doc.demo('Subscribe to the drag event', '''
  93. By default, a draggable object is only updated when the drag ends to avoid performance issues.
  94. But you can explicitly subscribe to the "drag" event to get immediate updates.
  95. In this demo we update the position and size of a box based on the positions of two draggable spheres.
  96. ''')
  97. def immediate_updates() -> None:
  98. from nicegui import events
  99. with ui.scene(width=285, drag_constraints='z=0') as scene:
  100. box = scene.box(1, 1, 0.2).move(0, 0).material('Orange')
  101. sphere1 = scene.sphere(0.2).move(0.5, -0.5).material('SteelBlue').draggable()
  102. sphere2 = scene.sphere(0.2).move(-0.5, 0.5).material('SteelBlue').draggable()
  103. def handle_drag(e: events.GenericEventArguments) -> None:
  104. x1 = sphere1.x if e.args['object_id'] == sphere2.id else e.args['x']
  105. y1 = sphere1.y if e.args['object_id'] == sphere2.id else e.args['y']
  106. x2 = sphere2.x if e.args['object_id'] == sphere1.id else e.args['x']
  107. y2 = sphere2.y if e.args['object_id'] == sphere1.id else e.args['y']
  108. box.move((x1 + x2) / 2, (y1 + y2) / 2).scale(x2 - x1, y2 - y1, 1)
  109. scene.on('drag', handle_drag)
  110. @doc.demo('Rendering point clouds', '''
  111. You can render point clouds using the `point_cloud` method.
  112. The `points` argument is a list of point coordinates, and the `colors` argument is a list of RGB colors (0..1).
  113. You can update the cloud using its `set_points()` method.
  114. ''')
  115. def point_clouds() -> None:
  116. import numpy as np
  117. def generate_data(frequency: float = 1.0):
  118. x, y = np.meshgrid(np.linspace(-3, 3), np.linspace(-3, 3))
  119. z = np.sin(x * frequency) * np.cos(y * frequency) + 1
  120. points = np.dstack([x, y, z]).reshape(-1, 3)
  121. colors = points / [6, 6, 2] + [0.5, 0.5, 0]
  122. return points, colors
  123. with ui.scene().classes('w-full h-64') as scene:
  124. points, colors = generate_data()
  125. point_cloud = scene.point_cloud(points, colors, point_size=0.1)
  126. ui.slider(min=0.1, max=3, step=0.1, value=1) \
  127. .on_value_change(lambda e: point_cloud.set_points(*generate_data(e.value)))
  128. @doc.demo('Wait for Initialization', '''
  129. You can wait for the scene to be initialized with the `initialized` method.
  130. This demo animates a camera movement after the scene has been fully loaded.
  131. ''')
  132. async def wait_for_init() -> None:
  133. # @ui.page('/')
  134. # async def page():
  135. with ui.column(): # HIDE
  136. with ui.scene(width=285, height=220) as scene:
  137. scene.sphere()
  138. await scene.initialized()
  139. scene.move_camera(x=1, y=-1, z=1.5, duration=2)
  140. @doc.demo(ui.scene_view)
  141. def scene_views():
  142. with ui.grid(columns=2).classes('w-full'):
  143. with ui.scene().classes('h-64 col-span-2') as scene:
  144. teapot = 'https://upload.wikimedia.org/wikipedia/commons/9/93/Utah_teapot_(solid).stl'
  145. scene.stl(teapot).scale(0.3)
  146. with ui.scene_view(scene).classes('h-32') as scene_view1:
  147. scene_view1.move_camera(x=1, y=-3, z=5)
  148. with ui.scene_view(scene).classes('h-32') as scene_view2:
  149. scene_view2.move_camera(x=0, y=4, z=3)
  150. @doc.demo('Camera Parameters', '''
  151. You can use the `camera` argument to `ui.scene` to use a custom camera.
  152. This allows you to set the field of view of a perspective camera or the size of an orthographic camera.
  153. ''')
  154. def orthographic_camera() -> None:
  155. with ui.scene(camera=ui.scene.orthographic_camera(size=2)) \
  156. .classes('w-full h-64') as scene:
  157. scene.box()
  158. @doc.demo('Get current camera pose', '''
  159. Using the `get_camera` method you can get a dictionary of current camera parameters like position, rotation, field of view and more.
  160. This demo shows how to continuously move a sphere towards the camera.
  161. Try moving the camera around to see the sphere following it.
  162. ''')
  163. def camera_pose() -> None:
  164. with ui.scene().classes('w-full h-64') as scene:
  165. ball = scene.sphere()
  166. async def move():
  167. camera = await scene.get_camera()
  168. if camera is not None:
  169. ball.move(x=0.95 * ball.x + 0.05 * camera['position']['x'],
  170. y=0.95 * ball.y + 0.05 * camera['position']['y'],
  171. z=1.0)
  172. ui.timer(0.1, move)
  173. @doc.demo('Custom Background', '''
  174. You can set a custom background color using the `background_color` parameter of `ui.scene`.
  175. ''')
  176. def custom_background() -> None:
  177. with ui.scene(background_color='#222').classes('w-full h-64') as scene:
  178. scene.box()
  179. @doc.demo('Custom Grid', '''
  180. You can set custom grid parameters using the `grid` parameter of `ui.scene`.
  181. It accepts a tuple of two integers, the first one for the grid size and the second one for the number of divisions.
  182. ''')
  183. def custom_grid() -> None:
  184. with ui.scene(grid=(3, 2)).classes('w-full h-64') as scene:
  185. scene.sphere()
  186. @doc.demo('Custom Composed 3D Objects', '''
  187. This demo creates a custom class for visualizing a coordinate system with colored X, Y and Z axes.
  188. This can be a nice alternative to the default `axes_helper` object.
  189. ''')
  190. def custom_composed_objects() -> None:
  191. import math
  192. class CoordinateSystem(ui.scene.group):
  193. def __init__(self, name: str, *, length: float = 1.0) -> None:
  194. super().__init__()
  195. with self:
  196. for label, color, rx, ry, rz in [
  197. ('x', '#ff0000', 0, 0, -math.pi / 2),
  198. ('y', '#00ff00', 0, 0, 0),
  199. ('z', '#0000ff', math.pi / 2, 0, 0),
  200. ]:
  201. with ui.scene.group().rotate(rx, ry, rz):
  202. ui.scene.cylinder(0.02 * length, 0.02 * length, 0.8 * length) \
  203. .move(y=0.4 * length).material(color)
  204. ui.scene.cylinder(0, 0.1 * length, 0.2 * length) \
  205. .move(y=0.9 * length).material(color)
  206. ui.scene.text(label, style=f'color: {color}') \
  207. .move(y=1.1 * length)
  208. ui.scene.text(name, style='color: #808080')
  209. with ui.scene().classes('w-full h-64'):
  210. CoordinateSystem('origin')
  211. CoordinateSystem('custom frame').move(-2, -2, 1).rotate(0.1, 0.2, 0.3)
  212. @doc.demo('Attaching/detaching objects', '''
  213. To add or remove objects from groups you can use the `attach` and `detach` methods.
  214. The position and rotation of the object are preserved so that the object does not move in space.
  215. But note that scaling is not preserved.
  216. If either the parent or the object itself is scaled, the object shape and position can change.
  217. *Added in version 2.7.0*
  218. ''')
  219. def attach_detach() -> None:
  220. import math
  221. import time
  222. with ui.scene().classes('w-full h-64') as scene:
  223. with scene.group() as group:
  224. a = scene.box().move(-2)
  225. b = scene.box().move(0)
  226. c = scene.box().move(2)
  227. ui.timer(0.1, lambda: group.move(y=math.sin(time.time())).rotate(0, 0, time.time()))
  228. ui.button('Detach', on_click=a.detach)
  229. ui.button('Attach', on_click=lambda: a.attach(group))
  230. doc.reference(ui.scene)