|
1 | 1 | import io |
2 | 2 | import json |
3 | 3 | import zipfile |
| 4 | +from enum import IntEnum |
4 | 5 |
|
5 | 6 | from trame_client.widgets.core import AbstractElement |
6 | 7 |
|
@@ -1130,3 +1131,60 @@ def set_camera(self, camera): |
1130 | 1131 | @property |
1131 | 1132 | def ref_name(self): |
1132 | 1133 | return self._ref |
| 1134 | + |
| 1135 | + |
| 1136 | +class VtkWebXRHelper(HtmlElement): |
| 1137 | + """ |
| 1138 | + The VtkWebXRHelper component provides an easy way to add WebXR support to |
| 1139 | + a VtkView or a VtkLocalView. Just add this widget in a VtkView or |
| 1140 | + VtkLocalView widget and start an XR session with `start_xr()`. |
| 1141 | + This widget will fire the `enterXR` and `exitXR` events when an XR session |
| 1142 | + is effectively entered and exited. |
| 1143 | +
|
| 1144 | + >>> webxr_helper = vtk.VtkWebXRHelper( |
| 1145 | + ... ref=..., # Identifier for this component |
| 1146 | + ... draw_controllers_ray=True, # Enable XR controllers rays |
| 1147 | + ... ) |
| 1148 | + """ |
| 1149 | + |
| 1150 | + _next_id = 0 |
| 1151 | + |
| 1152 | + class XrSessionTypes(IntEnum): |
| 1153 | + """ |
| 1154 | + WebXR Session Types supported by vtk.js |
| 1155 | + """ |
| 1156 | + |
| 1157 | + HmdVR = 0 |
| 1158 | + MobileAR = 1 |
| 1159 | + LookingGlassVR = 2 |
| 1160 | + HmdAR = 3 |
| 1161 | + |
| 1162 | + def __init__(self, ref=None, **kwargs): |
| 1163 | + super().__init__( |
| 1164 | + "vtk-webXR-helper", |
| 1165 | + **kwargs, |
| 1166 | + ) |
| 1167 | + |
| 1168 | + if ref is None: |
| 1169 | + VtkWebXRHelper._next_id += 1 |
| 1170 | + ref = f"trame__webxr_helper_{VtkWebXRHelper._next_id}" |
| 1171 | + |
| 1172 | + self.__ref = ref |
| 1173 | + self._attributes["ref"] = f'ref="{ref}"' |
| 1174 | + self._attr_names += [("draw_controllers_ray", "drawControllersRay")] |
| 1175 | + self._event_names += [ |
| 1176 | + ("enter_xr", "enterXR"), |
| 1177 | + ("exit_xr", "exitXR"), |
| 1178 | + ] |
| 1179 | + |
| 1180 | + def start_xr(self, session_type: XrSessionTypes): |
| 1181 | + """ |
| 1182 | + Start a WebXR session |
| 1183 | + """ |
| 1184 | + self.server.js_call(self.__ref, "startXR", session_type) |
| 1185 | + |
| 1186 | + def stop_xr(self): |
| 1187 | + """ |
| 1188 | + Stop the current WebXR session |
| 1189 | + """ |
| 1190 | + self.server.js_call(self.__ref, "stopXR") |
0 commit comments