You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取Python绑定的pxr.Usd.Prim对象的所有属性?

获取USD Prim对象的可访问属性方法

因为pxr.Usd.Prim是C++对象的Python绑定,Python原生的dir()和vars()只能拿到绑定的方法,无法直接获取USD层面的属性,得用USD提供的API来查询:

  • 获取Prim上所有属性(含Schema定义的默认属性)
    用GetAttributes()方法遍历所有属性实例,再获取属性名:

    from pxr import Usd
    
    # 假设prim是已获取的pxr.Usd.Prim对象
    for attr in prim.GetAttributes():
        print(f"属性名: {attr.GetName()},类型: {attr.GetTypeName()}")
    

    这个方法会返回该Prim所属Schema定义的所有属性,不管属性有没有被明确设置值。

  • 仅获取Prim上已创作(明确设置)的属性
    如果只关心该Prim上实际被设置过值的属性,用GetAuthoredAttributes():

    for attr in prim.GetAuthoredAttributes():
        print(f"已创作属性: {attr.GetName()}")
    
  • 通过Schema类查询属性
    先判断Prim的Schema类型,再用对应Schema类的API获取属性列表或直接访问预定义属性:

    from pxr import UsdGeom
    
    if prim.IsA(UsdGeom.Mesh):
        mesh = UsdGeom.Mesh(prim)
        # 获取Mesh Schema的所有属性名
        for attr_name in mesh.GetSchemaAttributeNames():
            print(f"Mesh属性: {attr_name}")
        # 直接访问预定义属性(更推荐,避免拼写错误)
        points_attr = mesh.GetPointsAttr()
        print(f"Points属性值: {points_attr.Get()}")
    

    每个USD Schema类(如UsdGeom.Xform、UsdShade.Material)都有类似的Get[AttributeName]Attr()方法,以及GetSchemaAttributeNames()返回该Schema的所有属性名。

  • 区分属性与关系
    USD中属性(Attribute)和关系(Relationship)都属于Property,用GetPropertyNames()可以拿到所有Property名,再过滤出属性:

    from pxr import Usd
    
    for prop_name in prim.GetPropertyNames():
        prop = prim.GetProperty(prop_name)
        if isinstance(prop, Usd.Attribute):
            print(f"属性: {prop_name}")
        # 若需要关系也可以单独处理
        # elif isinstance(prop, Usd.Relationship):
        #     print(f"关系: {prop_name}")
    

内容的提问来源于stack exchange,提问作者Der Fänger im Roggen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 04:29:58