scene_objects.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. from __future__ import annotations
  2. from typing import List, Optional
  3. import numpy as np
  4. from justpy import WebPage
  5. from .scene_object3d import Object3D
  6. class Scene(Object3D):
  7. def __init__(self, view):
  8. super().__init__('scene', view)
  9. class Group(Object3D):
  10. def __init__(self):
  11. super().__init__('group')
  12. class Box(Object3D):
  13. def __init__(self,
  14. width: float = 1.0,
  15. height: float = 1.0,
  16. depth: float = 1.0,
  17. wireframe: bool = False,
  18. ):
  19. super().__init__('box', width, height, depth, wireframe)
  20. class Sphere(Object3D):
  21. def __init__(self,
  22. radius: float = 1.0,
  23. width_segments: int = 32,
  24. height_segments: int = 16,
  25. wireframe: bool = False,
  26. ):
  27. super().__init__('sphere', radius, width_segments, height_segments, wireframe)
  28. class Cylinder(Object3D):
  29. def __init__(self,
  30. top_radius: float = 1.0,
  31. bottom_radius: float = 1.0,
  32. height: float = 1.0,
  33. radial_segments: int = 8,
  34. height_segments: int = 1,
  35. wireframe: bool = False,
  36. ):
  37. super().__init__('cylinder', top_radius, bottom_radius, height, radial_segments, height_segments, wireframe)
  38. class Ring(Object3D):
  39. def __init__(self,
  40. inner_radius: float = 0.5,
  41. outer_radius: float = 1.0,
  42. theta_segments: int = 8,
  43. phi_segments: int = 1,
  44. theta_start: float = 0,
  45. theta_length: float = 2 * np.pi,
  46. wireframe: bool = False,
  47. ):
  48. super().__init__('ring',
  49. inner_radius, outer_radius, theta_segments, phi_segments, theta_start, theta_length, wireframe)
  50. class QuadraticBezierTube(Object3D):
  51. def __init__(self,
  52. start: List(float, float, float),
  53. mid: List(float, float, float),
  54. end: List(float, float, float),
  55. tubular_segments: int = 64,
  56. radius: float = 1.0,
  57. radial_segments: int = 8,
  58. closed: bool = False,
  59. wireframe: bool = False,
  60. ):
  61. super().__init__('quadratic_bezier_tube',
  62. start, mid, end, tubular_segments, radius, radial_segments, closed, wireframe)
  63. class Extrusion(Object3D):
  64. def __init__(self,
  65. outline: List[List[float, float]],
  66. height: float,
  67. wireframe: bool = False,
  68. ):
  69. super().__init__('extrusion', outline, height, wireframe)
  70. class Stl(Object3D):
  71. def __init__(self,
  72. url: str,
  73. wireframe: bool = False,
  74. ):
  75. super().__init__('stl', url, wireframe)
  76. class Line(Object3D):
  77. def __init__(self,
  78. start: List[float, float, float],
  79. end: List[float, float, float],
  80. ):
  81. super().__init__('line', start, end)
  82. class Curve(Object3D):
  83. def __init__(self,
  84. start: List[float, float, float],
  85. control1: List[float, float, float],
  86. control2: List[float, float, float],
  87. end: List[float, float, float],
  88. num_points: int = 20,
  89. ):
  90. super().__init__('curve', start, control1, control2, end, num_points)
  91. class Text(Object3D):
  92. def __init__(self,
  93. text: str,
  94. style: str = '',
  95. ):
  96. super().__init__('text', text, style)
  97. class Texture(Object3D):
  98. def __init__(self,
  99. url: str,
  100. coordinates: List[List[Optional[List[float]]]],
  101. ):
  102. super().__init__('texture', url, coordinates)
  103. async def set_url(self, url: str):
  104. self.args[0] = url
  105. for socket in WebPage.sockets.get(self.page.page_id, {}).values():
  106. await self.view.run_method(f'set_texture_url("{self.id}", "{url}")', socket)
  107. async def set_coordinates(self, coordinates: List[List[Optional[List[float]]]]):
  108. self.args[1] = coordinates
  109. for socket in WebPage.sockets.get(self.page.page_id, {}).values():
  110. await self.view.run_method(f'set_texture_coordinates("{self.id}", {coordinates})', socket)
  111. class SpotLight(Object3D):
  112. def __init__(self,
  113. color: str = '#ffffff',
  114. intensity: float = 1.0,
  115. distance: float = 0.0,
  116. angle: float = np.pi / 3,
  117. penumbra: float = 0.0,
  118. decay: float = 1.0,
  119. ):
  120. super().__init__('spot_light', color, intensity, distance, angle, penumbra, decay)