Saving Mesh with Swift (4.1)


#1

Hi all, I try to save the mesh data on my iPhone, so I can render it on my Mac.
I do this using the following code:

func writeMeshTo(file fileName: String) {
        let fileOptoins: [AnyHashable: Int] = [
            kSTMeshWriteOptionFileFormatKey: 0
        ]
        
        self.slamState.captureState = .captureStateViewing
        self.slamState.mapper?.finalizeTriangleMesh()
        
        let finalMesh: STMesh = self.slamState.mapper!.scene.lockAndGetMesh()
        
        let paths: [URL] = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        print("Writing to folder: \(paths[0])")
        
        do {
            try finalMesh.write(toFile: fileName, options: fileOptoins)
        } catch {
            print("Failed writing mesh file: \(error.localizedDescription.debugDescription)")
        }
}

However, this creates the following output and error:

Writing to folder: file:///var/mobile/Containers/Data/Application/B7A99080-5BEC-48BC-80CD- 
C60C4E0457B2/Documents/
Failed writing mesh file: "Cannot write to `mesh2.obj\'."

Does anyone know what is going wrong?

Thanks!


#2

It seems to me that you are trying to write the file to the wrong URL, maybe even with the file name included. IE, the error seems to indicate that you are trying to save the mesh to the mesh2.obj\ folder location, which I think is wrong.

Have you seen the following page: Saving .OBJ files on iPad in the future?

It seems that they are getting the URL locations with the following link:

NSURL* fileURL = [NSURL fileURLWithPath:filePath]; if(self.documentInteractionController == nil) { self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL]; } else { self.documentInteractionController.URL = fileURL; }

I would also take a look at the following lines:

[myMesh writeToFile:filePath options:@{kSTMeshWriteOptionFileFormatKey: @(STMeshWriteOptionFileFormatObjFile)} error:&error];


#3

Thank you for your help @anthony.monaco

For future reference, I solved it using the following code (Swift 4.1):

func writeMeshTo(file fileName: String) {
    let fileOptoins: [AnyHashable: Int] = [
        kSTMeshWriteOptionFileFormatKey: STMeshWriteOptionFileFormat.objFile.rawValue
    ]
        
    self.slamState.captureState = .captureStateViewing
    self.slamState.mapper?.finalizeTriangleMesh()
    
    let finalMesh: STMesh = self.slamState.mapper!.scene.lockAndGetMesh()
    let path: String = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0].appending("/" + fileName)
        
    do {
        try finalMesh.write(toFile: path, options: fileOptoins)
    } catch {
        print("Failed writing mesh file: \(error.localizedDescription)")
    }
 }

#4

Thanks so much for supplying your code solution! I am changing the title of this post to be more descriptive to the solution for other to more easily find.