Randomize scenarios¶
Note
Both this guide and Create time series scenarios use the concept of keyframes to achieve randomization and time series data repsectively.
While manual overrides to environment parameters are possible, they may require careful tuning.
To get started quickly, we recommend randomizing from a list of environment presets instead.

If you’ve created a base scenario and want to make it diverse and varied, you can randomize the camera and environment across frames.
Let’s take the same scenario from Scatter assets randomly, and make it more diverse.
In that example, we defined a SpawnZone
that has force_respawn=True
, which already gives us
some nice asset randomization:
Recall: Base scenario with randomly scattered assets
from bbi import *
world = World("lake")
# Set the camera to the desired position
world.camera.set_transform(
Transform(
position=Vector3(0, 0, 10),
rotation=Vector3(0, 3, 175),
),
)
# Use the picker to select SpawnZone boundary points
poly = Polygon(
node_id="my_poly",
points=[
Vector3(-47.4, 34.95, 0),
Vector3(-19.95, 16.03, 0),
Vector3(-21.03, -12.97, 0),
Vector3(-70.03, -50.12, 0),
Vector3(-115.83, -39.97, 0),
Vector3(-85.21, 22.47, 0),
],
)
world.add(poly)
zone = SpawnZone(
node_id="my_zone",
asset_names=[
"Ivory MT",
"Grimmershorn",
"USCG Auxiliary Boat",
"CoastGuard Ship Singapore B",
"CoastGuard Ship Singapore A",
],
quantity=15,
geometry=poly,
asset_type=Asset, # or BuoyantAsset
yaw_range=(0, 360),
asset_weights=[1, 1, 1, 1, 1],
force_respawn=True,
)
world.add(zone)
To increase diversity, we’ll add keyframes to the world.camera
to vary its field of view, position,
and rotation, and to the world.env
, to vary its preset.
# Function to return randomized camera parameters
def rand_camera():
return {
"fov": rng.uniform(50, 90),
"position": Vector3(0, 0, rng.uniform(3, 15)),
"rotation": Vector3(rng.uniform(-2, 2), rng.uniform(-15, 15), rng.uniform(-5, 5)),
}
# Function to return randomized env presets
def rand_preset() -> dict:
return {
"preset_id": rng.choice([
"clear_sky_a",
"clear_sky_b",
"clear_sky_c",
"cloudy_a",
"cloudy_b",
"cloudy_c",
"foggy_a",
"foggy_b",
"foggy_c",
"overcast_a",
"overcast_b",
"overcast_c",
"partly_cloudy_a",
"partly_cloudy_b",
"partly_cloudy_c",
"rainy_a",
"rainy_b",
])
}
scenario = world.new_scenario(num_frames=10)
for t in range(scenario.num_frames):
scenario.set_keyframe(world.camera, t, **rand_camera())
scenario.set_keyframe(world.env, t, **rand_preset())
scenario.preview_animation() # To preview the 10 frames (optional)
scenario.render() # To render the 10 frames
Tip
Use the appropriate random methods to generate values for each parameter.
For continuous values, use
rng.uniform(a, b)
.For discrete values, use
rng.randint(a, b)
.For boolean values, use
rng.random() > 0.5
.For selecting from a sequence, use
rng.choice(sequence)
.
See Python’s random
documentation for details.