"""Vendor-neutral Rerun blueprint for prepared observation recordings.""" from __future__ import annotations from collections.abc import Callable from contextlib import suppress from threading import Lock from typing import Any, Literal from uuid import UUID, uuid5 import rerun as rr import rerun_bindings as bindings from rerun import blueprint as rrb from k1link.viewer.recorded_blueprint_lifecycle import ( BlueprintSessionReleased, RecordedBlueprintSessions, ) from k1link.viewer.rerun_bridge import RerunSceneSettings APPLICATION_ID = "nodedc_mission_core_recorded" SESSION_TIMELINE = "session_time" RECORDED_SPATIAL_VIEW_ID = UUID("5f5f11d5-3b0a-4a81-887b-2be767cba1c0") RECORDED_SPATIAL_RESET_VIEW_ID = UUID("1480abcc-a0ae-4287-ab69-4b606d15d947") RECORDED_ROOT_CONTAINER_ID = UUID("b02f2aca-8471-4dcb-b786-53df5a320fc8") RECORDED_SPATIAL_RESET_ROOT_CONTAINER_ID = UUID("27d95ad7-1e72-46ca-b854-d02cd99b7610") RECORDED_PERCEPTION_ROOT_CONTAINER_ID = UUID("70ea9fd5-bbbb-4b23-8ae8-8098af92b997") RECORDED_PERCEPTION_RESET_ROOT_CONTAINER_ID = UUID("d2196c6e-99da-4402-857c-4daea0dd3ae0") RECORDED_PERCEPTION_3D_ROOT_CONTAINER_ID = UUID("81051015-9808-413b-90a4-1cfaacacbc4f") RECORDED_PERCEPTION_3D_RESET_ROOT_CONTAINER_ID = UUID("3de10822-0274-4a58-8d45-f35d86625303") RECORDED_METRICS_ROOT_CONTAINER_ID = UUID("374710a2-1d77-4349-bea4-8719e7aa1f09") RECORDED_METRICS_RESET_ROOT_CONTAINER_ID = UUID("1e8ac565-bbd6-4554-9213-dad564e534e3") RECORDED_POINTS_VISUALIZER_ID = UUID("ca037ec0-8761-4417-86ee-846fa2875303") RECORDED_PERCEPTION_POINTS_VISUALIZER_ID = UUID("ab8db306-6981-49c5-b417-94fe2f01dbf0") RECORDED_CAMERA_VIEW_ID = UUID("5c1db75b-07cd-479a-903d-f4f2ed554513") RECORDED_CAMERA_RESET_VIEW_ID = UUID("d0618e0c-c889-4222-a643-60a4a882935c") RECORDED_PERCEPTION_3D_VIEW_ID = UUID("0496bd2e-2b4d-4a4f-87b8-3ce4f9f7e114") RECORDED_PERCEPTION_3D_RESET_VIEW_ID = UUID("31f63ab8-ffdd-4751-ae40-e8e7b4462c8f") RECORDED_METRICS_VIEW_ID = UUID("f973fc11-0867-4732-ad3c-97008621fab7") RECORDED_UNIFIED_ROOT_CONTAINER_ID = UUID("e9934ef8-453f-432e-9136-2b0908190253") RECORDED_UNIFIED_RESET_ROOT_CONTAINER_ID = UUID("2781407d-9f4e-4405-86e8-bbe6065e83df") RecordedView = Literal["spatial", "perception", "perception3d", "metrics"] class RecordedBlueprintError(RuntimeError): """A viewer blueprint update could not be serialized safely.""" class _RecordedBlueprintStream: """Keep one bounded SDK blueprint source for each browser viewport. Upstream 0.36.3 activates a clone, not this source store. Explicit refresh makes layer changes visible. Ordinary layer updates deliberately omit eye components so the active clone retains the operator's camera. Only an explicit 3D/plan/reset transition may write a new spatial eye preset. """ def __init__( self, application_id: str, recording_id: str, *, blueprint_session_id: str, view_reset_generation: Literal[0, 1], ) -> None: self.blueprint_session_id = blueprint_session_id self.view_reset_generation = view_reset_generation self._lock = Lock() self._sequence = 0 self._eye_contract: tuple[bool, bool] | None = None self._closed = False native = bindings.new_blueprint( application_id=application_id, make_default=False, make_thread_default=False, default_enabled=True, ) self._blueprint_recording = rr.RecordingStream._from_native(native) self._blueprint_memory = self._blueprint_recording.memory_recording() self._transport_recording = rr.RecordingStream( application_id, recording_id=recording_id, send_properties=False, ) self._transport = rr.binary_stream(self._transport_recording) def render( self, blueprint_factory: Callable[[bool, bool, str], rrb.Blueprint], *, follow_trajectory: bool, plan_view: bool, reactivate_updates: bool = False, ) -> bytes: with self._lock: if self._closed: raise RecordedBlueprintError("stable blueprint stream is closed") eye_contract = (follow_trajectory, plan_view) update_eye_controls = self._eye_contract != eye_contract use_spatial_preset = ( self._eye_contract is not None and self._eye_contract[1] != plan_view ) # Initial admission/reset uses native framing. Explicit presets # apply to mode transitions, after the viewer has a source cursor. # Keep one view identity for the lifetime of this browser owner. # Replacing the UUID on every layer toggle rebuilt both native # viewports, delayed a simple visibility change, and discarded the # operator's active interaction state. The explicit reset # generation already owns the one intentional identity change. view_instance_token = self.blueprint_session_id blueprint = blueprint_factory( update_eye_controls, use_spatial_preset, view_instance_token, ) # Appending rows alone does not refresh upstream's active clone. # Reactivation makes layer changes visible; eye components remain # absent unless the explicit view-mode contract changed above. make_active = self._sequence == 0 or reactivate_updates self._blueprint_recording.set_time( "blueprint", sequence=self._sequence, ) self._sequence += 1 blueprint._log_to_stream(self._blueprint_recording) self._blueprint_recording.flush(timeout_sec=5.0) bindings.send_blueprint( self._blueprint_memory.storage, make_active, False, self._transport_recording.to_native(), ) payload = self._transport.read(flush=True, flush_timeout_sec=5.0) if not payload: raise RecordedBlueprintError("stable blueprint stream produced no data") self._eye_contract = eye_contract return payload def close(self) -> None: with self._lock: self._closed = True with suppress(Exception): self._blueprint_recording.disconnect() with suppress(Exception): self._transport_recording.disconnect() recorded_blueprint_sessions = RecordedBlueprintSessions[_RecordedBlueprintStream]() def recorded_blueprint( settings: RerunSceneSettings, *, display_point_bank: str | None = None, timeline: Literal["session_time", "stream_time"] = SESSION_TIMELINE, include_initial_playback_state: bool = True, active_view: RecordedView = "spatial", view_reset_generation: Literal[0, 1] = 0, unified_perception: bool = False, unified_camera_share: float = 0.46, semantic_layer: Literal["city", "vegetation"] | None = None, plan_view: bool = False, show_detections_2d: bool = False, show_camera_image: bool = True, show_segmentation: bool = False, show_cuboids_3d: bool = False, show_costmap: bool = False, follow_trajectory: bool = False, update_eye_controls: bool = True, explicit_spatial_preset: bool = False, eye_position: tuple[float, float, float] | None = None, eye_look_target: tuple[float, float, float] | None = None, eye_up: tuple[float, float, float] | None = None, view_instance_token: str | None = None, ) -> rrb.Blueprint: accumulation = max(0.0, settings.accumulation_seconds) time_ranges: list[rr.VisibleTimeRange] | None = None if accumulation > 0: time_ranges = [ rr.VisibleTimeRange( timeline, start=rr.TimeRangeBoundary.cursor_relative(seconds=-accumulation), end=rr.TimeRangeBoundary.cursor_relative(), ) ] accumulated_time_ranges = ( rrb.VisibleTimeRanges(time_ranges) if time_ranges is not None else None ) point_visualizer = rr.Points3D.from_fields( radii=rr.Radius.ui_points(settings.point_size), ).visualizer() point_visualizer.id = RECORDED_POINTS_VISUALIZER_ID perception_point_visualizer = rr.Points3D.from_fields( radii=rr.Radius.ui_points(settings.point_size), ).visualizer() perception_point_visualizer.id = RECORDED_PERCEPTION_POINTS_VISUALIZER_ID point_overrides: list[Any] = [ rrb.EntityBehavior(visible=settings.show_points), point_visualizer, ] trajectory_overrides: list[Any] = [ rrb.EntityBehavior(visible=settings.show_trajectory), ] if accumulated_time_ranges is not None: point_overrides.append(accumulated_time_ranges) trajectory_overrides.append(accumulated_time_ranges) selected_semantic_path = ( f"/perception/camera/segmentation/{semantic_layer}" if semantic_layer is not None else "/perception/camera/segmentation" ) spatial_eye_controls = ( rrb.EyeControls3D.from_fields( kind=rrb.Eye3DKind.Orbital, position=eye_position, look_target=eye_look_target, eye_up=eye_up, tracking_entity="/world/sensor_pose" if follow_trajectory else "", ) if eye_position is not None and eye_look_target is not None and eye_up is not None else rrb.EyeControls3D.from_fields( kind=rrb.Eye3DKind.Orbital, position=[0.0, 0.0, 30.0], look_target=[0.0, 0.0, 0.0], eye_up=[0.0, 1.0, 0.0], tracking_entity="/world/sensor_pose" if follow_trajectory else "", ) if plan_view and update_eye_controls else rrb.EyeControls3D.from_fields( kind=rrb.Eye3DKind.Orbital if follow_trajectory else None, position=[16.0, -16.0, 18.0] if explicit_spatial_preset else None, look_target=[0.0, 0.0, 0.0] if explicit_spatial_preset else None, eye_up=[0.0, 0.0, 1.0] if explicit_spatial_preset else None, tracking_entity="/world/sensor_pose" if follow_trajectory else "", ) if update_eye_controls else None ) spatial_view = rrb.Spatial3DView( origin="/world", name="Мир · LiDAR и объекты" if unified_perception else "Пространственная сцена", contents=["+ /world/**", "- /world/display_points/**"] + ( ["- /world/points", f"+ /world/display_points/{display_point_bank}"] if display_point_bank is not None else []), background=[7, 8, 10, 255], line_grid=rrb.LineGrid3D( visible=settings.show_grid, color=[86, 91, 99, 110], stroke_width=0.75, ), overrides={ # Accumulation belongs to mapping data only. Dynamic perception # inherits the view's latest-at query and can never turn into an # object-history trail when the operator widens the cloud window. "/world/points": point_overrides, **({f"/world/display_points/{display_point_bank}": point_overrides} if display_point_bank is not None else {}), "/world/costmap": rrb.EntityBehavior(visible=show_costmap), "/world/trajectory": trajectory_overrides, "/world/perception": rrb.EntityBehavior(visible=show_cuboids_3d), "/world/perception/lidar": rrb.EntityBehavior(visible=False), "/world/perception/support": rrb.EntityBehavior(visible=False), "/world/perception/semantic_points": rrb.EntityBehavior(visible=False), "/world/perception/boxes3d": rrb.EntityBehavior( visible=show_cuboids_3d, ), }, # Log an explicit empty range set so a previous accumulated blueprint # for this stable view id is cleared instead of surviving in Rerun. time_ranges=rrb.VisibleTimeRanges([]), # Follow and plan configure the native eye, not the point coordinates. # An empty tracking path clears tracking. Retaining this source view id # alone does not preserve edits in upstream's activated blueprint clone. eye_controls=spatial_eye_controls, ) def instance_id(primary: UUID, reset: UUID) -> UUID: base = reset if view_reset_generation else primary return uuid5(base, view_instance_token) if view_instance_token is not None else base spatial_view.id = instance_id( RECORDED_SPATIAL_VIEW_ID, RECORDED_SPATIAL_RESET_VIEW_ID, ) camera_view = rrb.Spatial2DView( origin="/perception/camera", name="Оригинальное видео · слои AI", background=[7, 8, 10, 255], overrides={ "/perception/camera/image": rrb.EntityBehavior(visible=show_camera_image), "/perception/camera/detections": rrb.EntityBehavior( visible=show_detections_2d, ), "/perception/camera/segmentation": rrb.EntityBehavior( visible=show_segmentation, ), "/perception/camera/segmentation/city": rrb.EntityBehavior( visible=show_segmentation and selected_semantic_path.endswith("/city"), ), "/perception/camera/segmentation/vegetation": rrb.EntityBehavior( visible=show_segmentation and selected_semantic_path.endswith("/vegetation"), ), }, ) camera_view.id = instance_id( RECORDED_CAMERA_VIEW_ID, RECORDED_CAMERA_RESET_VIEW_ID, ) perception_3d_view = rrb.Spatial3DView( # Cuboids are expressed in the same calibrated world frame as the # native LiDAR recording. Rooting this view at /world/perception # excluded /world/points and left the operator looking at boxes in an # empty scene. Keep the main point cloud and the derived objects in # one latest-at world view; unlike the mapping tab, this deliberately # has no accumulated time range, so dynamic cuboids never stack. origin="/world", name="Сегментация и объекты · 3D", background=[7, 8, 10, 255], line_grid=rrb.LineGrid3D( visible=settings.show_grid, color=[86, 91, 99, 110], stroke_width=0.75, ), overrides={ "/world/points": [ rrb.EntityBehavior(visible=settings.show_points), perception_point_visualizer, ], "/world/trajectory": rrb.EntityBehavior( visible=settings.show_trajectory, ), "/world/perception": rrb.EntityBehavior(visible=True), "/world/costmap": rrb.EntityBehavior(visible=show_costmap), # The overlay already carries the selected fusion support points; # its grey diagnostic LiDAR copy would otherwise double-render the # native cloud from /world/points. "/world/perception/lidar": rrb.EntityBehavior(visible=False), }, eye_controls=spatial_eye_controls, ) perception_3d_view.id = instance_id( RECORDED_PERCEPTION_3D_VIEW_ID, RECORDED_PERCEPTION_3D_RESET_VIEW_ID, ) metrics_view = rrb.TimeSeriesView( origin="/metrics/device", name="Маршрут и время", ) metrics_view.id = RECORDED_METRICS_VIEW_ID root_container: Any if unified_perception: # Operator perception is one synchronized composition, not a set of # mutually exclusive modes. The camera keeps the original frame as # its base and overlays 2D detections/segmentation. The paired world # view shows the same cursor in the native LiDAR frame and reveals # semantic points and cuboids independently. spatial_view.visualizer_overrides["/world/perception"] = [ rrb.EntityBehavior(visible=True), ] spatial_view.visualizer_overrides["/world/perception/lidar"] = [ rrb.EntityBehavior(visible=False), ] spatial_view.visualizer_overrides["/world/perception/support"] = [ rrb.EntityBehavior(visible=False), ] spatial_view.visualizer_overrides["/world/perception/semantic_points"] = [ rrb.EntityBehavior(visible=show_segmentation), ] spatial_view.visualizer_overrides["/world/perception/boxes3d"] = [ rrb.EntityBehavior(visible=show_cuboids_3d), ] camera_share = min(0.9, max(0.1, unified_camera_share)) root_container = rrb.Horizontal( camera_view, spatial_view, column_shares=[camera_share, 1.0 - camera_share], name="Единая сцена восприятия", ) root_container.id = instance_id( RECORDED_UNIFIED_ROOT_CONTAINER_ID, RECORDED_UNIFIED_RESET_ROOT_CONTAINER_ID, ) else: # Keep operator video and 3D cuboids as direct root views. Rerun's nested # Tabs preserve their own active child and cannot be switched reliably by # a live blueprint channel after the operator has visited another child. active_tab = {"spatial": 0, "perception": 1, "perception3d": 2, "metrics": 3}[active_view] root_container = rrb.Tabs( spatial_view, camera_view, perception_3d_view, metrics_view, active_tab=active_tab, ) # Rerun persists the active child on a Tabs container and does not reliably # replace it from a later blueprint message. Give every operator mode (and # its explicit reset generation) a stable root identity so the requested # child is authoritative instead of inheriting a previously visited tab. base_root_id = { ("spatial", 0): RECORDED_ROOT_CONTAINER_ID, ("spatial", 1): RECORDED_SPATIAL_RESET_ROOT_CONTAINER_ID, ("perception", 0): RECORDED_PERCEPTION_ROOT_CONTAINER_ID, ("perception", 1): RECORDED_PERCEPTION_RESET_ROOT_CONTAINER_ID, ("perception3d", 0): RECORDED_PERCEPTION_3D_ROOT_CONTAINER_ID, ("perception3d", 1): RECORDED_PERCEPTION_3D_RESET_ROOT_CONTAINER_ID, ("metrics", 0): RECORDED_METRICS_ROOT_CONTAINER_ID, ("metrics", 1): RECORDED_METRICS_RESET_ROOT_CONTAINER_ID, }[(active_view, view_reset_generation)] root_container.id = ( uuid5(base_root_id, view_instance_token) if view_instance_token is not None else base_root_id ) if timeline == "stream_time": root_container = spatial_view if include_initial_playback_state: return rrb.Blueprint( root_container, rrb.TimePanel( timeline=timeline, play_state="following" if timeline == "stream_time" else "paused", state="hidden", ), auto_layout=False, auto_views=False, collapse_panels=True, ) return rrb.Blueprint( root_container, auto_layout=False, auto_views=False, collapse_panels=False, ) def recorded_blueprint_rrd( settings: RerunSceneSettings, *, display_point_bank: str | None = None, timeline: Literal["session_time", "stream_time"] = SESSION_TIMELINE, application_id: str = APPLICATION_ID, recording_id: str, blueprint_session_id: str | None = None, active_view: RecordedView = "spatial", view_reset_generation: Literal[0, 1] = 0, unified_perception: bool = False, unified_camera_share: float = 0.46, semantic_layer: Literal["city", "vegetation"] | None = None, plan_view: bool = False, show_detections_2d: bool = False, show_camera_image: bool = True, show_segmentation: bool = False, show_cuboids_3d: bool = False, show_costmap: bool = False, follow_trajectory: bool = False, reactivate_updates: bool = False, initialize_playback: bool = True, eye_position: tuple[float, float, float] | None = None, eye_look_target: tuple[float, float, float] | None = None, eye_up: tuple[float, float, float] | None = None, ) -> bytes: """Serialize a bounded active blueprint update without recorded data.""" def build_blueprint( update_eye_controls: bool, use_spatial_preset: bool = False, view_instance_token: str | None = None, ) -> rrb.Blueprint: return recorded_blueprint( settings, display_point_bank=display_point_bank, timeline=timeline, include_initial_playback_state=timeline == "stream_time" and initialize_playback, active_view=active_view, view_reset_generation=view_reset_generation, unified_perception=unified_perception, unified_camera_share=unified_camera_share, semantic_layer=semantic_layer, plan_view=plan_view, show_detections_2d=show_detections_2d, show_camera_image=show_camera_image, show_segmentation=show_segmentation, show_cuboids_3d=show_cuboids_3d, show_costmap=show_costmap, follow_trajectory=follow_trajectory, update_eye_controls=update_eye_controls, explicit_spatial_preset=use_spatial_preset and update_eye_controls, eye_position=eye_position, eye_look_target=eye_look_target, eye_up=eye_up, view_instance_token=view_instance_token, ) payload: bytes | None if blueprint_session_id is not None: try: payload = recorded_blueprint_sessions.use( (application_id, recording_id, blueprint_session_id), view_reset_generation, lambda: _RecordedBlueprintStream( application_id, recording_id, blueprint_session_id=blueprint_session_id, view_reset_generation=view_reset_generation, ), lambda stream: stream.render( build_blueprint, follow_trajectory=follow_trajectory, plan_view=plan_view, reactivate_updates=reactivate_updates, ), ) except (RecordedBlueprintError, BlueprintSessionReleased): raise except Exception as exc: raise RecordedBlueprintError("failed to serialize stable recorded blueprint") from exc else: blueprint = build_blueprint(True) recording = rr.RecordingStream( application_id, recording_id=recording_id, send_properties=False, ) stream = rr.binary_stream(recording) try: recording.send_blueprint( blueprint, make_active=True, make_default=False, ) payload = stream.read(flush=True, flush_timeout_sec=5.0) except Exception as exc: raise RecordedBlueprintError("failed to serialize recorded blueprint") from exc finally: with suppress(Exception): recording.disconnect() if not payload or not payload.startswith(b"RRF2") or len(payload) > 1_048_576: raise RecordedBlueprintError("serialized recorded blueprint is invalid") return payload