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.

../_images/randomized-scenario.gif

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:

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.

Randomize camera and environment
# 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.