unisim.backend.drake.backend¶
UniSim adapter for the drake-uni batch runtime.
UniLab owns task logic, reset sampling, named sensor views, and training flow.
DrakeUni owns Drake model construction, batched stepping, and raw sensor
evaluation. This module translates the SimBackend contract into DrakeUni
runtime calls and keeps UniLab’s cached state/sensor views synchronized.
Functions
Report whether the DrakeUni batch extension can be used. |
Classes
UniLab |
- class unisim.backend.drake.backend.DrakeBackend[source]¶
Bases:
SimBackendUniLab
SimBackendimplementation backed by DrakeUni batch runtime.The backend keeps the public UniLab API stable while delegating model construction, integration, and raw sensor evaluation to DrakeUni.
- backend_type: str = 'drake'¶
- property model: _DrakeUniModelView¶
Underlying physics model.
- get_actuator_ctrl_range()[source]¶
Return actuator control ranges.
- Return type:
- Returns:
Array with shape
(num_actuators, 2)and columns[low, high].
- get_actuator_joint_names()[source]¶
Return each actuator’s target single-DoF joint in control-vector order.
Backends must fail closed when an actuator does not target exactly one hinge/slide joint. Manager action terms use this cold-path metadata to map community joint selectors onto the backend control vector without inspecting backend-private model objects.
- get_scene_layout()[source]¶
Return the immutable materialized public addresses, never native handles.
- Return type:
CompiledSceneLayout
- get_entity_default_state(entity, env_ids=None)[source]¶
Detached construction/keyframe defaults in selected environment order.
Fields and frames match get_entity_state. Defaults follow immutable variant assignment and never reflect current reset-time randomization. This query does not reset, step or reparse the scene.
- get_entity_state(entity)[source]¶
Return detached root_pose/root_velocity/joint_positions/joint_velocities.
Root fields follow EntityStatePatch’s link-origin/world-frame contract. Non-root joint fields use the frozen entity joint order and native generalized widths. Unavailable fields must fail, never return old data as if current. State freshness follows the adapter’s declared profile.
- get_joint_range(*, names=None)[source]¶
Return joint position limits, excluding the floating base.
- Parameters:
names (
Sequence[str] |None) – Optional joint names in requested return order. Adapters that support named scalar joints resolve the model mapping internally.- Return type:
- Returns:
Array with shape
(num_dof, 2)and columns[low, high], orNonewhen the backend does not expose limits.When
namesis provided by a supporting adapter, the result has shape(len(names), 2). Hinge limits are radians, slide limits are meters, and joints without enabled limits return infinities.
- get_keyframe_qpos(name)[source]¶
Return the full qpos for a named keyframe, including the floating base.
- get_default_qpos()[source]¶
Return the backend/model default qpos through a stable contract.
- Return type:
- get_default_dof_pos()[source]¶
Return default joint positions in canonical non-root qpos order.
The returned array is detached, one-dimensional, and excludes floating root coordinates. Backends whose DoF view is actuator-indexed must use that same actuator-target order here.
- Return type:
- get_init_qvel()[source]¶
Return a zero-initialized qvel vector compatible with
set_state_tensor.- Return type:
- Returns:
Zero-filled qvel array.
- get_body_ids(names)[source]¶
Resolve body/link names to backend integer IDs.
- Parameters:
- Return type:
- Returns:
int32array with shape(len(names),).- Raises:
ValueError – If any name is not found.
- get_motion_body_ids(names)[source]¶
Resolve body IDs used by motion datasets.
Motion datasets are generated from MuJoCo, so the returned ids follow the MJCF body order with
worldbodyas id 0, regardless of the backend-native indexing used byget_body_ids.
- get_joint_dof_indices(names)[source]¶
Resolve joint names to absolute DoF indices in velocity space.
- get_joint_dof_pos_indices(names)[source]¶
Resolve joint names to DoF indices in position space (qpos).
Only single-DoF joints are supported; free joints are excluded.
- get_joint_dof_vel_indices(names)[source]¶
Resolve joint names to joint-section DoF indices in velocity space.
Unlike
get_joint_dof_indices(), the returned indices exclude any root/free-joint velocity columns. They address the same joint section used byget_joint_dof_pos_indices().
- get_joint_state_qpos_indices(names)[source]¶
Resolve single-DoF joints to full
set_state_tensorqpos columns.Unlike
get_joint_dof_pos_indices(), these indices address the complete qpos vector accepted byset_state_tensor(), including any root coordinates. Manager reset transactions resolve them on the cold path.
- get_joint_state_qvel_indices(names)[source]¶
Resolve single-DoF joints to full
set_state_tensorqvel columns.
- reset_entities(request)[source]¶
Validate the complete selected-entity request, then commit it once.
Validation failure leaves all state unchanged. A native partial commit failure must fault the backend unless the adapter can actually roll back. Unselected entities/environments and fixed identity remain unchanged.
- Parameters:
request (
SceneResetRequest)- Return type:
- get_dr_capabilities()[source]¶
Return supported domain-randomization capabilities for this backend.
- Return type:
DomainRandomizationCapabilities
- apply_interval_randomization(plan)[source]¶
Apply a scheduled interval randomization plan.
Generic dispatch: each op yielded by
plan.iter_ops()is validated against the builtin term specs (custom terms pass through) and routed to the backend-owned handler table returned by_interval_term_handlers(). Op payloads are torch-first: Torch tensors orTensorIntervalPayloadvalues produced by the caller; host adapters convert tensors at their private boundary throughunisim.dr._host, and a missing producer-sampled payload fails closed. A term without a handler fails closed withNotImplementedErrornaming the backend class and the term. Backends that need per-plan prologue/epilogue semantics (for example clearing staged external forces before the ops accumulate) keep a thin override that calls this base implementation.- Parameters:
plan (
IntervalRandomizationPlan)- Return type:
- get_play_capabilities()[source]¶
Return backend-native play/render capabilities.
- Return type:
PlayCapabilities
- resolve_play_render_plan(*, play_render_mode, play_steps, output_video)[source]¶
Resolve high-level playback mode into backend-owned render parameters.
- run_playback(*, env, initialize, step, num_steps, output_video=None, render_spacing=None, render_offset_mode=None, headless=None, record_video=None, frame_state_getter=None, camera_kwargs=None, debug_overlay_getter=None, on_frame=None)[source]¶
Execute backend-owned playback for an env wrapper.
camera_kwargsis normalized intoCameraCfgat this boundary; unknown mapping keys fail closed with an error naming them.debug_overlay_getteris an optional per-frame callback returning a sequence with one entry per environment (len == num_envs); each entry is that env’s sequence ofDebugPrimitive(Noneor empty marks an env without overlay) and returningNonedisables overlays for the frame. Primitive poses are env-local; the renderer applies grid offsets when composing multiple envs. Backends whoseget_play_capabilities().supports_debug_overlayis False fail closed withNotImplementedErrorwhen this is notNone. On the interactive rendering path only backends whosesupports_interactive_debug_overlayis True consume it; the others fail closed withNotImplementedError.on_frameis an optional per-frame video hook called by offline render pipelines before encoding: it receives(frame_index, frame)with the frame an(H, W, 3)uint8 array, and returns a replacement frame of the same shape/dtype orNoneto keep the original. Backends rendering through a native (non-offline) renderer fail closed withNotImplementedErrorwhen this is notNone.Known boundary:
envis the owning env wrapper, not a physics-layer concept. Current playback implementations read env-level configuration (e.g.cfg.scene,cfg.ctrl_dt,cfg.render_spacing) and env-owned playback helpers (get_playback_model,get_physics_state_snapshot) that have no backend-native equivalent yet. The parameter stays on this contract until playback asset/config resolution moves onto backend-owned metadata; backends must only use it on the cold playback path.- Parameters:
env (Any)
initialize (Callable[[], Any])
step (Callable[[Any], Any])
num_steps (int | None)
output_video (str | PathLike[str] | None)
render_spacing (float | None)
render_offset_mode (str | None)
headless (bool | None)
record_video (bool | None)
frame_state_getter (Callable[[], np.ndarray] | None)
camera_kwargs (CameraCfg | Mapping[str, Any] | None)
debug_overlay_getter (DebugOverlayGetter | None)
on_frame (Callable[[int, np.ndarray], np.ndarray | None] | None)
- Return type:
str | None
- bind_renderer(spacing=1.0, *, offset_mode='grid', headless=False, capture=False, width=1280, height=720, camera_kwargs=None)[source]¶
Initialize a backend-native renderer.
headlesscontrols whether a native window is opened.capturecontrols whethercapture_video_frameis valid for the renderer.camera_kwargsis normalized intoCameraCfgat this boundary; unknown mapping keys fail closed with an error naming them.
- render()[source]¶
Render one frame through a backend-native interactive renderer.
- Raises:
RenderClosedError – If the user closed the render window.
- Return type:
- capture_video_frame()[source]¶
Capture one RGB frame through a backend-native renderer.
- Raises:
RenderClosedError – If the user closed the render window.
- Return type:
- get_physics_state_layout()[source]¶
Return the
[time, qpos, qvel]packet layout (no mocap bodies).- Return type:
PhysicsStateLayout
- get_physics_state()[source]¶
Return a physics snapshot suitable for offline playback/video export.
Rows use the
[time, qpos, qvel]layout; backends whose model has mocap bodies append[mocap_pos(nmocap*3), mocap_quat(nmocap*4)]so offline rendering can replay mocap-driven geometry at its recorded pose. Consumers must decode snapshots throughget_physics_state_layout().split_staterather than hardcoding column slices.- Return type:
- get_playback_model(env_index=None)[source]¶
Return the playback model for a specific env when variants exist.
- apply_body_force(body_ids, force, torque=None)[source]¶
Apply a world-frame force (and optional torque) to bodies for the upcoming step.
- Parameters:
body_ids (
ndarray) – Body ids whose external forces should be perturbed.force (
ndarray) – Force values with shape(num_envs, len(body_ids), 3).torque (
ndarray|None) – Optional world-frame torque values with the same shape. Backends without a torque channel must fail closed when this is notNone.
- Return type:
- Returns:
None. Backends that support this mutate their pending simulation state.
- get_tensor_execution()[source]¶
Declare Drake’s CPU-authoritative tensor execution profile.
- Return type:
TensorExecution
- get_tensor_capabilities()[source]¶
Return the deliberately narrow initial packed host-bridge profile.
- Return type:
TensorLifecycleCapabilities
- bind_host_bridge_io(spec)[source]¶
Compile persistent Drake staging and packed device layouts.
- Parameters:
spec (
TensorIOSpec)- Return type:
HostBridgeTransferPlan
- get_state_views(fields=None, device=None)[source]¶
Copy authoritative CPU state to an explicitly selected Torch device.
- get_sensor_view(name, device=None)[source]¶
Copy one authoritative CPU sensor block to a Torch device.
- set_state_tensor(env_indices, qpos, qvel, randomization=None)[source]¶
Bridge selected accelerator reset state to CPU Drake physics.