scene.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. from dataclasses import dataclass
  2. from typing import Callable, Dict, List, Optional, Union
  3. from .. import binding
  4. from ..dependencies import register_component
  5. from ..element import Element
  6. from ..events import SceneClickEventArguments, SceneClickHit, handle_event
  7. from ..globals import socket_id
  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 PointCloud as point_cloud
  40. from .scene_objects import QuadraticBezierTube as quadratic_bezier_tube
  41. from .scene_objects import Ring as ring
  42. from .scene_objects import Sphere as sphere
  43. from .scene_objects import SpotLight as spot_light
  44. from .scene_objects import Stl as stl
  45. from .scene_objects import Text as text
  46. from .scene_objects import Text3d as text3d
  47. from .scene_objects import Texture as texture
  48. def __init__(self, width: int = 400, height: int = 300, on_click: Optional[Callable] = None) -> None:
  49. """3D Scene
  50. Display a 3d scene using `three.js <https://threejs.org/>`_.
  51. Currently NiceGUI supports boxes, spheres, cylinders/cones, extrusions, straight lines, curves and textured meshes.
  52. Objects can be translated, rotated and displayed with different color, opacity or as wireframes.
  53. They can also be grouped to apply joint movements.
  54. :param width: width of the canvas
  55. :param height: height of the canvas
  56. :param on_click: callback to execute when a 3d object is clicked
  57. """
  58. super().__init__('scene')
  59. self._props['width'] = width
  60. self._props['height'] = height
  61. self.objects: Dict[str, Object3D] = {}
  62. self.stack: List[Union[Object3D, SceneObject]] = [SceneObject()]
  63. self.camera: SceneCamera = SceneCamera()
  64. self.on_click = on_click
  65. self.on('init', self.handle_init)
  66. self.on('click3d', self.handle_click)
  67. def handle_init(self, msg: Dict) -> None:
  68. with socket_id(msg['args']):
  69. self.move_camera(duration=0)
  70. for object in self.objects.values():
  71. object.send()
  72. def handle_click(self, msg: Dict) -> None:
  73. arguments = SceneClickEventArguments(
  74. sender=self,
  75. client=self.client,
  76. click_type=msg['args']['click_type'],
  77. button=msg['args']['button'],
  78. alt=msg['args']['alt_key'],
  79. ctrl=msg['args']['ctrl_key'],
  80. meta=msg['args']['meta_key'],
  81. shift=msg['args']['shift_key'],
  82. hits=[SceneClickHit(
  83. object_id=hit['object_id'],
  84. object_name=hit['object_name'],
  85. x=hit['point']['x'],
  86. y=hit['point']['y'],
  87. z=hit['point']['z'],
  88. ) for hit in msg['args']['hits']],
  89. )
  90. handle_event(self.on_click, arguments)
  91. def __len__(self) -> int:
  92. return len(self.objects)
  93. def move_camera(self,
  94. x: Optional[float] = None,
  95. y: Optional[float] = None,
  96. z: Optional[float] = None,
  97. look_at_x: Optional[float] = None,
  98. look_at_y: Optional[float] = None,
  99. look_at_z: Optional[float] = None,
  100. up_x: Optional[float] = None,
  101. up_y: Optional[float] = None,
  102. up_z: Optional[float] = None,
  103. duration: float = 0.5) -> None:
  104. self.camera.x = self.camera.x if x is None else x
  105. self.camera.y = self.camera.y if y is None else y
  106. self.camera.z = self.camera.z if z is None else z
  107. self.camera.look_at_x = self.camera.look_at_x if look_at_x is None else look_at_x
  108. self.camera.look_at_y = self.camera.look_at_y if look_at_y is None else look_at_y
  109. self.camera.look_at_z = self.camera.look_at_z if look_at_z is None else look_at_z
  110. self.camera.up_x = self.camera.up_x if up_x is None else up_x
  111. self.camera.up_y = self.camera.up_y if up_y is None else up_y
  112. self.camera.up_z = self.camera.up_z if up_z is None else up_z
  113. self.run_method('move_camera',
  114. self.camera.x, self.camera.y, self.camera.z,
  115. self.camera.look_at_x, self.camera.look_at_y, self.camera.look_at_z,
  116. self.camera.up_x, self.camera.up_y, self.camera.up_z, duration)
  117. def delete(self) -> None:
  118. binding.remove(list(self.objects.values()), Object3D)
  119. super().delete()