How to output the random poses of the TurtleBot for every frame?

Background: I am working on a script that requires the TurtleBot to refresh its pose randomly in each of the next 50 frames, and I also want to print its pose for each frame.

Issue: While the TurtleBot can refresh its pose randomly without errors in the Script Editor, the system only prints and outputs the pose for the first frame. It doesn’t print or write the poses for the subsequent frames to a txt file. How can I resolve this issue?
Here are my code:
import omni.replicator.core as rep
from pxr import UsdGeom, Gf, Usd

Constants

WORKER_PRIM_PATH = “/World/turtlebot_tutorial_multi_sensor_publish_rates/turtlebot3_burger”
USD_SCENE_PATH = ‘/home/lenovo/JammingWong/FuhuaProject_9_6 (copy)/Turtlebot_Example.usd’
FILE_PATH = ‘/home/lenovo/JammingWong/Recorder/empty/position_orientation/turtlebot_positions_orientations.txt’
Counter = 1

Define the TurtleBot randomization function

def turtlebot():
global Counter

# Load the USD scene and get the prim
stage = Usd.Stage.Open(USD_SCENE_PATH)
turtlebot_prim = stage.GetPrimAtPath(WORKER_PRIM_PATH)
turtlebot_prim2 = rep.get.prim_at_path(WORKER_PRIM_PATH)

# Randomize position and rotation
with turtlebot_prim2:
    rep.modify.pose(
        position=rep.distribution.uniform((-1, -1, -0.75377), (1, 0, -0.75377)),
        rotation=rep.distribution.uniform((0, 0, -180), (0, 0, 180))
    )

# Extract the world transform matrix and get actual position and rotation
xformable = UsdGeom.Xformable(turtlebot_prim)
transform_matrix = xformable.ComputeLocalToWorldTransform(Usd.TimeCode.Default())
actual_rotation = transform_matrix.ExtractRotation()
actual_translation = transform_matrix.ExtractTranslation()

# Record the position and orientation to a file
with open(FILE_PATH, 'a') as f:
    f.write(f"Frame {Counter}:\n")
    f.write(f"Actual Position: {actual_translation}\n")
    f.write(f"Actual Rotation: {actual_rotation}\n")
    f.write("-" * 40 + "\n")

# Print debug information
print("Actual Position:", actual_translation)
print("Actual Rotation:", actual_rotation)
print("Counter =", Counter)

# Increment counter
Counter += 1

return turtlebot_prim2

Register the randomizer

rep.randomizer.register(turtlebot)

Trigger the randomization for 10,000 frames

with rep.trigger.on_frame(num_frames=50):
rep.randomizer.turtlebot()

You want to “print” them ? Meaning just output the poses in the console ?

I want to export the pose of the TurtleBot to a .txt file (”print“ is just for temporary testing in terminal).
I recently updated my code, but the original problem still hasn’t been solved."
import os
import omni
import omni.kit
import omni.replicator.core as rep
import omni.usd
from pxr import UsdGeom, Usd

from omni.isaac.synthetic_utils import SyntheticDataHelper

Constants

PRIM_PATH = “/World/turtlebot_tutorial_multi_sensor_publish_rates/turtlebot3_burger”
USD_SCENE_PATH = ‘/home/lenovo/.local/share/ov/pkg/isaac-sim-4.2.0/JammingWong/CusWriter_09_29 /Turtlebot_Example.usd’
FILE_PATH = ‘/home/lenovo/omni.replicator_out/turtlebot_positions_orientations.txt’
Counter = 1

Open the USD stage once at the beginning

stage = Usd.Stage.Open(USD_SCENE_PATH)

with rep.new_layer():

# Define the TurtleBot randomization function
def turtlebot():
    global Counter
    turtlebot_prim = rep.get.prims(semantics=[('class', 'bot')])
    
    # Randomize position and rotation
    with turtlebot_prim:
        rep.modify.pose(
            position=rep.distribution.uniform((-1, -1, -0.75377), (1, 0, -0.75377)),
            rotation=rep.distribution.uniform((0, 0, -180), (0, 0, 180))
        )
    # Return the prim for further processing
    return turtlebot_prim.node

# Register the randomizer
rep.randomizer.register(turtlebot)

# Function to extract and save the turtlebot's actual position and orientation
def save_actual_position_orientation(turtlebot_prim, frame_counter):
    # Original-------------------------------------------------------------------------------------------------------------------
    # Extract the world transform matrix and get actual position and rotation
    xformable = UsdGeom.Xformable(turtlebot_prim)
    transform_matrix = xformable.ComputeLocalToWorldTransform(Usd.TimeCode.Default())
    actual_rotation = transform_matrix.ExtractRotation()
    actual_translation = transform_matrix.ExtractTranslation()
    # Original-------------------------------------------------------------------------------------------------------------------


    # Record the position and orientation to a file
    with open(FILE_PATH, 'a') as f:
        f.write(f"Frame {frame_counter}:\n")
        f.write(f"Actual Position: {actual_translation}\n")
        f.write(f"Actual Rotation: {actual_rotation}\n")
        f.write("-" * 80 + "\n")

    # Print debug information
    print(f"Frame {frame_counter} Actual Position:", actual_translation)
    print(f"Frame {frame_counter} Actual Rotation:", actual_rotation)


# Create render products
rp1 = rep.create.render_product("/World/Camera_01", (1024, 1024))
rp2 = rep.create.render_product("/World/Camera_02", (1024, 1024))
rp3 = rep.create.render_product("/World/Camera_03", (1024, 1024))
rp4 = rep.create.render_product("/World/Camera_04", (1024, 1024))

# Trigger on each frame
with rep.trigger.on_frame(num_frames=10):

    print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=")
    # Apply randomization
    rep.randomizer.turtlebot()
    turtlebot_prim = stage.GetPrimAtPath(PRIM_PATH)
    # turtlebot_prim = rep.get.prims(semantics=[('class', 'bot')])
    # turtlebot_prim, _ = rep.randomizer.turtlebot()

    # Recompute transform and save the new position and rotation in every frame
    save_actual_position_orientation(turtlebot_prim, Counter)

    # Increment counter after saving
    Counter += 1

# Initialize and attach writer
writer = rep.WriterRegistry.get("BasicWriter")
writer.initialize(
    output_dir="out_dir_test_visualization",
    rgb=True,
    bounding_box_2d_loose=True,
    instance_segmentation=True,
    colorize_instance_segmentation=True,
    pointcloud=True,
)

writer.attach([rp1, rp2, rp3, rp4])

# Run the orchestrator
rep.orchestrator.run()

Ok, I am going to move this over to the Isaac Sim forum for better help.