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

加载USDZ文件后,如何在RealityView中获取ModelEntity的Blend Shapes?

解决RealityView中获取ModelEntity Blend Shapes的问题

直接说重点:RealityKit里的ModelEntity不会直接暴露Blend Shapes,得从它关联的MeshResource里提取,具体步骤如下:

1. 找到正确的ModelEntity

加载USDZ后,若模型是嵌套结构,顶级Entity往往不是ModelEntity,需要递归遍历子实体找到目标:

import RealityKit

// 加载USDZ资源
guard let rootEntity = try? Entity.load(named: "YourModel.usdz") else {
    fatalError("加载USDZ失败")
}

// 递归查找ModelEntity
func getModelEntity(from entity: Entity) -> ModelEntity? {
    if let modelEnt = entity as? ModelEntity {
        return modelEnt
    }
    for child in entity.children {
        if let found = getModelEntity(from: child) {
            return found
        }
    }
    return nil
}

guard let modelEntity = getModelEntity(from: rootEntity) else {
    print("未找到目标ModelEntity")
    return
}

2. 从MeshResource提取Blend Shapes

拿到ModelEntity后,通过model.mesh获取MeshResource,它的blendShapes属性就是所有形状键的字典(键为形状名称,值为当前权重):

guard let mesh = modelEntity.model?.mesh as? MeshResource else {
    print("该ModelEntity无有效网格数据")
    return
}

// 打印所有可用的Blend Shape名称
print("可用Blend Shapes: \(mesh.blendShapes.keys)")

// 获取指定Blend Shape的当前权重
if let smileWeight = mesh.blendShapes["Smile"] {
    print("Smile当前权重: \(smileWeight)")
}

// 修改Blend Shape权重
mesh.setBlendShapeWeight(0.7, for: "Smile")

3. 常见问题排查

  • Blender导出遗漏:导出USDZ时必须勾选「形状键(Shape Keys)」选项,否则RealityKit无法读取到Blend Shapes。
  • 系统版本限制:MeshResource的Blend Shape相关API仅支持iOS 15+、macOS 12+,需确认部署目标符合要求。
  • 层级结构错误:不少USDZ模型会将ModelEntity放在子层级中,务必遍历找到正确的实体,不要直接使用顶级Entity。

内容的提问来源于stack exchange,提问作者Frank

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 22:53:20