Implementation:CARLA simulator Carla Traffic Generation Script
| Knowledge Sources | |
|---|---|
| Domains | Simulation, Recording |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for generating diverse traffic scenarios suitable for recording provided by the CARLA simulator.
Description
The generate_traffic.py script is the primary example tool for populating a CARLA world with autonomous vehicles and pedestrians. It spawns a configurable number of vehicles (with autopilot via the Traffic Manager) and walkers (with AI controllers navigating the sidewalk mesh), producing realistic multi-agent traffic that generates meaningful recorded data. The script handles the full lifecycle of spawning, running, and cleaning up actors.
The script supports a wide range of configuration options including the number of vehicles and walkers, the proportion of vehicles set to dangerous behavior, filtering by specific vehicle or walker blueprints, and setting a random seed for reproducibility. It also supports synchronous mode operation, which is important for deterministic recordings.
When combined with the recording API, this script provides a turnkey solution for generating recorded traffic scenarios: start the recorder, run the traffic generation script for a desired duration, then stop the recorder to produce a complete recording file.
Usage
Use this script or its patterns when you need to populate a CARLA simulation with traffic for recording purposes. It is particularly useful for creating standardized benchmark recordings, generating diverse training data, or producing scenarios that exercise the full range of traffic interactions. The script can be run standalone or its patterns can be incorporated into custom recording workflows.
Code Reference
Source Location
- Repository: CARLA
- File:
PythonAPI/examples/generate_traffic.py:L43-327
Signature
# Command-line script invocation
python generate_traffic.py [options]
# Key arguments:
# -n, --number-of-vehicles Number of vehicles (default: 30)
# -w, --number-of-walkers Number of walkers (default: 10)
# --safe Avoid spawning vehicles prone to accidents
# -s, --seed Random seed for reproducibility
# --tm-port Traffic Manager port (default: 8000)
# --sync Enable synchronous mode
Import
import carla
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| --number-of-vehicles | int | No | Number of vehicles to spawn with autopilot. Defaults to 30. |
| --number-of-walkers | int | No | Number of pedestrian walkers to spawn with AI navigation. Defaults to 10. |
| --safe | flag | No | When set, filters out vehicle blueprints prone to erratic behavior (e.g., bicycles, motorcycles). |
| --seed | int | No | Random seed for deterministic actor spawning and behavior. Critical for reproducible recordings. |
| --sync | flag | No | Enables synchronous mode. Recommended for recordings to ensure deterministic frame timing. |
| --host | str | No | CARLA server hostname. Defaults to localhost. |
| --port | int | No | CARLA server port. Defaults to 2000. |
Outputs
| Name | Type | Description |
|---|---|---|
| spawned actors | side effect | Vehicles and walkers are spawned into the CARLA world and begin autonomous behavior. |
| console output | str | Status messages indicating how many vehicles and walkers were successfully spawned. |
Usage Examples
Basic Example
import carla
import time
client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.load_world('Town03')
# Start recording before generating traffic
client.start_recorder("/home/carla/recordings/traffic_scenario_01.log", additional_data=True)
# Spawn vehicles with autopilot
blueprint_library = world.get_blueprint_library()
vehicle_blueprints = blueprint_library.filter('vehicle.*')
spawn_points = world.get_map().get_spawn_points()
vehicles = []
for i in range(30):
bp = vehicle_blueprints[i % len(vehicle_blueprints)]
spawn_point = spawn_points[i % len(spawn_points)]
vehicle = world.try_spawn_actor(bp, spawn_point)
if vehicle is not None:
vehicle.set_autopilot(True)
vehicles.append(vehicle)
print(f"Spawned {len(vehicles)} vehicles")
# Let the simulation run for 60 seconds
time.sleep(60)
# Cleanup
client.stop_recorder()
for v in vehicles:
v.destroy()
print("Recording complete")
Synchronous Mode with Walkers
import carla
import random
client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.load_world('Town05')
# Enable synchronous mode for deterministic recording
settings = world.get_settings()
settings.synchronous_mode = True
settings.fixed_delta_seconds = 0.05
world.apply_settings(settings)
# Start recording
client.start_recorder("town05_sync_traffic.log")
# Spawn vehicles
blueprint_library = world.get_blueprint_library()
spawn_points = world.get_map().get_spawn_points()
random.shuffle(spawn_points)
vehicles = []
for i, sp in enumerate(spawn_points[:50]):
bp = random.choice(blueprint_library.filter('vehicle.*'))
vehicle = world.try_spawn_actor(bp, sp)
if vehicle:
vehicle.set_autopilot(True)
vehicles.append(vehicle)
# Spawn walkers with AI controllers
walker_blueprints = blueprint_library.filter('walker.pedestrian.*')
walker_controller_bp = blueprint_library.find('controller.ai.walker')
walkers = []
controllers = []
for _ in range(20):
spawn_point = carla.Transform()
loc = world.get_random_location_from_navigation()
if loc is not None:
spawn_point.location = loc
walker_bp = random.choice(walker_blueprints)
walker = world.try_spawn_actor(walker_bp, spawn_point)
if walker:
controller = world.spawn_actor(walker_controller_bp, carla.Transform(), walker)
walkers.append(walker)
controllers.append(controller)
world.tick() # Required to initialize controllers
# Start walker controllers
for controller in controllers:
controller.start()
controller.go_to_location(world.get_random_location_from_navigation())
# Run for 1000 ticks (50 seconds at 0.05s per tick)
for tick in range(1000):
world.tick()
# Cleanup
client.stop_recorder()
for c in controllers:
c.stop()
c.destroy()
for w in walkers:
w.destroy()
for v in vehicles:
v.destroy()
settings.synchronous_mode = False
world.apply_settings(settings)