chrisjcc commited on
Commit
1b7961a
·
verified ·
1 Parent(s): 3ebf482

TraySim module

Browse files
Files changed (1) hide show
  1. tray_sim.py +29 -0
tray_sim.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import mujoco
2
+ import numpy as np
3
+ import imageio
4
+ import os
5
+ import tempfile
6
+ from mujoco import viewer
7
+
8
+ MODEL_PATH = "assets/tray.xml"
9
+
10
+ def run_tray_simulation(seed=0):
11
+ np.random.seed(seed)
12
+ model = mujoco.MjModel.from_xml_path(MODEL_PATH)
13
+ data = mujoco.MjData(model)
14
+
15
+ # Randomize initial object position
16
+ data.qpos[:3] = [0.0, 0.0, 0.3 + 0.2 * np.random.rand()] # x, y, z
17
+ data.qvel[:] = 0
18
+
19
+ # Simulate
20
+ frames = []
21
+ for _ in range(200):
22
+ mujoco.mj_step(model, data)
23
+ img = mujoco.mj_render(model, data, width=480, height=480, camera=None)
24
+ frames.append(img)
25
+
26
+ # Save as GIF to temp file
27
+ gif_path = os.path.join(tempfile.gettempdir(), f"tray_sim_{seed}.gif")
28
+ imageio.mimsave(gif_path, frames, fps=20)
29
+ return gif_path