Skip to content

USD API

dcc_mcp_core (usd 模块)

DCC-MCP-Core 的 USD(通用场景描述)支持。

概述

提供:

  • 核心 USD 类型: SdfPathVtValueUsdPrimUsdStage
  • USDA 序列化: 导出的场景为人类可读的 .usda 文本
  • JSON 传输: 序列化和反序列化场景用于 MCP IPC
  • DCC 桥接: 在 SceneInfo JSON 和 UsdStage 之间转换
  • 纯 Rust: 无需 OpenUSD C++ 库依赖

WARNING

此 crate 提供兼容的数据模型和序列化格式用于轻量级场景描述交换。它不链接 OpenUSD C++ 库。

SdfPath

USD 场景图路径(例如 /World/Cube)。

构造函数

python
from dcc_mcp_core import SdfPath

path = SdfPath("/World/Cube")

方法

方法返回描述
child(name)SdfPath追加子路径段
parent()SdfPath | None父路径
__eq__(other)bool相等比较
__hash__()int哈希值(可用作字典键)
__str__()str字符串表示

属性

属性类型描述
is_absolutebool路径是否以 / 开头
namestr最后一个路径元素

示例

python
path = SdfPath("/World/Cube")
print(path.is_absolute)  # True
print(path.name)         # Cube
child = path.child("Material")  # SdfPath("/World/Cube/Material")
parent = path.parent()   # SdfPath("/World")

VtValue

USD 变体值容器。

工厂方法

方法返回描述
from_bool(v)VtValue创建布尔值
from_int(v)VtValue创建整数值
from_float(v)VtValue创建浮点值
from_string(v)VtValue创建字符串值
from_token(v)VtValue创建 token 值
from_asset(v)VtValue创建 asset 值
from_vec3f(x, y, z)VtValue从浮点数创建 vec3

属性

属性类型描述
type_namestrUSD 类型名称(例如 float3

方法

方法返回描述
to_python()bool | int | float | str | tuple | list | None转换为 Python 原生类型

示例

python
v_bool = VtValue.from_bool(True)
v_int = VtValue.from_int(42)
v_float = VtValue.from_float(3.14)
v_vec3 = VtValue.from_vec3f(1.0, 2.0, 3.0)

print(v_vec3.to_python())   # (1.0, 2.0, 3.0)
print(v_vec3.type_name)    # float3

UsdPrim

USD stage 中的图元。

属性

属性类型描述
pathSdfPath图元路径
type_namestr图元类型(例如 Mesh
activebool图元是否激活
namestr图元名称

方法

方法返回描述
set_attribute(name, value)None设置属性值
get_attribute(name)VtValue | None获取属性值
attribute_names()list[str]列出属性名称
attributes_summary()dict[str, str]属性名称到类型名称的映射
has_api(schema)bool检查是否有 API

UsdStage

USD 场景数据的主要容器。

构造函数

python
from dcc_mcp_core import UsdStage

stage = UsdStage("my_scene")

属性

属性类型描述
namestrStage 名称
idstrStage UUID
default_primstr | None默认图元名称
up_axisstr向上轴(XYZ
meters_per_unitfloat每单位米数
fpsfloat | None每秒帧数
start_time_codefloat | None开始时间码
end_time_codefloat | None结束时间码

方法

方法返回描述
define_prim(path, type_name)UsdPrim定义图元
get_prim(path)UsdPrim | None获取图元
has_prim(path)bool检查图元是否存在
remove_prim(path)bool删除图元
traverse()list[UsdPrim]获取所有图元
prims_of_type(type_name)list[UsdPrim]获取特定类型的图元
set_attribute(prim_path, attr_name, value)None设置属性
get_attribute(prim_path, attr_name)VtValue | None获取属性
metrics()dict[str, int]获取 stage 指标
to_json()str导出为 JSON
from_json(json) (静态方法)UsdStage从 JSON 导入
export_usda()str导出为 USDA 文本

示例

python
stage = UsdStage("my_scene")
stage.default_prim = "World"

stage.define_prim("/World", "Xform")
stage.define_prim("/World/Cube", "Mesh")
stage.set_attribute("/World/Cube", "extent", VtValue.from_vec3f(1, 1, 1))

# 序列化
json_str = stage.to_json()
usda = stage.export_usda()

# 查询
for prim in stage.traverse():
    print(f"{prim.path} ({prim.type_name})")

DCC 桥接函数

scene_info_json_to_stage()

python
from dcc_mcp_core import scene_info_json_to_stage

scene_info_json = '{"name": "my_scene", "prim_count": 100}'
usd_stage = scene_info_json_to_stage(scene_info_json, "maya")

stage_to_scene_info_json()

python
from dcc_mcp_core import stage_to_scene_info_json

scene_info_json = stage_to_scene_info_json(stage)

units_to_mpu()

python
from dcc_mcp_core import units_to_mpu

print(units_to_mpu("cm"))   # 0.01
print(units_to_mpu("m"))    # 1.0

mpu_to_units()

python
from dcc_mcp_core import mpu_to_units

print(mpu_to_units(0.01))   # cm
print(mpu_to_units(1.0))    # m

性能说明

  • USDA 导出是人类可读的但比 JSON 慢
  • JSON 序列化是紧凑和快速的,适合 IPC
  • 路径验证在构造时执行,而不是惰性地执行

Released under the MIT License.