Export Unity 3D models to Excel

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

We are working on some Pattern Recognition algorithms for HoloLens. Because of this we have to often use ideally created holograms as reference data for Machine Learning. Following very simple script creates menu item “ExportVertices” under menu Assets.  If you want to export vertices and triangles of a hologram, put tag on it with name ‘exportable’ and function below will export data for all tagged holograms.

Function will export two files. First one contains vertices and se3cond one contains triangles.

using System;
using
System.Collections.Generic;
using
System.IO;
using
System.Linq;
using
System.Text;
using
UnityEditor;
using
UnityEngine;

namespace
Assets.Editor
{
   
public class Extractor

    {
        [
MenuItem("Assets/ExportVertices")]
       
static void
ExportVerticesAsCsv()
        {
           
Debug.Log("Exporting all GameObjects with tag 'exportable'"
);

           
var objects = GameObject.FindObjectsOfType(typeof(GameObject
));
           
foreach (GameObject item in
objects)
            {
               
if (item.tag == "exportable"
)
                {
                   
Debug
.Log(item.name);
                    exportAsCSV(item);
                }
            }
        }


       
private static void exportAsCSV(GameObject
item)
        {
           
string fNameV = String.Format("{0}_vertices.csv"
, item.name);
           
string fNameT = String.Format("{0}_triangles.csv"
, item.name);

           
var meshFilt = item.GetComponent<MeshFilter
>();
           
if (meshFilt != null
)
            {
               
string path = Path.Combine(Application
.dataPath, fNameV);

               
Debug.Log("Exporting triangles to: "
+ path);

               
using (StreamWriter sw = new StreamWriter
(path))
                {                
                    sw.WriteLine(
String.Format("Header: rows = {0}"
,
                     meshFilt.sharedMesh.triangles.Length));

                   
foreach (var triangle in
meshFilt.sharedMesh.triangles)
                    {
                        sw.WriteLine(triangle);
                    }
                }

                path =
Path.Combine(Application
.dataPath, fNameT);

               
Debug.Log("Exporting verticies to: "
+ path);

               
using (StreamWriter sw = new StreamWriter
(path))
                {
                    sw.WriteLine(
String.Format("Header: x,y,z, rows: {0}"

                    meshFilt.sharedMesh.vertices.Length));

                   
foreach (var vert in
meshFilt.sharedMesh.vertices)
                    {
                        sw.WriteLine(
String.Format("{0},{1},{2}"
,
                        vert.x, vert.y, vert.z));
                    }
                }
            }
           
else

               
Debug.Log("No mesh found on object :("
);
        }
    }
}





Posted Jan 03 2017, 06:39 PM by Damir Dobric
Filed under:
developers.de is a .Net Community Blog powered by daenet GmbH.