The SimulationScene class allows you to initialize a simulation from real trajectory data and step through it to track agent motion and metrics.
To use it, you must first create a UnifiedDataset pointing to your local data directories, retrieve a Scene object using get_scene(), and then instantiate a SimulationScene.
Key parameters for SimulationScene:
env_name: A string identifier for the environment.scene_name: A string identifier for the scene.scene: The Scene object retrieved from the dataset.dataset: The UnifiedDataset instance.init_timestep: The starting timestep for the simulation.freeze_agents: A boolean indicating whether to freeze agent states.
After calling .reset(), you can iterate through the scene length using .step(new_xyh_dict), where new_xyh_dict is a dictionary mapping agent names to their next state (typically [x, y, heading]).
from typing import Dict
import numpy as np
from trajdata import AgentBatch, UnifiedDataset
from trajdata.data_structures.scene_metadata import Scene
from trajdata.simulation import SimulationScene
# 1. Setup Dataset
dataset = UnifiedDataset(
desired_data=["nusc_mini"],
data_dirs={
"nusc_mini": "~/datasets/nuScenes",
},
)
# 2. Get a specific scene
desired_scene: Scene = dataset.get_scene(scene_idx=0)
# 3. Initialize Simulation
sim_scene = SimulationScene(
env_name="nusc_mini_sim",
scene_name="sim_scene",
scene=desired_scene,
dataset=dataset,
init_timestep=0,
freeze_agents=True,
)
# 4. Run Simulation Loop
obs: AgentBatch = sim_scene.reset()
for t in range(1, sim_scene.scene.length_timesteps):
new_xyh_dict: Dict[str, np.ndarray] = dict()
# Define next states for agents
for idx, agent_name in enumerate(obs.agent_name):
curr_yaw = obs.curr_agent_state[idx, -1]
curr_pos = obs.curr_agent_state[idx, :2]
next_state = np.zeros((3,))
next_state[:2] = curr_pos
next_state[2] = curr_yaw
new_xyh_dict[agent_name] = next_state
# Step the simulation
obs = sim_scene.step(new_xyh_dict)