How to obtain Camera Position Information when Randomly Placed

Hi, experts

I want to obtain the translate and rotate information each time a camera is randomly placed.
I thought that by writing such processing inside rep.trigger.on_frame, the above could be achieved, but it didn’t work.
I tried the following code:

import omni.usd
import omni.replicator.core as rep

stage = omni.usd.get_context().get_stage()
translate = "xformOp:translate"
rotation = "xformOp:rotateXYZ"
prim_path = "/Replicator/Camera_Xform"

camera_positions = [(0, 3, 0.344), (1, 3, 0.344), (2, 3, 0.344), (3, 3, 0.344)]
cube = rep.create.cube()
camera = rep.create.camera(position = (0,3,0.344),look_at=(0,0,0.344))


with rep.trigger.on_frame(num_frames=len(camera_positions)):
    with camera:
        rep.modify.pose(position=rep.distribution.sequence(camera_positions), look_at=(0,0,0.344))
    prim = stage.GetPrimAtPath(str(prim_path))
    translate_attribute=prim.GetAttribute(translate).Get()
    rotation_attribute=prim.GetAttribute(rotation).Get()
    print("translate_attribute:",translate_attribute)
    print("rotation_attribute:",rotation_attribute)

The result is as follows. It seems that the attribute retrieval is only performed once.
image

Hi @matsumoto-kai -
To capture the translation and rotation values of the camera at each randomly placed position during the execution of your Replicator script, you should make sure that you collect this data within the scope of the on_frame trigger when the rendering is actually taking place. The original code snippet doesn’t seem to fetch the attributes at the right time.

Let’s modify your script to print the camera’s translation and rotation each time a frame is rendered with the updated camera position:

import omni.usd
import omni.replicator.core as rep

stage = omni.usd.get_context().get_stage()
translate = "xformOp:translate"
rotation = "xformOp:rotateXYZ"
prim_path = "/Replicator/Camera_Xform"

camera_positions = [(0, 3, 0.344), (1, 3, 0.344), (2, 3, 0.344), (3, 3, 0.344)]
cube = rep.create.cube()
camera = rep.create.camera(position=(0, 3, 0.344), look_at=(0, 0, 0.344))

def fetch_camera_attributes():
    prim = stage.GetPrimAtPath(str(prim_path))
    if prim:
        translate_attribute = prim.GetAttribute(translate).Get()
        rotation_attribute = prim.GetAttribute(rotation).Get()
        print("translate_attribute:", translate_attribute)
        print("rotation_attribute:", rotation_attribute)
    else:
        print(f"Could not find prim at {prim_path}")


# Register on_frame trigger and assemble scene within that context
with rep.trigger.on_frame(num_frames=len(camera_positions)) as f:
    # Your camera update logic would go here if using a regular sequence of positions
    with camera:
        rep.modify.pose(position=rep.distribution.sequence(camera_positions), 
                        look_at=(0, 0, 0.344))

    # Fetch attributes at this point, for each frame update
    fetch_camera_attributes()

If your camera positioning isn’t at fixed intervals but truly random, you might need to generate random numbers instead of using rep.distribution.sequence(camera_positions).