First and Second Randomization Identical When Using Replicator with Simulation

Hi everyone, I just started building an SDG pipeline with Isaac Sim and Replicator. I ran into an issue where, with the following script, the first and second images would always have identical colors on the cube. The randomization works fine for the consecutive frames.

This only happens when I use the Replicator with simulation. If I comment out the line “run_simulation(60)” in the for loop, the colors will be properly randomized. However, for my use case, I need to run physics simulation after randomizing the scene and before data capture.

I am using Isaac Sim 4.5.0. Any help is appreciated!

from isaacsim import SimulationApp
simulation_app = SimulationApp({"headless": False})

import omni
import numpy as np
import omni.replicator.core as rep
from isaacsim.core.api import World
from pxr import UsdPhysics, PhysxSchema
from omni.physx.scripts import utils
import omni.timeline
from scipy.spatial.transform import Rotation
from pathlib import Path

world = World(stage_units_in_meters=0.001, physics_prim_path="/PhysicsScene")

# add light
rep.create.light(light_type="dome", intensity=1000, color=[1, 1, 1])

# add cube
cube = rep.create.cube(scale=[0.1, 0.1, 0.1])

def randomize_color():
    with cube:
        rep.randomizer.color(colors=rep.distribution.uniform((0, 0, 0), (1, 1, 1)))
    return cube.node

rep.randomizer.register(randomize_color)
with rep.trigger.on_custom_event("randomize_cube"):
    rep.randomizer.randomize_color()


def run_simulation(steps):
    print("Simulation started")
    world.play()
    for _ in range(steps):
        world.step()
    world.pause()
    print("Simulation paused")

rep.orchestrator.set_capture_on_play(False)
camera = rep.create.camera(position=[0, 0, 800], look_at=[0, 0, 0], focal_length=60) # focal legnth = x * 0.1 world unit
                                                                                     # world unit = 1 mm

rp = rep.create.render_product(camera, (1280, 720))
writer = rep.writers.get("BasicWriter")
out_dir = "test_output"
writer.initialize(output_dir=out_dir, rgb=True)
writer.attach(rp)


for _ in range(5):
    rep.utils.send_og_event("randomize_cube")
    run_simulation(60)
    rep.orchestrator.step(delta_time=0)
    print("Orchesctrator step")

writer.detach()
rp.destroy()
rep.orchestrator.wait_until_complete()

while simulation_app.is_running():
    simulation_app.update()


simulation_app.close() # close Isaac Sim