Implementation:CARLA simulator Carla Python Sensor Bindings
| Knowledge Sources | |
|---|---|
| Domains | Python Bindings, Sensors |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
Boost.Python binding file that exposes the CARLA Sensor class hierarchy (Sensor, ServerSideSensor, ClientSideSensor, LaneInvasionSensor) to the Python API.
Description
This file defines the export_sensor() function that registers the sensor class hierarchy. The base Sensor class (inheriting from Actor) provides listen(callback) to subscribe to sensor data streams, is_listening property, and stop() to unsubscribe. The ServerSideSensor extends Sensor with G-buffer access (listen_to_gbuffer, is_listening_gbuffer, stop_gbuffer) and ROS integration methods (enable_for_ros, disable_for_ros, is_enabled_for_ros). ClientSideSensor and LaneInvasionSensor are exposed as subclasses of the client-side sensor hierarchy. The SubscribeToStream helper wraps the Python callback using MakeCallback for GIL-safe execution. SubscribeToGBuffer similarly wraps callbacks for G-buffer data subscriptions.
Usage
This binding enables Python scripts to attach sensors (cameras, LiDAR, radar, collision detectors, etc.) to actors and receive their data through callback functions.
Code Reference
Source Location
- Repository: CARLA
- File: PythonAPI/carla/src/Sensor.cpp
Signature
void export_sensor();
// Key classes exposed:
class_<cc::Sensor, bases<cc::Actor>>("Sensor", no_init)
class_<cc::ServerSideSensor, bases<cc::Sensor>>("ServerSideSensor", no_init)
class_<cc::ClientSideSensor, bases<cc::Sensor>>("ClientSideSensor", no_init)
class_<cc::LaneInvasionSensor, bases<cc::ClientSideSensor>>("LaneInvasionSensor", no_init)
Import
import carla
sensor = world.spawn_actor(sensor_bp, transform, attach_to=vehicle)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| callback | callable | Yes (for listen) | Python function to receive sensor data |
| gbuffer_id | int | Yes (for listen_to_gbuffer) | G-buffer texture identifier |
Outputs
| Name | Type | Description |
|---|---|---|
| is_listening | bool | Whether the sensor is currently streaming data |
| sensor_data | carla.SensorData | Data passed to callback (type depends on sensor) |
Usage Examples
import carla
client = carla.Client('localhost', 2000)
world = client.get_world()
bp_lib = world.get_blueprint_library()
# Spawn a camera sensor
camera_bp = bp_lib.find('sensor.camera.rgb')
camera_bp.set_attribute('image_size_x', '800')
camera_bp.set_attribute('image_size_y', '600')
camera = world.spawn_actor(camera_bp,
carla.Transform(carla.Location(x=-5, z=3)),
attach_to=vehicle)
# Subscribe to data
camera.listen(lambda image: image.save_to_disk('output/%06d.png' % image.frame))
# Check status
print(camera.is_listening)
# Stop and destroy
camera.stop()
camera.destroy()