Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:ARISE Initiative Robosuite MujocoEnv Close

From Leeroopedia

Metadata

Overview

Concrete method for closing robosuite environments and releasing simulation resources provided by the MujocoEnv base class.

Description

The close() method performs cleanup by destroying the viewer and simulation objects. Observations returned by step() are OrderedDicts with keys registered via the Observable system. Standard observation keys include robot0_proprio-state, object-state, and optional camera image arrays.

Usage

Call env.close() when finished with an environment to free MuJoCo and rendering resources.

Code Reference

  • Source: robosuite
  • File: robosuite/environments/base.py
  • Lines: L784-787
  • Signature:
def close(self):
    """Do any cleanup necessary here."""
  • Import: via robosuite.make() then env.close()

I/O Contract

Inputs

  • None (called on environment instance)

Outputs

  • None (side effect: destroys viewer and sim)

Usage Examples

Basic Environment Lifecycle

import robosuite as suite
from robosuite.wrappers import GymWrapper

# Create environment
env = suite.make(
    env_name="Lift",
    robots="Panda",
    has_renderer=False,
    has_offscreen_renderer=False,
    use_camera_obs=False,
)

# Run a single episode
obs = env.reset()

# Inspect observation structure
print("Observation keys:", obs.keys())
print("Robot proprioception shape:", obs["robot0_proprio-state"].shape)
print("Object state shape:", obs["object-state"].shape)

# Execute episode
for i in range(100):
    action = env.action_space.sample()
    obs, reward, done, info = env.step(action)

    if done:
        break

# Properly close environment
env.close()

Observation Inspection Pattern

import robosuite as suite
import numpy as np

# Create environment with camera observations
env = suite.make(
    env_name="Stack",
    robots="Panda",
    has_renderer=False,
    has_offscreen_renderer=True,
    use_camera_obs=True,
    camera_names="agentview",
)

obs = env.reset()

# Detailed observation inspection
print("\n=== Observation Space Analysis ===")
for key, value in obs.items():
    if isinstance(value, np.ndarray):
        print(f"{key}:")
        print(f"  Shape: {value.shape}")
        print(f"  Dtype: {value.dtype}")
        print(f"  Range: [{value.min():.3f}, {value.max():.3f}]")
    else:
        print(f"{key}: {type(value)}")

# Always cleanup
env.close()

Resource Management with Context Manager Pattern

import robosuite as suite
from contextlib import contextmanager

@contextmanager
def robosuite_env(**kwargs):
    """Context manager for automatic environment cleanup."""
    env = suite.make(**kwargs)
    try:
        yield env
    finally:
        env.close()

# Usage
with robosuite_env(env_name="Door", robots="Sawyer") as env:
    obs = env.reset()
    print("Available observations:", list(obs.keys()))

    # Environment automatically closed when exiting context

Related Pages

Page Connections

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