converting .usdz to .obj results in flattened model

I'm using the Apple RoomPlan sdk to generate a .usdz file, which works fine, and gives me a 3D scan of my room.

But when I try to use Model I/O's MDLAsset to convert that output into an .obj file, it comes out as a completely flat model shaped like a rectangle. Here is my Swift code:

let destinationURL = destinationFolderURL.appending(path: "Room.usdz")
do {
    try FileManager.default.createDirectory(at: destinationFolderURL, withIntermediateDirectories: true)
    try finalResults?.export(to: destinationURL, exportOptions: .model)
    let newUsdz = destinationURL;
    let asset = MDLAsset(url: newUsdz);
    let obj = destinationFolderURL.appending(path: "Room.obj")
    try asset.export(to: obj)
}

Not sure what's wrong here. According to MDLAsset documentation, .obj is a supported format and exporting from .usdz to the other formats like .stl and .ply works fine and retains the original 3D shape.

Some things I've tried:

  • changing "exportOptions" to parametric, mesh, or model.
  • simply changing the file extension of "destinationURL" (throws error)
Answered by DTS Engineer in 920000022

Your comparison with the STL and PLY exports was the key detail: this is a bug in how MDLAsset writes OBJ files. I reproduced it with a RoomPlan scan on an iPhone 18 Pro Max running iOS 27.2.1. I used your conversion code with each of the three export options. In the OBJ file that MDLAsset writes, every mesh after the first uses the first mesh's transform. Every face of those meshes references vertex 1. In my scan, the first mesh is a wall, so that wall is the only shape that appears when you open the OBJ file. In the same test, the STL export placed every mesh correctly.

Until this is resolved, you can work around it by applying each mesh's transform to its vertices before you export. Then the meshes you export have no transforms. In my tests, the OBJ export wrote meshes without transforms correctly. The function below builds a copy of the asset with each mesh's transform applied. It uses globalTransform(with:atTime:), which returns a mesh's transform "relative to the root coordinate space of the scene that the asset describes."

import ModelIO

// Returns a copy of an asset in which each mesh's world transform has been applied
// to its vertex positions and normals, so none of the copied meshes needs a transform.
func assetWithTransformsApplied(_ asset: MDLAsset) -> MDLAsset {
    let allocator = MDLMeshBufferDataAllocator()
    let result = MDLAsset(bufferAllocator: allocator)

    for case let mesh as MDLMesh in asset.childObjects(of: MDLMesh.self) {
        guard let positions = mesh.vertexAttributeData(forAttributeNamed: MDLVertexAttributePosition, as: .float3)
        else { continue }
        let normals = mesh.vertexAttributeData(forAttributeNamed: MDLVertexAttributeNormal, as: .float3)

        let world = MDLTransform.globalTransform(with: mesh, atTime: 0)
        let normalMatrix = simd_float3x3(
            SIMD3(world.columns.0.x, world.columns.0.y, world.columns.0.z),
            SIMD3(world.columns.1.x, world.columns.1.y, world.columns.1.z),
            SIMD3(world.columns.2.x, world.columns.2.y, world.columns.2.z)
        ).inverse.transpose

        func vector(_ data: MDLVertexAttributeData, _ index: Int) -> SIMD3<Float> {
            let p = data.dataStart.advanced(by: index * data.stride).assumingMemoryBound(to: Float.self)
            return SIMD3(p[0], p[1], p[2])
        }

        var vertices: [Float] = []
        for i in 0..<mesh.vertexCount {
            let p = world * SIMD4(vector(positions, i), 1)
            vertices += [p.x, p.y, p.z]
            if let normals {
                let n = simd_normalize(normalMatrix * vector(normals, i))
                vertices += [n.x, n.y, n.z]
            }
        }

        let descriptor = MDLVertexDescriptor()
        descriptor.attributes[0] = MDLVertexAttribute(name: MDLVertexAttributePosition, format: .float3,
                                                      offset: 0, bufferIndex: 0)
        if normals != nil {
            descriptor.attributes[1] = MDLVertexAttribute(name: MDLVertexAttributeNormal, format: .float3,
                                                          offset: 12, bufferIndex: 0)
        }
        descriptor.layouts[0] = MDLVertexBufferLayout(stride: normals != nil ? 24 : 12)

        let buffer = allocator.newBuffer(with: vertices.withUnsafeBufferPointer { Data(buffer: $0) }, type: .vertex)
        let submeshes = (mesh.submeshes as? [MDLSubmesh] ?? []).map {
            MDLSubmesh(indexBuffer: $0.indexBuffer, indexCount: $0.indexCount, indexType: $0.indexType,
                       geometryType: $0.geometryType, material: $0.material)
        }
        let copy = MDLMesh(vertexBuffer: buffer, vertexCount: mesh.vertexCount,
                           descriptor: descriptor, submeshes: submeshes)
        copy.name = mesh.name
        result.add(copy)
    }
    return result
}

Then export the copy in place of the original asset:

let asset = MDLAsset(url: newUsdz)
try assetWithTransformsApplied(asset).export(to: obj)

In iOS 27.2.1 and macOS 27.2.1, the OBJ files this function produced from my scan matched the room's size in the USDZ file. Every wall and object was in place. The function copies positions and normals. If your meshes also have texture coordinates, copy them the same way, without the transform. This works around the bug rather than fixing it.

Please file a bug report in Feedback Assistant. Attach the USDZ file from RoomPlan, the OBJ file your code produces from it, and your conversion code. If you post the Feedback number here I'll check the status next time I do a sweep of forums posts where I've suggested bug reports.

Your comparison with the STL and PLY exports was the key detail: this is a bug in how MDLAsset writes OBJ files. I reproduced it with a RoomPlan scan on an iPhone 18 Pro Max running iOS 27.2.1. I used your conversion code with each of the three export options. In the OBJ file that MDLAsset writes, every mesh after the first uses the first mesh's transform. Every face of those meshes references vertex 1. In my scan, the first mesh is a wall, so that wall is the only shape that appears when you open the OBJ file. In the same test, the STL export placed every mesh correctly.

Until this is resolved, you can work around it by applying each mesh's transform to its vertices before you export. Then the meshes you export have no transforms. In my tests, the OBJ export wrote meshes without transforms correctly. The function below builds a copy of the asset with each mesh's transform applied. It uses globalTransform(with:atTime:), which returns a mesh's transform "relative to the root coordinate space of the scene that the asset describes."

import ModelIO

// Returns a copy of an asset in which each mesh's world transform has been applied
// to its vertex positions and normals, so none of the copied meshes needs a transform.
func assetWithTransformsApplied(_ asset: MDLAsset) -> MDLAsset {
    let allocator = MDLMeshBufferDataAllocator()
    let result = MDLAsset(bufferAllocator: allocator)

    for case let mesh as MDLMesh in asset.childObjects(of: MDLMesh.self) {
        guard let positions = mesh.vertexAttributeData(forAttributeNamed: MDLVertexAttributePosition, as: .float3)
        else { continue }
        let normals = mesh.vertexAttributeData(forAttributeNamed: MDLVertexAttributeNormal, as: .float3)

        let world = MDLTransform.globalTransform(with: mesh, atTime: 0)
        let normalMatrix = simd_float3x3(
            SIMD3(world.columns.0.x, world.columns.0.y, world.columns.0.z),
            SIMD3(world.columns.1.x, world.columns.1.y, world.columns.1.z),
            SIMD3(world.columns.2.x, world.columns.2.y, world.columns.2.z)
        ).inverse.transpose

        func vector(_ data: MDLVertexAttributeData, _ index: Int) -> SIMD3<Float> {
            let p = data.dataStart.advanced(by: index * data.stride).assumingMemoryBound(to: Float.self)
            return SIMD3(p[0], p[1], p[2])
        }

        var vertices: [Float] = []
        for i in 0..<mesh.vertexCount {
            let p = world * SIMD4(vector(positions, i), 1)
            vertices += [p.x, p.y, p.z]
            if let normals {
                let n = simd_normalize(normalMatrix * vector(normals, i))
                vertices += [n.x, n.y, n.z]
            }
        }

        let descriptor = MDLVertexDescriptor()
        descriptor.attributes[0] = MDLVertexAttribute(name: MDLVertexAttributePosition, format: .float3,
                                                      offset: 0, bufferIndex: 0)
        if normals != nil {
            descriptor.attributes[1] = MDLVertexAttribute(name: MDLVertexAttributeNormal, format: .float3,
                                                          offset: 12, bufferIndex: 0)
        }
        descriptor.layouts[0] = MDLVertexBufferLayout(stride: normals != nil ? 24 : 12)

        let buffer = allocator.newBuffer(with: vertices.withUnsafeBufferPointer { Data(buffer: $0) }, type: .vertex)
        let submeshes = (mesh.submeshes as? [MDLSubmesh] ?? []).map {
            MDLSubmesh(indexBuffer: $0.indexBuffer, indexCount: $0.indexCount, indexType: $0.indexType,
                       geometryType: $0.geometryType, material: $0.material)
        }
        let copy = MDLMesh(vertexBuffer: buffer, vertexCount: mesh.vertexCount,
                           descriptor: descriptor, submeshes: submeshes)
        copy.name = mesh.name
        result.add(copy)
    }
    return result
}

Then export the copy in place of the original asset:

let asset = MDLAsset(url: newUsdz)
try assetWithTransformsApplied(asset).export(to: obj)

In iOS 27.2.1 and macOS 27.2.1, the OBJ files this function produced from my scan matched the room's size in the USDZ file. Every wall and object was in place. The function copies positions and normals. If your meshes also have texture coordinates, copy them the same way, without the transform. This works around the bug rather than fixing it.

Please file a bug report in Feedback Assistant. Attach the USDZ file from RoomPlan, the OBJ file your code produces from it, and your conversion code. If you post the Feedback number here I'll check the status next time I do a sweep of forums posts where I've suggested bug reports.