import omni.usd
from pxr import Usd, Gf

stage = omni.usd.get_context().get_stage()
cam = stage.GetPrimAtPath("/World/Camera")  # <-- 替换为你的实体Camera路径!

if not cam.IsValid():
    print("没找到实体Camera! 请先新建Camera并用Align对齐视图~")
else:
    pos = cam.GetAttribute("xformOp:translate").Get()
    orient = cam.GetAttribute("xformOp:orient").Get()
    if orient:
        q = orient  # Gf.Quatf
        quat = [q.GetReal(), q.GetImaginary()[0], q.GetImaginary()[1], q.GetImaginary()[2]]
        print("Camera position:", pos)
        print("Camera rotation (quaternion):", quat)
    else:
        rot = cam.GetAttribute("xformOp:rotateXYZ").Get()
        print("Camera position:", pos)
        print("Camera rotation (euler):", rot)






import omni.usd
from pxr import Usd, Gf
from scipy.spatial.transform import Rotation as R
import numpy as np

stage = omni.usd.get_context().get_stage()
cam = stage.GetPrimAtPath("/World/Camera")  # <-- 替换为你的实体Camera路径!
if not cam.IsValid():
    print("没找到实体Camera! 请先新建Camera并用Align对齐视图~")
else:
    pos = cam.GetAttribute("xformOp:translate").Get()
    orient = cam.GetAttribute("xformOp:orient").Get()
    if orient:
        q = orient  # Gf.Quatf
        # Gf.Quatf（w, x, y, z）转为scipy的[x, y, z, w]
        quat = [q.GetReal(), q.GetImaginary()[0], q.GetImaginary()[1], q.GetImaginary()[2]]

        # 转为 scipy 的 [x, y, z, w]
        quat_s = [quat[1], quat[2], quat[3], quat[0]]

        # Z轴逆时针-90度补偿
        r_correction = R.from_euler('z', -90, degrees=True)
        r_app = R.from_quat(quat_s)
        r_total = r_correction * r_app
        q_new = r_total.as_quat()  # [x, y, z, w]

        print("Camera position:", pos)
        print("Camera rotation (quaternion, with -90deg z compensation):", list(q_new))
    else:
        rot = cam.GetAttribute("xformOp:rotateXYZ").Get()
        print("Camera position:", pos)
        print("Camera rotation (euler):", rot)






import omni.usd
from pxr import Usd, Gf
from scipy.spatial.transform import Rotation as R
stage = omni.usd.get_context().get_stage()
cam = stage.GetPrimAtPath("/World/Camera")
if not cam.IsValid():
    print("没找到实体Camera! 请先新建Camera并用Align对齐视图~")
else:
    pos = cam.GetAttribute("xformOp:translate").Get()
    orient = cam.GetAttribute("xformOp:orient").Get()
    if orient:
        q = orient  # Gf.Quatf
        # Gf.Quatf(w, x, y, z) → scipy [x, y, z, w]
        quat = [q.GetReal(), q.GetImaginary()[0], q.GetImaginary()[1], q.GetImaginary()[2]]  # [w, x, y, z]
        quat_s = [quat[1], quat[2], quat[3], quat[0]]  # [x, y, z, w]
        # 逆时针210度补偿（z轴）
        r_correction = R.from_euler('z', -180, degrees=True)
        r_app = R.from_quat(quat_s)
        r_total = r_correction * r_app
        q_new = r_total.as_quat()  # [x, y, z, w]，直接给你的捕获脚本
        print("Camera position:", pos)
        print("Camera rotation (quaternion, with -210deg z compensation):", list(q_new))
    else:
        rot = cam.GetAttribute("xformOp:rotateXYZ").Get()
        print("Camera position:", pos)
        print("Camera rotation (euler):", rot)





import omni.usd
from pxr import Usd, Gf
from scipy.spatial.transform import Rotation as R
stage = omni.usd.get_context().get_stage()
cam = stage.GetPrimAtPath("/World/Camera")
if not cam.IsValid():
    print("没找到实体Camera! 请先新建Camera并用Align对齐视图~")
else:
    pos = cam.GetAttribute("xformOp:translate").Get()
    orient = cam.GetAttribute("xformOp:orient").Get()
    if orient:
        q = orient  # Gf.Quatf
        # Gf.Quatf(w, x, y, z) → scipy [x, y, z, w]
        quat = [q.GetReal(), q.GetImaginary()[0], q.GetImaginary()[1], q.GetImaginary()[2]]  # [w, x, y, z]
        quat_s = [quat[1], quat[2], quat[3], quat[0]]  # [x, y, z, w]

        r_correction = R.from_euler('zyx', [360, 195, -10], degrees=True)
        r_app = R.from_quat(quat_s)
        r_total = r_correction * r_app
        q_new = r_total.as_quat()  # [x, y, z, w]
        print("Camera position:", pos)
        print("Camera rotation:", list(q_new))
    else:
        rot = cam.GetAttribute("xformOp:rotateXYZ").Get()
        print("Camera position:", pos)
        print("Camera rotation (euler):", rot)






import omni.usd
from pxr import Usd

stage = omni.usd.get_context().get_stage()
prim = stage.GetPrimAtPath("/World/Camera")
if not prim.IsValid():
    print("找不到实体Camera")
else:
    # 打印局部坐标
    print("Local Position:", prim.GetAttribute("xformOp:translate").Get())
    # 打印世界坐标
    m = UsdGeom.Xformable(prim).ComputeLocalToWorldTransform(Usd.TimeCode.Default())
    print("World Transform Matrix:")
    print(m)





from pxr import UsdGeom, Usd
import omni.usd

stage = omni.usd.get_context().get_stage()
prim = stage.GetPrimAtPath("/World/Camera")

if prim.IsValid():
    xform = UsdGeom.Xformable(prim)
    world_xform = xform.ComputeLocalToWorldTransform(Usd.TimeCode.Default())
    # 世界坐标
    position = [world_xform.ExtractTranslation()[i] for i in range(3)]
    # 世界四元数
    rot = world_xform.ExtractRotationQuat() # Gf.Quatf
    # 转为scipy格式[x,y,z,w]
    quat = [rot.GetImaginary()[0], rot.GetImaginary()[1], rot.GetImaginary()[2], rot.GetReal()]
    print("Exported world position:", position)
    print("Exported world rotation quaternion:", quat)




from pxr import UsdGeom, Usd
import omni.usd

stage = omni.usd.get_context().get_stage()
prim = stage.GetPrimAtPath("/World/Camera")

if prim.IsValid():
    xform = UsdGeom.Xformable(prim)
    world_xform = xform.ComputeLocalToWorldTransform(Usd.TimeCode.Default())
    # 世界坐标
    position = [world_xform.ExtractTranslation()[i] for i in range(3)]
    # 世界四元数
    rot = world_xform.ExtractRotationQuat() # Gf.Quatf
    # 转为scipy格式[x,y,z,w]
    quat = [rot.GetImaginary()[0], rot.GetImaginary()[1], rot.GetImaginary()[2], rot.GetReal()]
    print("Exported world position:", position)
    print("Exported world rotation quaternion:", quat)





from pxr import Usd, UsdGeom
import omni.usd
from scipy.spatial.transform import Rotation as R

stage = omni.usd.get_context().get_stage()
prim = stage.GetPrimAtPath("/World/Camera")   # 你的camera路径

if prim.IsValid():
    # 常规命名：'xformOp:rotateXYZ'
    rot_attr = prim.GetAttribute("xformOp:rotateXYZ")
    if rot_attr:
        euler_xyz = rot_attr.Get()  # [x, y, z]，单位为度
        print("Euler angles (deg):", euler_xyz)
        quat = R.from_euler('xyz', euler_xyz, degrees=True).as_quat()  # [x, y, z, w]
        print("Exported world position:", prim.GetAttribute("xformOp:translate").Get())
        print("Exported world rotation quaternion:", quat)
    else:
        print("没有rotateXYZ属性，可检查相机transform或路径")






from pxr import UsdGeom, Usd
import omni.usd
from scipy.spatial.transform import Rotation as R
import numpy as np

stage = omni.usd.get_context().get_stage()
prim = stage.GetPrimAtPath("/World/Camera")   # 修改成你的相机路径

if prim.IsValid():
    xform = UsdGeom.Xformable(prim)
    # 获取世界变换矩阵
    world_xform = xform.ComputeLocalToWorldTransform(Usd.TimeCode.Default())
    position = [world_xform.ExtractTranslation()[i] for i in range(3)]

    # 获取世界四元数：Gf.Quatf
    rot = world_xform.ExtractRotationQuat()
    quat = [rot.GetImaginary()[0], rot.GetImaginary()[1], rot.GetImaginary()[2], rot.GetReal()]  # [x, y, z, w]
    formatted_quat = '[{}]'.format(', '.join(f'{x:.20f}' for x in quat))

    # 从Transform得到的3x3矩阵 (只取前三行前三列)
    T = np.array([
        [-1.0, 0.0, 0.0],
        [0.0, 0.0, -1.0],
        [0.0, -1.0, 0.0]
    ])

    # Step 1: 父frame的旋转部分求四元数
    r_T = R.from_matrix(T)
    # Step 2: 取逆（因为你要从world到gauss)
    r_T_inv = r_T.inv()
    # Step 3: 变换你的相机四元数
    r_world = R.from_quat(quat)
    # Step 4: 新的本地frame四元数
    r_local = r_T.inv() * r_world
    q_new = r_local.as_quat()  # [x, y, z, w]，直接塞到Camera即可！

    # 四元数转欧拉角，顺序'xyz'（单位:度）
    angles = R.from_quat(quat).as_euler('xyz', degrees=True)

    print("===== /World/Camera 全世界坐标 =====")
    print("World Position:", position)
    print("World Quaternion [x, y, z, w]:", formatted_quat)
    print("New World Quaternion [x, y, z, w]:", q_new)
    print("World Euler angles (xyz, deg):", angles)
else:
    print("找不到/World/Camera")






from pxr import UsdGeom, Usd
import omni.usd
from scipy.spatial.transform import Rotation as R
import numpy as np

stage = omni.usd.get_context().get_stage()
prim = stage.GetPrimAtPath("/World/Camera")  # 修改成你的相机路径

if prim.IsValid():
    xform = UsdGeom.Xformable(prim)
    # 世界坐标
    world_xform = xform.ComputeLocalToWorldTransform(Usd.TimeCode.Default())
    position = [world_xform.ExtractTranslation()[i] for i in range(3)]

    # Omni输出的世界四元数 [x, y, z, w]
    rot = world_xform.ExtractRotationQuat()
    quat = np.array([
        rot.GetImaginary()[0],
        rot.GetImaginary()[1],
        rot.GetImaginary()[2],
        rot.GetReal()
    ])
    print("World Position:", position)
    print("原始World Quaternion: [x, y, z, w]:", quat)

    # 3dgs/gauss的父 Transform (只取3x3上半角)
    T = np.array([
        [-1.0, 0.0, 0.0],
        [0.0, 0.0, -1.0],
        [0.0, -1.0, 0.0]
    ])
    r_T = R.from_matrix(T)
    r_T_inv = r_T.inv()
    r_world = R.from_quat(quat)

    print("\n==== 常用变换组合（四元数均[x, y, z, w]顺序）====")
    combos = {
        "A: 不做任何补偿": quat,
        "B: gauss父frame逆左乘": (r_T_inv * r_world).as_quat(),
        "C: 父frame逆右乘": (r_world * r_T_inv).as_quat(),
        "D: 父frame正左乘": (r_T * r_world).as_quat(),
        "E: 父frame正右乘": (r_world * r_T).as_quat(),
        "F: 全部分量取反": -quat,
        "G: [w, x, y, z]": quat[[3, 0, 1, 2]],
    }

    for name, q in combos.items():
        print(f"\n{name}")
        print("四元数:", q)
        try:
            angles = R.from_quat(q).as_euler('xyz', degrees=True)
            print("Euler(xyz, deg):", angles)
            fwd = R.from_quat(q).apply([0, 0, 1])
            print("摄像机朝向Fwd(z):", fwd)
        except Exception as ex:
            print("无法转换欧拉角:", ex)
else:
    print("找不到/World/Camera")






from pxr import Usd, UsdGeom
from scipy.spatial.transform import Rotation as R
import numpy as np
import omni.usd

stage = omni.usd.get_context().get_stage()
prim = stage.GetPrimAtPath("/World/Camera")
if prim.IsValid():
    xform = UsdGeom.Xformable(prim)
    world_xform = xform.ComputeLocalToWorldTransform(Usd.TimeCode.Default())
    position = [world_xform.ExtractTranslation()[i] for i in range(3)]
    rot = world_xform.ExtractRotationQuat()
    quat = np.array([
        rot.GetImaginary()[0],
        rot.GetImaginary()[1],
        rot.GetImaginary()[2],
        rot.GetReal()
    ])
    r_world = R.from_quat(quat)
    # 相机forward和up（世界frame下）
    forward = r_world.apply([0,0,1])
    up = r_world.apply([0,1,0])
    # gauss Transform
    T = np.array([
        [-1.0,  0.0,  0.0],
        [ 0.0,  0.0, -1.0],
        [ 0.0, -1.0,  0.0]
    ])
    # 变换方向到点云本地frame
    f2 = T.T @ forward
    u2 = T.T @ up
    # 用新的朝前/朝上向量生成本地frame四元数
    r_new = R.from_matrix(np.column_stack([
        np.cross(u2, f2),  # right
        u2,                # up
        f2                 # forward
    ]))
    q_new = r_new.as_quat()
    print("\n=== 最通用: 直接用方向向量变换后重建四元数 ===")
    print("四元数[x, y, z, w]:", q_new)
    print("欧拉角(xyz, deg):", r_new.as_euler('xyz', degrees=True))
else:
    print("找不到/World/Camera")






from pxr import UsdGeom, Usd
import omni.usd
from scipy.spatial.transform import Rotation as R
import numpy as np

stage = omni.usd.get_context().get_stage()
prim = stage.GetPrimAtPath("/World/Camera")   # 修改成你的相机路径

if prim.IsValid():
    xform = UsdGeom.Xformable(prim)
    # 获取世界变换矩阵
    world_xform = xform.ComputeLocalToWorldTransform(Usd.TimeCode.Default())
    position = [world_xform.ExtractTranslation()[i] for i in range(3)]

    # 获取世界四元数：Gf.Quatf
    rot = world_xform.ExtractRotationQuat()
    quat = [rot.GetImaginary()[0], rot.GetImaginary()[1], rot.GetImaginary()[2], rot.GetReal()]  # [x, y, z, w]
    formatted_quat = '[{}]'.format(', '.join(f'{x:.20f}' for x in quat))

    # 四元数转欧拉角，顺序'xyz'（单位:度）
    angles = R.from_quat(quat).as_euler('xyz', degrees=True)

    print("===== /World/Camera 全世界坐标 =====")
    print("World Position:", position)
    print("World Quaternion [x, y, z, w]:", formatted_quat)
    print("World Euler angles (xyz, deg):", angles)
else:
    print("找不到/World/Camera")







from pxr import UsdGeom, Usd
import omni.usd
from scipy.spatial.transform import Rotation as R
import numpy as np

stage = omni.usd.get_context().get_stage()
prim = stage.GetPrimAtPath("/World/Camera")
if prim.IsValid():
    xform = UsdGeom.Xformable(prim)
    world_xform = xform.ComputeLocalToWorldTransform(Usd.TimeCode.Default())
    position = [world_xform.ExtractTranslation()[i] for i in range(3)]
    rot = world_xform.ExtractRotationQuat()
    quat = np.array([
        rot.GetImaginary()[0],
        rot.GetImaginary()[1],
        rot.GetImaginary()[2],
        rot.GetReal()
    ])
    formatted_quat = '[{}]'.format(', '.join(f'{x:.20f}' for x in quat))
    print("World Position:", position)
    print("原始World Quaternion: [x, y, z, w]:", formatted_quat)

    # 父frame为RotateX = -90，相当于你的gauss本地frame比世界frame多了一个+90°的x旋转
    # 所以用 +90° 左乘采样四元数
    r_fix = R.from_euler('x', 90, degrees=True)
    r_world = R.from_quat(quat)
    quat_new = (r_fix * r_world).as_quat()
    formatted_quat_new = '[{}]'.format(', '.join(f'{x:.20f}' for x in quat_new))

    print("推荐: 最终用于Camera四元数 [x, y, z, w]:", formatted_quat_new)
    print("其欧拉角: (deg):", R.from_quat(quat_new).as_euler('xyz', degrees=True))
else:
    print("找不到/World/Camera")





