第六章、Isaacsim中的资产(usd)
第六章、Isaacsim中的资产(usd)
0 前言
官方文档:https://docs.isaacsim.omniverse.nvidia.com/latest/omniverse_usd/open_usd.html
在Isaac sim 中通过通用场景描述(universal scene description USD)来描述机器人及场景。
USD提供文本格式可编辑的.usda
,二进制编码格式的.usd
等。
1 Hello World
在isaaac-sim文件夹下新建一个usd_test
文件夹,并在文件夹下创建一个hello_world.py
,将下述代码copy到文件中:
from pxr import Usd, UsdGeom
stage = Usd.Stage.CreateNew('你的地址/isaac-sim/usd_test/HelloWorld.usda')
xformPrim = UsdGeom.Xform.Define(stage, '/hello')
spherePrim = UsdGeom.Sphere.Define(stage, '/hello/world')
# generic_spherePrim = stage.DefinePrim('/hello/world_generic', 'Sphere')
stage.GetRootLayer().Save()
运行上述代码会在指定的'你的地址/isaac-sim/usd_test
生成'HelloWorld.usda'
打开vcscode,安装USD Language
扩展并在/isaac-sim/.vscode/settings.json
文件最后添加
"files.associations":{
"*.usda": "usd",
"*.usd": "usd"
}
此时打开usd_test/HelloWorld.usda
文件,可以高亮显示:
类型(Type):在 USD 中,Prim(基本元素) 是场景中的基本单位,每个 Prim 都有一个明确的 类型(Type),决定了它的功能和行为。(例如 Sphere 会有半径属性)
- Xform 类型:表示一个包含 变换(Transform) 的容器(例如平移、旋转、缩放)。
- Sphere 类型:表示一个球体几何体(属于 UsdGeom 模块的几何类型)。
组合(Composition):USD 的 组合机制 允许 Prim 嵌套其他 Prim,形成层级结构。每个子 Prim 是独立的实体,具有自己的属性和类型。
# 创建父级 Xform
parent_prim = stage.DefinePrim("/group", "Xform")
# 嵌套子 Sphere
child_prim = stage.DefinePrim("/group/sphere", "Sphere")
内省(Introspection):内省 指在运行时动态检查或创建 Prim 的类型和属性。通过 DefinePrim 直接指定模式名称即可创建对应类型的 Prim。
from pxr import Usd, UsdGeom
# 创建 Stage
stage = Usd.Stage.CreateInMemory()
# 动态创建 Sphere 类型的 Prim
generic_sphere_prim = stage.DefinePrim("/hello/world_generic", "Sphere")
# 设置 Sphere 的半径属性
sphere = UsdGeom.Sphere(generic_sphere_prim)
sphere.GetRadiusAttr().Set(5.0)
命名空间:USD 的类型通过 命名空间(Namespaces) 组织,例如 Xform 和 Sphere 属于 UsdGeom 命名空间,专门用于几何相关类型。
from pxr import UsdGeom # 导入几何相关类型
# 创建 Xform 和 Sphere
xform = UsdGeom.Xform.Define(stage, "/xform")
sphere = UsdGeom.Sphere.Define(stage, "/xform/sphere")
2 在isaac sim场景中加载usda
打开isaac sim通过vscode与isaac sim交互运行代码加载usda模型
import omni
omni.usd.get_context().open_stage("/home/gxy/isaac-sim/usd_test/HelloWorld.usda")
3 查看和修改对象的属性
查看:
from pxr import Usd, Vt
stage = Usd.Stage.Open('/path/to/HelloWorld.usda')
xform = stage.GetPrimAtPath('/hello')
sphere = stage.GetPrimAtPath('/hello/world')
print(xform.GetPropertyNames())
print(sphere.GetPropertyNames())
修改:可以看到球体的半径从 1.0 减小到 0.5,但它也会在控制台中打印这些值。
radiusAttr = sphere.GetAttribute('radius')
print(radiusAttr.Get())
radiusAttr.Set(0.50)
print(radiusAttr.Get())
修改材质等细节见官网:https://openusd-org.translate.goog/release/tut_usd_tutorials.html?_x_tr_sl=auto&_x_tr_tl=zh-CN&_x_tr_hl=en&_x_tr_pto=wapp
4 USD 中的单位系统
USD 通过 Stage 的元数据(Metadata) 定义场景的全局单位。这些单位会影响所有几何变换、物理属性(如质量、速度)和动画时间轴的计算。
在 USD 文件的开头,通常会声明单位:
#usda 1.0
(
metersPerUnit = 1.0 # 距离单位:1.0 表示米
timeCodesPerSecond = 24 # 时间单位:每秒 24 帧(用于动画)
upAxis = "Z" # 坐标系的上方向轴(Isaac Sim 默认是 Z-up)
)
更多细节见:https://docs.isaacsim.omniverse.nvidia.com/latest/reference_material/reference_conventions.html#isaac-sim-conventions