Implementation:CARLA simulator Carla Python Commands Bindings
| Knowledge Sources | |
|---|---|
| Domains | Python Bindings, Batch Commands |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
Boost.Python binding file that exposes CARLA's batch command system to the Python API, enabling efficient bulk operations on actors.
Description
This file defines the export_commands() function that registers batch command classes within the libcarla.command submodule. It exposes SpawnActor (with chaining via then()), DestroyActor, ApplyVehicleControl, ApplyVehicleAckermannControl, ApplyWalkerControl, ApplyVehiclePhysicsControl, ApplyTransform, ApplyWalkerState, ApplyTargetVelocity, ApplyTargetAngularVelocity, ApplyImpulse, ApplyForce, ApplyAngularImpulse, ApplyTorque, SetSimulatePhysics, SetEnableGravity, SetAutopilot, ShowDebugTelemetry, and SetVehicleLightState. The Response class provides actor_id, error, and has_error() for checking batch results. A FutureActor sentinel value (0) is provided for referencing not-yet-spawned actors in chained commands. All command types are implicitly convertible to the base Command type.
Usage
These command classes are used with client.apply_batch() and client.apply_batch_sync() to perform multiple actor operations in a single server call, significantly reducing overhead when spawning or controlling many actors.
Code Reference
Source Location
- Repository: CARLA
- File: PythonAPI/carla/src/Commands.cpp
Signature
void export_commands();
// Key classes in libcarla.command submodule:
class_<cr::Command::SpawnActor>("SpawnActor")
class_<cr::Command::DestroyActor>("DestroyActor")
class_<cr::Command::ApplyVehicleControl>("ApplyVehicleControl")
class_<cr::Command::ApplyTransform>("ApplyTransform")
class_<cr::Command::SetAutopilot>("SetAutopilot")
class_<cr::CommandResponse>("Response", no_init)
Import
import carla
SpawnActor = carla.command.SpawnActor
DestroyActor = carla.command.DestroyActor
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| blueprint | carla.ActorBlueprint | Yes (SpawnActor) | Blueprint for actor to spawn |
| transform | carla.Transform | Yes (SpawnActor) | Spawn location and rotation |
| actor / actor_id | carla.Actor / int | Yes (most commands) | Target actor or actor ID |
| control | carla.VehicleControl | Yes (ApplyVehicleControl) | Control to apply |
| enabled | bool | Yes (SetAutopilot) | Whether to enable autopilot |
Outputs
| Name | Type | Description |
|---|---|---|
| response.actor_id | int | ID of spawned/affected actor (0 on error) |
| response.error | str | Error message if operation failed |
| response.has_error() | bool | Whether the command encountered an error |
Usage Examples
import carla
client = carla.Client('localhost', 2000)
world = client.get_world()
SpawnActor = carla.command.SpawnActor
SetAutopilot = carla.command.SetAutopilot
FutureActor = carla.command.FutureActor
bp = world.get_blueprint_library().find('vehicle.tesla.model3')
transforms = world.get_map().get_spawn_points()[:10]
# Batch spawn with autopilot chaining
batch = []
for t in transforms:
batch.append(SpawnActor(bp, t).then(SetAutopilot(FutureActor, True)))
results = client.apply_batch_sync(batch, True)
for result in results:
if not result.has_error():
print(f"Spawned actor {result.actor_id}")