Skip to content

Commit

Permalink
Make OpenXR work with meetings. Made ChimeraX 1.6 and 1.7 and SteamVR…
Browse files Browse the repository at this point in the history
… and OpenXR interoperate in meetings in any combination. Ticket #9640
  • Loading branch information
tomgoddard committed Oct 24, 2023
1 parent bd0299b commit 1b3e7cf
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/bundles/graphics/src/gsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ def set_state_from_snapshot(view, session, data):

@staticmethod
def reset_state(view, session):
if not session.restore_options.get('restore camera'):
return
view.set_default_parameters()
from chimerax.surface.settings import settings as surf_settings
surf_settings.reset() # Reset clip cap settings
Expand Down
27 changes: 24 additions & 3 deletions src/bundles/meeting/src/meeting.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,10 @@ def send_scene(self):
def _encode_session(self):
from io import BytesIO
stream = BytesIO()
from chimerax.vive import xr
xr.save_camera_in_session(self._session, False)
self._session.save(stream, version=3, include_maps=True)
xr.save_camera_in_session(self._session, True)
from lz4.frame import compress
sbytes = compress(stream.getbuffer())
return sbytes
Expand All @@ -881,7 +884,8 @@ def _restore_session(self, session_bytes):
from io import BytesIO
stream = BytesIO(sbytes)
restore_camera = (ses.main_view.camera.name != 'vr')
ses.restore(stream, resize_window = False, restore_camera = restore_camera,
ses.restore(stream, resize_window = False,
restore_camera = restore_camera,
clear_log = False)
self._received_scene = True

Expand Down Expand Up @@ -1791,13 +1795,30 @@ def _hand_buttons_update(self, vr_camera):
h._meeting_button_modes = last_mode = {}
hm = []
for button, mode in h.button_modes.items():
if isinstance(button, str):
button = self._button_name_to_index(button)
if button is None:
continue
if mode != last_mode.get(button):
last_mode[button] = mode
hm.append((button, mode.name))
update = True
bu.append(hm)
return bu if update else None

def _button_name_to_index(self, name):
# Convert OpenXR button name to SteamVR button index
# for compatibility.
n2i = {
'trigger': 33,
'touchpad': 32,
'thumbstick': 32,
'grip': 2,
'menu': 1,
'A': 7,
}
return n2i.get(name, None)

def _gui_updates(self, vr_camera):
msg = {}
c = vr_camera
Expand Down Expand Up @@ -2065,8 +2086,8 @@ def update_image(self, encoded_rgba, session):

def _vr_camera(session):
c = session.main_view.camera
from chimerax.vive.vr import SteamVRCamera
return c if isinstance(c, SteamVRCamera) else None
from chimerax.vive import vr, xr
return c if isinstance(c, (vr.SteamVRCamera, xr.OpenXRCamera)) else None

def _place_matrix(p, encoding = 'float32 matrix'):
'''Encode Place as bytes for sending over socket.'''
Expand Down
3 changes: 3 additions & 0 deletions src/bundles/vive/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def get_class(class_name):
if class_name == 'SteamVRCamera':
from . import vr
return vr.SteamVRCamera
if class_name == 'OpenXRCamera':
from . import xr
return xr.OpenXRCamera
return None

bundle_api = _VRAPI()
6 changes: 5 additions & 1 deletion src/bundles/vive/src/vr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1146,11 +1146,15 @@ def restore_snapshot(cls, session, data):
session.logger.info(str(e))
return None

if not session.restore_options.get('restore camera'):
return c

c.room_to_scene = data['room_to_scene']
for hc, ba in zip(c._hand_controllers, data['button_assignments']):
hc.button_assignments = ba
if data['active']:
# Try to start VR if it was active when session saved.
# Try to start VR if it was active when session saved
# and we are not already using a VR camera.
def start_vr(trigger_name, session):
try:
vr(session, enable = True)
Expand Down
16 changes: 13 additions & 3 deletions src/bundles/vive/src/xr.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,19 @@ def vr_camera(session, create = True):
c = getattr(session, '_openxr_camera', None)
if c is None and create:
session._openxr_camera = c = OpenXRCamera(session)
session.add_state_manager('_openxr_camera', c) # For session saving
save_camera_in_session(session)
return c


# -----------------------------------------------------------------------------
#
def save_camera_in_session(session, save = True):
c = getattr(session, '_openxr_camera', None)
if c:
if save:
session.add_state_manager('_openxr_camera', c)
else:
session.remove_state_manager('_openxr_camera')

# -----------------------------------------------------------------------------
#
def stop_vr(session, simplify_graphics = True):
Expand Down Expand Up @@ -1055,7 +1065,7 @@ def start_vr(trigger_name, session):
except Exception as e:
# Failed to start VR.
session.logger.info(str(e))
from chimerax.core.triggerset import DEREGISTER
from chimerax.core.triggerset import DEREGISTER
return DEREGISTER
session.triggers.add_handler('end restore session', start_vr)
return c
Expand Down

0 comments on commit 1b3e7cf

Please sign in to comment.