scene.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from dataclasses import dataclass
  2. from typing import Callable, Dict, List, Optional, Union
  3. from .. import globals
  4. from ..dependencies import register_component
  5. from ..element import Element
  6. from ..events import SceneClickEventArguments, SceneClickHit, handle_event
  7. from ..functions.lifecycle import on_connect
  8. from .scene_object3d import Object3D
  9. from .scene_objects import Scene as SceneObject
  10. register_component('scene', __file__, 'scene.js', [
  11. 'lib/three.min.js',
  12. 'lib/CSS2DRenderer.js',
  13. 'lib/CSS3DRenderer.js',
  14. 'lib/OrbitControls.js',
  15. 'lib/STLLoader.js',
  16. 'lib/tween.umd.min.js',
  17. ])
  18. @dataclass
  19. class SceneCamera:
  20. x: float = 0
  21. y: float = -3
  22. z: float = 5
  23. look_at_x: float = 0
  24. look_at_y: float = 0
  25. look_at_z: float = 0
  26. up_x: float = 0
  27. up_y: float = 0
  28. up_z: float = 1
  29. @dataclass
  30. class SceneObject:
  31. id: str = 'scene'
  32. class Scene(Element):
  33. from .scene_objects import Box as box
  34. from .scene_objects import Curve as curve
  35. from .scene_objects import Cylinder as cylinder
  36. from .scene_objects import Extrusion as extrusion
  37. from .scene_objects import Group as group
  38. from .scene_objects import Line as line
  39. from .scene_objects import QuadraticBezierTube as quadratic_bezier_tube
  40. from .scene_objects import Ring as ring
  41. from .scene_objects import Sphere as sphere
  42. from .scene_objects import SpotLight as spot_light
  43. from .scene_objects import Stl as stl
  44. from .scene_objects import Text as text
  45. from .scene_objects import Text3d as text3d
  46. from .scene_objects import Texture as texture
  47. def __init__(self, width: int = 400, height: int = 300, on_click: Optional[Callable] = None) -> None:
  48. """3D Scene
  49. Display a 3d scene using `three.js <https://threejs.org/>`_.
  50. Currently NiceGUI supports boxes, spheres, cylinders/cones, extrusions, straight lines, curves and textured meshes.
  51. Objects can be translated, rotated and displayed with different color, opacity or as wireframes.
  52. They can also be grouped to apply joint movements.
  53. :param width: width of the canvas
  54. :param height: height of the canvas
  55. :param on_click: callback to execute when a 3d object is clicked
  56. """
  57. super().__init__('scene')
  58. self._props['width'] = width
  59. self._props['height'] = height
  60. self.objects: Dict[str, Object3D] = {}
  61. self.stack: List[Union[Object3D, SceneObject]] = [SceneObject()]
  62. self.camera: SceneCamera = SceneCamera()
  63. self.on_click = on_click
  64. self.on('click3d', self.handle_click)
  65. on_connect(self.handle_connect)
  66. def handle_connect(self) -> None:
  67. with globals.current_socket():
  68. self.move_camera(duration=0)
  69. for object in self.objects.values():
  70. object.send()
  71. def handle_click(self, msg: Dict) -> None:
  72. arguments = SceneClickEventArguments(
  73. sender=self,
  74. client=self.client,
  75. click_type=msg['args']['click_type'],
  76. button=msg['args']['button'],
  77. alt=msg['args']['alt_key'],
  78. ctrl=msg['args']['ctrl_key'],
  79. meta=msg['args']['meta_key'],
  80. shift=msg['args']['shift_key'],
  81. hits=[SceneClickHit(
  82. object_id=hit['object_id'],
  83. object_name=hit['object_name'],
  84. x=hit['point']['x'],
  85. y=hit['point']['y'],
  86. z=hit['point']['z'],
  87. ) for hit in msg['args']['hits']],
  88. )
  89. handle_event(self.on_click, arguments)
  90. def __len__(self) -> int:
  91. return len(self.objects)
  92. def move_camera(self,
  93. x: Optional[float] = None,
  94. y: Optional[float] = None,
  95. z: Optional[float] = None,
  96. look_at_x: Optional[float] = None,
  97. look_at_y: Optional[float] = None,
  98. look_at_z: Optional[float] = None,
  99. up_x: Optional[float] = None,
  100. up_y: Optional[float] = None,
  101. up_z: Optional[float] = None,
  102. duration: float = 0.5) -> None:
  103. self.camera.x = self.camera.x if x is None else x
  104. self.camera.y = self.camera.y if y is None else y
  105. self.camera.z = self.camera.z if z is None else z
  106. self.camera.look_at_x = self.camera.look_at_x if look_at_x is None else look_at_x
  107. self.camera.look_at_y = self.camera.look_at_y if look_at_y is None else look_at_y
  108. self.camera.look_at_z = self.camera.look_at_z if look_at_z is None else look_at_z
  109. self.camera.up_x = self.camera.up_x if up_x is None else up_x
  110. self.camera.up_y = self.camera.up_y if up_y is None else up_y
  111. self.camera.up_z = self.camera.up_z if up_z is None else up_z
  112. self.run_method('move_camera',
  113. self.camera.x, self.camera.y, self.camera.z,
  114. self.camera.look_at_x, self.camera.look_at_y, self.camera.look_at_z,
  115. self.camera.up_x, self.camera.up_y, self.camera.up_z, duration)