Saving 6D.ai’s mesh at runtime as an OBJ in Unity

For anyone who’s wondering how to save the meshes generated by 6d.ai, it’s pretty straightforward. I’ve seen a few people asking and no definitive answer, so thought I’d write it up in case anyone searching for this found it useful.
The steps are basically:
- Combine the chunks into one mesh.
I used Unity’s built-in Combine Meshes. You can see a sample of the code here, which I adapted. - **Save the mesh locally.
**I used obj-unity3d to save the mesh as an obj to Application.persistentDataPath. - Access the mesh.
Currently 6d.ai is iOS only, so plug your iOS device in, open XCode, and do the following:
Window>Devices and Simulators>Select your App> Press the cog>Download container > Save > Navigate to the container >Right click >Show package contents>App Data>Documents. Your file will be there!
You could also upload the local OBJ to a cloud storage solution for easier access. I did this using Firebase, but that’s a post for another time.
A hacky code sample that might help you out:
//Get a reference to the SDMesh SDMesh sdMesh = FindObjectOfType<SDMesh>(); //combine the meshes MeshFilter[] meshFilters = sdMesh.GetComponentsInChildren<MeshFilter>(); CombineInstance[] combine = new CombineInstance[meshFilters.Length]; int i = 0; while (i < meshFilters.Length) { Debug.Log("combining.."+meshFilters[i].gameObject.transform.name); combine[i].mesh = meshFilters[i].sharedMesh; combine[i].transform = meshFilters[i].transform.localToWorldMatrix; i++; } Mesh combinedMesh = new Mesh(); combinedMesh.CombineMeshes(combine); //save the mesh OBJData data = combinedMesh.EncodeOBJ(); string filepath = Application.persistentDataPath + "/Obj_" + UnityEngine.Random.Range(0, 10000) + ".obj"; var lStream = new FileStream(filepath, FileMode.Create); OBJLoader.ExportOBJ(data, lStream); lStream.Close();
Originally published at https://medium.com on November 24, 2018.