5

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Hello Jagjot, I want to do the same but couldn't find anything on the web, can you help me if you've done it? –  Apr 05 '19 at 14:01

2 Answers2

7

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.

rickster
  • 124,678
  • 26
  • 272
  • 326
  • Thanks for reply but i want to export the mesh file and edit that in 3d softwares like blender. Is this approach that you gave suitable for that? – Jagjot Singh Apr 05 '18 at 07:13
  • 1
    hello rickster, I am trying to do the same; exporting a mesh programmatically into Obj. file and I can't find anything on the web. Is it possible for you to give some more information on how I can do it, or maybe share some resources with me? –  Apr 05 '19 at 14:36
  • 1
    If using `MDLMesh(scnGeometry:)` the way to import the header is `import SceneKit.ModelIO` – Juan Boero Jul 02 '19 at 18:57
  • is there any method export ARFaceGeometry with actual face size?? – MJ Studio Sep 25 '19 at 10:16
  • I'm stumped on this, would love some example code, here's a question I asked related to it: https://stackoverflow.com/questions/59475201/save-arfacegeometry-to-obj-file – Daniel McLean Dec 27 '19 at 22:13
3

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);
  • Hey Can you please tell me can we also save the facial expressions while exporting into file.after that using the same file with the mesh replicate the same expressions.? Can you guide me on how to do this? – Chaitu Jan 28 '20 at 08:49
  • As of iOS 16, the code in this answer now exports an SCN file (with a DAE file extension). Think it did previously work. – Nestor Oct 31 '22 at 13:02