1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use super::{Actor, ActorBase, ActorBlueprint, Sensor, Vehicle, World};
use crate::{
error::{ResourceError, ResourceType, Result, ValidationError},
geom::Transform,
};
use carla_sys::carla::rpc::AttachmentType;
/// The builder is used to construct an actor with customized options.
#[derive(Debug)]
pub struct ActorBuilder<'a> {
world: &'a mut World,
blueprint: ActorBlueprint,
}
impl<'a> ActorBuilder<'a> {
pub fn new(world: &'a mut World, key: &str) -> Result<Self> {
let lib = world.blueprint_library()?;
let blueprint = lib.find(key)?.ok_or_else(|| ResourceError::NotFound {
resource_type: ResourceType::Blueprint,
identifier: key.to_string(),
context: Some(format!(
"Available blueprints:\n{}",
lib.iter()
.map(|bp| format!("- {}", bp.id()))
.collect::<Vec<_>>()
.join("\n")
)),
})?;
Ok(Self { world, blueprint })
}
pub fn set_attribute(mut self, id: &str, value: &str) -> Result<Self> {
let ok = self.blueprint.set_attribute(id, value);
if !ok {
return Err(ValidationError::InvalidConfiguration {
setting: format!("blueprint attribute '{}'", id),
reason: format!(
"Unable to set attribute '{}' to value '{}'. \
The attribute may not exist or the value may be invalid.",
id, value
),
}
.into());
}
Ok(self)
}
/// Spawns an actor at the given transform.
///
/// # Arguments
///
/// * `transform` - Initial position and rotation for the actor
///
/// # Returns
///
/// The spawned actor, or an error if spawning failed.
///
/// # Examples
///
/// ```no_run
/// # use carla::client::Client;
/// # use carla::geom::Transform;
/// let client = Client::default();
/// let mut world = client.world();
/// let transform = Transform::default();
/// let actor = world
/// .actor_builder("vehicle.tesla.model3")?
/// .spawn(transform)?;
/// # Ok::<(), carla::error::CarlaError>(())
/// ```
pub fn spawn(self, transform: Transform) -> Result<Actor> {
self.spawn_opt::<Actor, _>(transform, None, None)
}
pub fn spawn_opt<A, T>(
self,
transform: Transform,
parent: Option<&A>,
attachment_type: T,
) -> Result<Actor>
where
A: ActorBase,
T: Into<Option<AttachmentType>>,
{
self.world
.spawn_actor_opt(&self.blueprint, &transform, parent, attachment_type)
}
/// Spawns a vehicle at the given transform.
///
/// # Arguments
///
/// * `transform` - Initial position and rotation for the vehicle
///
/// # Returns
///
/// The spawned vehicle, or an error if spawning failed or the blueprint is not a vehicle.
///
/// # Examples
///
/// ```no_run
/// # use carla::client::Client;
/// # use carla::geom::Transform;
/// let client = Client::default();
/// let mut world = client.world();
/// let transform = Transform::default();
/// let vehicle = world
/// .actor_builder("vehicle.tesla.model3")?
/// .spawn_vehicle(transform)?;
/// # Ok::<(), carla::error::CarlaError>(())
/// ```
pub fn spawn_vehicle(self, transform: Transform) -> Result<Vehicle> {
self.spawn_vehicle_opt::<Actor, _>(transform, None, None)
}
pub fn spawn_vehicle_opt<A, T>(
self,
transform: Transform,
parent: Option<&A>,
attachment_type: T,
) -> Result<Vehicle>
where
A: ActorBase,
T: Into<Option<AttachmentType>>,
{
let id = self.blueprint.id();
self.spawn_opt(transform, parent, attachment_type)?
.try_into()
.map_err(|_| {
ValidationError::InvalidConfiguration {
setting: "blueprint type".to_string(),
reason: format!(
"Blueprint '{}' is not a vehicle. Use spawn() for non-vehicle actors.",
id
),
}
.into()
})
}
/// Spawns a sensor at the given transform.
///
/// # Arguments
///
/// * `transform` - Initial position and rotation for the sensor
///
/// # Returns
///
/// The spawned sensor, or an error if spawning failed or the blueprint is not a sensor.
///
/// # Examples
///
/// ```no_run
/// # use carla::client::Client;
/// # use carla::geom::Transform;
/// let client = Client::default();
/// let mut world = client.world();
/// let transform = Transform::default();
/// let sensor = world
/// .actor_builder("sensor.camera.rgb")?
/// .spawn_sensor(transform)?;
/// # Ok::<(), carla::error::CarlaError>(())
/// ```
pub fn spawn_sensor(self, transform: Transform) -> Result<Sensor> {
self.spawn_sensor_opt::<Actor, _>(transform, None, None)
}
pub fn spawn_sensor_opt<A, T>(
self,
transform: Transform,
parent: Option<&A>,
attachment_type: T,
) -> Result<Sensor>
where
A: ActorBase,
T: Into<Option<AttachmentType>>,
{
let id = self.blueprint.id();
self.spawn_opt(transform, parent, attachment_type)?
.try_into()
.map_err(|_| {
ValidationError::InvalidConfiguration {
setting: "blueprint type".to_string(),
reason: format!(
"Blueprint '{}' is not a sensor. Use spawn() for non-sensor actors.",
id
),
}
.into()
})
}
pub fn blueprint(&self) -> &ActorBlueprint {
&self.blueprint
}
pub fn blueprint_mut(&mut self) -> &mut ActorBlueprint {
&mut self.blueprint
}
pub fn into_blueprint(self) -> ActorBlueprint {
self.blueprint
}
}