Implementation:CARLA simulator Carla World Spawn Actor Attached
| Knowledge Sources | |
|---|---|
| Domains | Autonomous Driving Simulation, Robotics Perception |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for spawning a sensor actor attached to a parent vehicle or actor provided by the CARLA simulator.
Description
The World.spawn_actor method, when called with the attach_to parameter, creates a new actor and immediately establishes a parent-child attachment. The transform parameter is interpreted as a relative offset from the parent actor's local coordinate frame, defining the sensor's mounting position and orientation.
This is the standard method for instrumenting vehicles with sensors. The spawned sensor actor automatically moves with its parent, and its world-space position is computed by composing the parent's transform with the relative offset. The attachment_type parameter controls the mechanical coupling between parent and child (Rigid for fixed mounting, SpringArm for damped following).
Unlike try_spawn_actor, this method raises a RuntimeError if the spawn fails, making it appropriate for critical sensor attachments where failure should halt execution.
Usage
Use this method to attach sensors (cameras, LiDAR, radar, GNSS, IMU) to vehicles. Call it after spawning the parent vehicle and configuring the sensor blueprint. Always register a listen callback on the spawned sensor to receive data.
Code Reference
Source Location
- Repository: CARLA
- File:
LibCarla/source/carla/client/World.cpp - Lines: L122-128
- Python binding:
PythonAPI/carla/src/Sensor.cpp - Lines: L22-40
Signature
World.spawn_actor(
blueprint: carla.ActorBlueprint,
transform: carla.Transform,
attach_to: carla.Actor,
attachment_type: carla.AttachmentType = carla.AttachmentType.Rigid
) -> carla.Sensor
Import
import carla
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| blueprint | carla.ActorBlueprint | Yes | The sensor blueprint with configured attributes (resolution, field of view, channels, etc.) |
| transform | carla.Transform | Yes | Relative transform from the parent actor's local frame, defining the sensor mounting position and orientation |
| attach_to | carla.Actor | Yes | The parent actor (typically a vehicle) to which the sensor will be attached |
| attachment_type | carla.AttachmentType | No | Attachment mode: Rigid (fixed, default), SpringArm (damped following), or SpringArmGhost (damped, no collision). Defaults to Rigid. |
Outputs
| Name | Type | Description |
|---|---|---|
| return | carla.Sensor | The spawned sensor actor, attached to the parent. Call .listen(callback) to receive sensor data. |
Usage Examples
Basic Example
import carla
import numpy as np
client = carla.Client("localhost", 2000)
client.set_timeout(10.0)
world = client.get_world()
# Spawn ego vehicle
blueprint_library = world.get_blueprint_library()
vehicle_bp = blueprint_library.find("vehicle.tesla.model3")
spawn_point = world.get_map().get_spawn_points()[0]
ego_vehicle = world.spawn_actor(vehicle_bp, spawn_point)
# Configure an RGB camera
camera_bp = blueprint_library.find("sensor.camera.rgb")
camera_bp.set_attribute("image_size_x", "1920")
camera_bp.set_attribute("image_size_y", "1080")
camera_bp.set_attribute("fov", "90")
# Attach camera to the vehicle roof (relative transform)
camera_transform = carla.Transform(
carla.Location(x=1.5, y=0.0, z=2.4),
carla.Rotation(pitch=-15.0, yaw=0.0, roll=0.0)
)
camera_sensor = world.spawn_actor(
camera_bp,
camera_transform,
attach_to=ego_vehicle,
attachment_type=carla.AttachmentType.Rigid
)
# Register callback to process camera data
def process_image(image):
array = np.frombuffer(image.raw_data, dtype=np.uint8)
array = array.reshape((image.height, image.width, 4)) # BGRA
print(f"Frame {image.frame}: captured {image.width}x{image.height} image")
camera_sensor.listen(process_image)
Full Sensor Suite Example
import carla
client = carla.Client("localhost", 2000)
client.set_timeout(10.0)
world = client.get_world()
blueprint_library = world.get_blueprint_library()
# Spawn ego vehicle
vehicle_bp = blueprint_library.find("vehicle.lincoln.mkz_2020")
spawn_point = world.get_map().get_spawn_points()[0]
ego_vehicle = world.spawn_actor(vehicle_bp, spawn_point)
sensors = []
# Front RGB camera
cam_bp = blueprint_library.find("sensor.camera.rgb")
cam_bp.set_attribute("image_size_x", "1280")
cam_bp.set_attribute("image_size_y", "720")
cam_transform = carla.Transform(carla.Location(x=2.0, z=1.4))
front_camera = world.spawn_actor(cam_bp, cam_transform, attach_to=ego_vehicle)
front_camera.listen(lambda img: img.save_to_disk(f"output/front/{img.frame:06d}.png"))
sensors.append(front_camera)
# Roof LiDAR
lidar_bp = blueprint_library.find("sensor.lidar.ray_cast")
lidar_bp.set_attribute("channels", "64")
lidar_bp.set_attribute("points_per_second", "1200000")
lidar_bp.set_attribute("range", "100.0")
lidar_transform = carla.Transform(carla.Location(x=0.0, z=2.4))
lidar_sensor = world.spawn_actor(lidar_bp, lidar_transform, attach_to=ego_vehicle)
lidar_sensor.listen(lambda data: data.save_to_disk(f"output/lidar/{data.frame:06d}.ply"))
sensors.append(lidar_sensor)
# Front radar
radar_bp = blueprint_library.find("sensor.other.radar")
radar_bp.set_attribute("horizontal_fov", "30.0")
radar_bp.set_attribute("vertical_fov", "10.0")
radar_transform = carla.Transform(carla.Location(x=2.2, z=0.5))
radar_sensor = world.spawn_actor(radar_bp, radar_transform, attach_to=ego_vehicle)
radar_sensor.listen(lambda data: print(f"Radar detections: {len(data)}"))
sensors.append(radar_sensor)
# GNSS sensor
gnss_bp = blueprint_library.find("sensor.other.gnss")
gnss_transform = carla.Transform(carla.Location(z=2.4))
gnss_sensor = world.spawn_actor(gnss_bp, gnss_transform, attach_to=ego_vehicle)
gnss_sensor.listen(lambda data: print(f"GPS: lat={data.latitude:.6f}, lon={data.longitude:.6f}"))
sensors.append(gnss_sensor)
# IMU sensor
imu_bp = blueprint_library.find("sensor.other.imu")
imu_transform = carla.Transform() # Vehicle center
imu_sensor = world.spawn_actor(imu_bp, imu_transform, attach_to=ego_vehicle)
imu_sensor.listen(lambda data: print(f"IMU: accel=({data.accelerometer.x:.2f}, {data.accelerometer.y:.2f}, {data.accelerometer.z:.2f})"))
sensors.append(imu_sensor)
# Cleanup
# for sensor in sensors:
# sensor.stop()
# sensor.destroy()
# ego_vehicle.destroy()