It’s hard to find such simple function in omniverse…
I found a way to get the prim and its transform matrix by the following code
world = World(stage_units_in_meters=1.0)
sphere = get_prim_at_path('/Sphere')
mat = omni.usd.get_world_transform_matrix(sphere)
How can I set the location of a prim using python?
Hi @joshchiu - To set the prim location in Isaac Sim using Python, you can use the set_pose function from the omni.isaac.dynamic_control API. Here is an example:
from omni.isaac.dynamic_control import _dynamic_control
from omni.physx.scripts import utils
from pxr import Gf, UsdGeom
dc = _dynamic_control.acquire_dynamic_control_interface()
# Get the prim you want to move
prim_path = "/path/to/your/prim"
prim = dc.get_rigid_body(prim_path)
# Set the new location
new_location = Gf.Vec3f(1.0, 2.0, 3.0) # Replace with your desired location
new_rotation = Gf.Rotation(Gf.Vec3d(1, 0, 0), 0) # Replace with your desired rotation
# Create a new transform
new_transform = UsdGeom.TransformAPI(prim)
new_transform.SetTransform(UsdGeom.XformOp.Transform(Gf.Matrix4d(new_rotation.GetMatrix(), new_location)))
# Apply the new transform
dc.set_rigid_body_pose(prim, new_transform.GetLocalTransformation())
This script will move the specified prim to the new location. Make sure to replace “/path/to/your/prim” with the actual path to your prim, and new_location with the desired location. The new_rotation variable can be used to set the rotation of the prim.
Hi @rthaker, I tried the script and got the following error
transform = UsdGeom.TransformAPI(prim)
AttributeError: module 'pxr.UsdGeom' has no attribute 'TransformAPI'
And I can’t find any document about TransformAPI
Hi @joshchiu - I apologize for the confusion. The TransformAPI
is not a part of the pxr.UsdGeom
module. Instead, you should use the Gf.Matrix4d()
function to create a new transformation matrix. Here’s how you can modify the script:
from omni.isaac.dynamic_control import _dynamic_control
from pxr import Gf
# Acquire dynamic control interface
dc = _dynamic_control.acquire_dynamic_control_interface()
# Get the prim you want to move
prim_path = "/path/to/your/prim"
prim = dc.get_rigid_body(prim_path)
# Set the new location
new_location = Gf.Vec3f(1.0, 2.0, 3.0) # Replace with your desired location
# Set the new rotation
new_rotation = Gf.Rotation(Gf.Vec3d(1, 0, 0), 0) # Replace with your desired rotation
# Create a new transform
new_transform = Gf.Matrix4d(new_rotation.GetMatrix(), new_location)
# Apply the new transform
dc.set_rigid_body_pose(prim, new_transform)
```or, which are created from new_rotation and new_location, respectively.
This script will create a new transformation matrix using the `Gf.Matrix4d()` function and apply it to the specified prim. The `Gf.Matrix4d()` function takes two arguments: a rotation matrix and a translation vector, which are created from `new_rotation` and `new_location` , respectively.
If this doesn't work then I will try to find the expert who can help you resolve your issue.