Skip to content

Commit

Permalink
Merge pull request #95 from Carifio24/restore-camera-params
Browse files Browse the repository at this point in the history
Save and restore camera parameters
  • Loading branch information
Carifio24 authored May 4, 2024
2 parents 2cc3918 + 2580aad commit a16c371
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion glue_wwt/viewer/data_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

from __future__ import absolute_import, division, print_function

from astropy.coordinates import SkyCoord
import astropy.units as u
from glue.core.coordinates import WCSCoordinates
from glue.logger import logger
from pywwt import ViewerNotAvailableError

from .image_layer import WWTImageLayerArtist
from .table_layer import WWTTableLayerArtist
Expand All @@ -25,7 +29,6 @@ class WWTDataViewerBase(object):
}

_UPDATE_SETTINGS = [
"foreground", "background", "foreground_opacity", "galactic",
"equatorial_grid", "equatorial_grid_color", "equatorial_text",
"ecliptic_grid", "ecliptic_grid_color", "ecliptic_text",
"alt_az_grid", "alt_az_grid_color", "alt_az_text",
Expand All @@ -37,6 +40,8 @@ class WWTDataViewerBase(object):
"precession_chart_color"
]

_IMAGERY_UPDATE_SETTINGS = ["foreground", "background", "foreground_opacity", "galactic"]

def __init__(self):
self._initialize_wwt()
self._wwt.actual_planet_scale = True
Expand All @@ -55,6 +60,7 @@ def _update_wwt(self, force=False, **kwargs):
self._wwt.solar_system.cosmos = self.state.mode == 'Universe'
# Only show local stars when not in Universe or Milky Way mode
self._wwt.solar_system.stars = self.state.mode not in ['Universe', 'Milky Way']
force = True

if force or 'constellation_boundaries' in kwargs:
self._wwt.constellation_boundaries = self.state.constellation_boundaries != 'None'
Expand All @@ -65,6 +71,12 @@ def _update_wwt(self, force=False, **kwargs):
wwt_attr = self._GLUE_TO_WWT_ATTR_MAP.get(setting, setting)
setattr(self._wwt, wwt_attr, getattr(self.state, setting, None))

show_imagery = self.state.mode == 'Sky'
if show_imagery:
for setting in self._IMAGERY_UPDATE_SETTINGS:
if force or setting in kwargs:
setattr(self._wwt, setting, getattr(self.state, setting, None))

def get_layer_artist(self, cls, **kwargs):
"In this package, we must override to append the wwt_client argument."
return cls(self.state, wwt_client=self._wwt, **kwargs)
Expand All @@ -84,3 +96,34 @@ def get_data_layer_artist(self, layer=None, layer_state=None):
def get_subset_layer_artist(self, layer=None, layer_state=None):
# At some point maybe we'll use different classes for this?
return self.get_data_layer_artist(layer=layer, layer_state=layer_state)

def __gluestate__(self, context):
state = super(WWTDataViewerBase, self).__gluestate__(context)
try:
center = self._wwt.get_center()
camera = {
"ra": center.ra.deg,
"dec": center.dec.deg,
"fov": self._wwt.get_fov().value
}
if hasattr(self._wwt, 'get_roll'):
camera["roll"] = self._wwt.get_roll().value
state["camera"] = camera
except ViewerNotAvailableError:
logger.error("Unable to export camera parameters as WWT viewer is not responding.")
return state

@classmethod
def __setgluestate__(cls, rec, context):
viewer = super(WWTDataViewerBase, cls).__setgluestate__(rec, context)
if "camera" in rec:
camera = rec["camera"]
ra = camera.get("ra", 0)
dec = camera.get("dec", 0)
fov = camera.get("fov", 60)
roll = camera.get("roll", None)
camera_kwargs = dict(fov=fov * u.deg, instant=True)
if hasattr(viewer._wwt, 'get_roll') and roll is not None:
camera_kwargs["roll"] = roll * u.deg
viewer._wwt.center_on_coordinates(SkyCoord(ra, dec, unit=u.deg), **camera_kwargs)
return viewer

0 comments on commit a16c371

Please sign in to comment.