import sapien import numpy # TODO import gymnasium.spaces # TODO from tensorspecs import BoxSpec, TensorTableSpec class JointStateRef: """ TODO """ # TODO JointStateRef(..., include=("joint.positions", )) def __init__(self, joints: list[sapien.physx.PhysxArticulationJoint]): self._joints = joints @property def spec(self): joints = self._joints # TODO !!!! TensorTableSpec({ ("joint.positions", joint.name): BoxSpec( "dof", bounds=(joint.limits[..., 0], joint.limits[..., 1]), shape={"dof": joint.dof}, ) for joint in joints }) return gymnasium.spaces.Dict({ "joint.positions": gymnasium.spaces.Dict({ joint.name: gymnasium.spaces.Box( low=joint.limits[..., 0], high=joint.limits[..., 1], shape=(joint.dof, ), ) for joint in joints }), "joint.velocities": gymnasium.spaces.Dict({ joint.name: gymnasium.spaces.Box( low=-numpy.inf, high=numpy.inf, shape=(joint.dof, ), ) for joint in joints }), }) @property def current(self): return { "joint.positions": { joint.name: joint.drive_target for joint in self._joints }, "joint.velocities": { joint.name: joint.drive_velocity_target for joint in self._joints }, } # TODO typing @current.setter def current(self, value: dict): for joint in self._joints: # TODO better syntax!!!! if "joint.positions" in value and joint.name in value["joint.positions"]: joint.drive_target = value["joint.positions"][joint.name] if "joint.velocities" in value and joint.name in value["joint.velocities"]: joint.drive_velocity_target = value["joint.velocities"][joint.name] class CameraStateRef: # TODO CameraStateRef(..., channels=("R", "G", "B", "D")) def __init__( self, cameras: list[sapien.render.RenderCameraComponent], # channels: tuple[str, ...] = ("R", "G", "B"), ): self._cameras = cameras @property def spec(self): return gymnasium.spaces.Dict({ camera.name: gymnasium.spaces.Box( low=0., high=1., # TODO channels shape=(camera.height, camera.width, 3), dtype=numpy.float_, ) for camera in self._cameras }) @property def current(self): def _todo_take_get_picture(camera): # TODO camera.get_picture_names camera.take_picture() return camera.get_picture("Color") return { # TODO camera.get_picture_names camera.name: _todo_take_get_picture(camera)[..., :3] # TODO !!!!! for camera in self._cameras } # TODO import io import numpy import IPython.display import sapien from PIL import Image as PILImage # TODO mv upstream; display logic should be in camera.image, which should be a Stream obj class TODOCamera: def __init__(self, _todo_camera_entity: sapien.render.RenderCameraComponent): self._update_callbacks = set() self._entity = _todo_camera_entity @property def image(self): self._entity.take_picture() return PILImage.fromarray( numpy.array( # self._entity.get_picture("Color")[..., [0, 1, 2]] * 255, self._entity.get_picture("Color") * 255, dtype=numpy.uint8, ) ) def step(self): # TODO for callback in self._update_callbacks: callback() # TODO FIXME lifecycle!!! weakref??? def _ipython_display_(self): display_handle = IPython.display.display(display_id=True) def update_display_callback(display_handle=display_handle): buf = io.BytesIO() # self.image.save(buf, format="JPEG") self.image.save(buf, format="webp") display_handle.update( IPython.display.Image( buf.getvalue(), embed=True, ) ) update_display_callback() self._update_callbacks.add(update_display_callback)