Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programatically save arkit object in .dae format

I am working on an app the creates a 3d mesh of users face. I am successful in generating data of a users face.

I want to programatically save this data in .dae format so that i could export my .dae file, edit that in 3d softwares like blender, and than further import it in my iphone and display that file in sceneview.

Long story short i want programatically save the data in .dae format. I am not able to find anything on internet about this.

If there could be another approach then please tell me.

like image 382
Jagjot Singh Avatar asked Apr 03 '18 09:04

Jagjot Singh


2 Answers

SceneKit doesn’t natively read or write DAE format in iOS. (SceneKit reads/writes DAE only in macOS. When you ship a DAE in your app’s bundle resources, Xcode converts it to an iOS-optimized format at build time.)

Your best bet for exporting a mesh to common file formats from iOS is Model I/O. That framework supports several formats, but not DAE. If you’re just looking to output the ARFaceGeometry mesh generated by ARKit, you don’t really need a format more complicated than OBJ, and Model I/O does that.

The gist:

  1. Create an MDLMesh from your vertex/index data. That’ll require MDLMeshBuffers for the vertex and texture coordinate data, and MDLSubmeshes for the triangle index data. Or, if you already have the mesh in SceneKit, convert it with MDLMesh(scnGeometry:).

  2. Create an empty MDLAsset, and add your mesh to it as a child object.

  3. Export the asset to a file. The filename extension of the URL you provide for writing the asset to determines the file format for export, so use a “.obj” filename if you want to write an OBJ file.

like image 98
rickster Avatar answered Sep 28 '22 16:09

rickster


you can try the code below:

  let scene2 = sceneView.scene
  let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
  let timeInterval = Date().timeIntervalSince1970 * 1000
  let filename = String(format: "test_%d.dae", timeInterval)
  let exportUrl = documentsPath.appendingPathComponent(filename)
  scene2.write(to: exportUrl, options: nil, delegate: nil, progressHandler: nil);
like image 24
user1436465 Avatar answered Sep 28 '22 18:09

user1436465