Rotate prim globally using python API

Hello,

I want to rotate prim locally and globally as well. There should not be any parent to prim.

So, I want to know the API methods (python) that can be used to rotate a prim locally and globally. for ex. like in Unity we have transform.localRotation and transform.rotation, do we have same kinds of APIs in Omniverse ?

1 Like

Hi, I just figured this out, although I have some problems with rotation with my extension camera keys.

The easiest way to find this kind of stuff, is enable the extension Commands in Windows - Extensions.

Then go to the Windows - Commands

This will launch the Commands window. Select your prop and rotate it, and the python that is executed under the hood will show in the commands window.

Here is an example of rotating a camera, you can replace /World/Camera with the path to your prim.
omni.kit.commands.execute(‘ChangeProperty’,
prop_path=Sdf.Path(‘/World/Camera.xformOp:rotateYXZ’),
value=Gf.Vec3f(newRotationX, rotationY, rotationZ),
prev=Gf.Vec3f(rotationX, rotationY, rotationZ))

Here is my full button click where I get the rotation of a camera and move it based upon a slider value (@maticodes helped with this and he wrote the camera moving forward math because I would have never figured it out).

import omni.ext
import omni.ui as ui
import omni.timeline
import math
from omni.kit.viewport.utility import get_active_viewport
from pxr import Sdf, Usd, UsdGeom, Gf
import omni.usd

def XRotateUp_Click():

                usd_context = omni.usd.get_context()
                stage = usd_context.get_stage()
                active_viewport = get_active_viewport()
                camera_path = active_viewport.camera_path
                camera = stage.GetPrimAtPath("/World/Camera")                    
                timeline = omni.timeline.get_timeline_interface()
                current_frame = timeline.get_current_time() * timeline.get_time_codes_per_seconds()

                xForm = UsdGeom.Xformable(camera)
                local_transform: Gf.Matrix4d = xForm.GetLocalTransformation()                    
                decomposed_Transform = decompose_matrix(local_transform)
                
                 # local_rotate = get_local_rot(camera)
                rotationX = round(decomposed_Transform[1][0], 1)
                rotationY = round(decomposed_Transform[1][1], 1)
                rotationZ = round(decomposed_Transform[1][2], 1)

                # calculate the new value
                newRotationX = round(rotationX + self._RotationValue, 1)

                omni.kit.commands.execute('ChangeProperty',
                    prop_path=Sdf.Path('/World/Camera.xformOp:rotateYXZ'),
                    value=Gf.Vec3f(newRotationX, rotationY, rotationZ),
                    prev=Gf.Vec3f(rotationX, rotationY, rotationZ))

                label.text = "New Rotation X = " + str(newRotationX)

If you want the full extension, the code is here:

In case anyone wants to try this extension

image

You can load this extension into Omniverse by:

  1. Download the zip file at the link above.
  2. Extra the contents to a folder.
  3. In Create (or Code) go to Extensions, click on the Gear Icon.
  4. In the places to look at the top, click the plus sign, then double click in the blank area, and paste in the path to the exts folder of the extracted zip file from step 1.

Video coming soon, but to use it set the timeline to the frame you desire, move your camera to where you want, then click the Set Keys button. This sets all 6 keys for translate x,y,z and rotation x,y,z.

I have already found this pretty useful for making. fly throughs and other camera movements.

1 Like