Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Farama Foundation Gymnasium Vector Rendering Wrappers

From Leeroopedia
Revision as of 12:38, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Farama_Foundation_Gymnasium_Vector_Rendering_Wrappers.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Reinforcement_Learning, Wrappers
Last Updated 2026-02-15 03:00 GMT

Overview

Vector environment rendering wrappers that provide human-mode display and video recording for vectorized environments, merging sub-environment frames into a single composite view.

Description

This module provides two rendering wrappers for vector environments:

HumanRendering:

  • Adds "human" render mode support to vector environments that only support "rgb_array" or "depth_array" render modes.
  • Merges rendered frames from all sub-environments into a single window using Pygame.
  • Automatically computes the optimal grid layout (rows x columns) to fit sub-environments into the screen, maintaining aspect ratio.
  • Scales sub-environment renders to fit within the specified screen_size.
  • Requires pygame and opencv-python packages.

RecordVideo:

  • Records video of vector environment rollouts, concatenating frames from all sub-environments into a single frame.
  • Supports episode-triggered and step-triggered recording with configurable schedule functions.
  • Finds the optimal frame concatenation layout based on the video_aspect_ratio parameter.
  • Supports record_first_only mode to record only the first sub-environment.
  • Handles both "next-step" and "same-step" autoreset modes.
  • Supports configurable video length, name prefix, FPS, and garbage collection triggers.
  • Uses MoviePy for video encoding to MP4 format.
  • Requires the moviepy package.

Usage

Use HumanRendering for real-time visualization of vectorized environments during debugging or demonstration. Use RecordVideo for capturing training or evaluation videos for analysis, logging, or sharing.

Code Reference

Source Location

Signature

class HumanRendering(VectorWrapper, gym.utils.RecordConstructorArgs):
    def __init__(self, env: VectorEnv, screen_size: tuple[int, int] | None = None): ...

class RecordVideo(gym.vector.VectorWrapper, gym.utils.RecordConstructorArgs):
    def __init__(
        self,
        env: gym.vector.VectorEnv,
        video_folder: str,
        video_aspect_ratio: tuple[int, int] = (1, 1),
        record_first_only: bool = False,
        episode_trigger: Callable[[int], bool] | None = None,
        step_trigger: Callable[[int], bool] | None = None,
        video_length: int = 0,
        name_prefix: str = "rl-video",
        fps: int | None = None,
        disable_logger: bool = True,
        gc_trigger: Callable[[int], bool] | None = lambda episode: True,
    ): ...

Import

from gymnasium.wrappers.vector import HumanRendering, RecordVideo

I/O Contract

Inputs (HumanRendering)

Name Type Required Description
env VectorEnv Yes The vector environment to wrap (must have rgb_array or depth_array render mode)
screen_size tuple[int, int] or None No Screen size for the display window (default: uses sub-env render size)

Inputs (RecordVideo)

Name Type Required Description
env VectorEnv Yes The vector environment to wrap (must have image-based render mode)
video_folder str Yes Directory for saving video recordings
video_aspect_ratio tuple[int, int] No Desired aspect ratio for concatenated frame (default (1, 1))
record_first_only bool No If True, only record the first sub-environment (default False)
episode_trigger Callable or None No Function to determine if recording should start at a given episode
step_trigger Callable or None No Function to determine if recording should start at a given step
video_length int No Max video length in frames; 0 for full episodes (default 0)
name_prefix str No Prefix for video filenames (default "rl-video")
fps int or None No Video frame rate (default: from metadata or 30)
disable_logger bool No Disable MoviePy logging (default True)
gc_trigger Callable or None No When to trigger garbage collection (default: every episode)

Outputs

Name Type Description
observation ObsType Unchanged observations from the vector environment
rewards ArrayType Unchanged rewards from the vector environment
terminations ArrayType Unchanged termination flags
truncations ArrayType Unchanged truncation flags
info dict Unchanged info from the vector environment

Usage Examples

import gymnasium as gym
from gymnasium.wrappers.vector import RecordVideo

# Record video every 5 episodes
envs = gym.make_vec("CartPole-v1", num_envs=5, render_mode="rgb_array")
envs = RecordVideo(
    envs,
    video_folder="save_videos",
    video_aspect_ratio=(1, 1),
    episode_trigger=lambda t: t % 5 == 0,
)
_ = envs.reset(seed=123)
_ = envs.action_space.seed(123)
for i in range(200):
    actions = envs.action_space.sample()
    _ = envs.step(actions)
envs.close()

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment