Skip to content

Instantly share code, notes, and snippets.

View spatial-bit's full-sized avatar

spatial spatial-bit

View GitHub Profile
@spatial-bit
spatial-bit / snippet.py
Created February 3, 2022 16:14
USD get prim starts_with #USD #OMNIVERSE
import omni.usd
from pxr import UsdGeom, Usd, UsdGeom
def get_prims_startswith(stage, root_path, starts_with):
matching_prims = []
root_prim = stage.GetPrimAtPath(root_path)
for prim in Usd.PrimRange(root_prim):
if prim.GetName().startswith(starts_with):
matching_prims.append(prim)
return matching_prims
@spatial-bit
spatial-bit / snippet.py
Created January 25, 2022 23:02
USD more get cameras #USD #OMNIVERSE
# def _get_cameras(stage, filter) -> typing.List:
# """ Get all cameras in the scene except for the ones that are excluded (ie: Perspective, Top, Left, Right cameras)
# """
# cameras = {}
# for prim in Usd.PrimRange(stage.GetPseudoRoot()):
# if prim.IsA(UsdGeom.Camera) and prim.GetName():
# if filter in prim.GetPath().pathString:
# cameras[prim.GetName()] = prim.GetPath().pathString
# return cameras
@spatial-bit
spatial-bit / snippet.py
Created January 24, 2022 20:21
USD Create Xform object #USD #OMNIVERSE
xform = UsdGeom.Xform.Define(stage, '/my_prim')
@spatial-bit
spatial-bit / snippet.py
Created January 21, 2022 20:16
USD Reading transforms/animations from a stage #USD #OMNIVERSE
from pxr import UsdGeom, Usd
stage_path = "D:\\Source\\internal\\projects\\example_simgen\\input\\stage_xxx2usd.usda"
stage = Usd.Stage.Open(stage_path)
prim_path = "/stage/Device_rig"
tx_prop = UsdGeom.Xform.Get(stage, prim_path)
times = tx_prop.GetTimeSamples()
transforms = []
for time in times:
transforms.append(tx_prop.GetLocalTransformation(time=time))
@spatial-bit
spatial-bit / snippet.py
Created January 19, 2022 18:46
USD Get Cameras at Path #USD
from pxr import UsdGeom, Usd, UsdGeom
stage = Usd.Stage.Open('stage.usda')
device_prim = stage.GetPrimAtPath('/world/device')
for prim in Usd.PrimRange(device_prim):
if prim.IsA(UsdGeom.Camera):
print(prim.GetName())
@spatial-bit
spatial-bit / snippet.py
Created January 16, 2022 01:06
USD pose transformations... #USD #OMNIVERSE
# Not sure what I have here
# from pxr import Gf, Usd, UsdGeom
# pose = Gf.Matrix4d(0.10028808960009783, 1.0899614544257474e-13, -0.9949584408830167, 0, -0.2896496895839791, 0.9566873438849783, -0.02919560539207469, 0, 0.9518641480843092, 0.29111737504021507, 0.09594434606284785, 0, 170.13235291094, 118.33856922920114, -26.433392816374774, 1)
# rotateAroundX = Gf.Matrix3d(1, 0, 0, 0, 0, -1, 0, 1, 0)
# rotation = pose.ExtractRotation()
# print(f'rotation={rotation}')
# print(f'type(rotation)={type(rotation)}')
@spatial-bit
spatial-bit / snippet.py
Created January 13, 2022 19:26
USD set viewport #USD #OMNIVERSE
# import omni.kit.app as _app
# import omni.usd as _usd
# import omni.client
# import omni.renderer_capture
# import omni.timeline
# from omni.kit import app
# USD setting viewport
width = 640
@spatial-bit
spatial-bit / snippet.py
Created January 13, 2022 19:19
USD procedural variants #USD #OMNIVERSE
# procedural variants
import omni.usd as _usd
stage = _usd.get_context().get_stage()
rootPrim = stage.GetPrimAtPath('/base_stage')
#rootPrim = stage.GetPseudoRoot()
vset = rootPrim.GetVariantSets()
print(vset.GetNames())
@spatial-bit
spatial-bit / snippet.py
Last active January 15, 2022 01:00
USD get transform property+ #USD #OMNIVERSE
# USD get transform property at path
import omni.usd
prim_path = '/Motions/traj_'
stage = omni.usd.get_context().get_stage()
prop = stage.GetPropertyAtPath(f"{prim_path}.xformOp:transform")
value = prop.Get()
print(value)
# or
@spatial-bit
spatial-bit / snippet.py
Last active January 13, 2022 19:07
USD get prim at path+ #USD #OMNIVERSE
# USD get prim
import omni.usd
from pxr import UsdGeom
stage = omni.usd.get_context().get_stage()
prim = stage.GetPrimAtPath('/Motions')
# look at children of prim and print path as string
target_prim_path = prim.GetAllChildren()[0].GetPath().pathString