Convert mesh to stl/obj/fbx in runtime

11,804

This is really complicated since you have to read the specifications for each each format (stl/obj/fbx) and understand them in order to make one yourself. Luckily, there are many plugins out there already that can be used to export Unity mesh to stl, obj and fbx.

FBX:

UnityFBXExporter is used to export Unity mesh to fbx during run-time.

public GameObject objMeshToExport;

void Start()
{
    string path = Path.Combine(Application.persistentDataPath, "data");
    path = Path.Combine(path, "carmodel"+ ".fbx");

    //Create Directory if it does not exist
    if (!Directory.Exists(Path.GetDirectoryName(path)))
    {
        Directory.CreateDirectory(Path.GetDirectoryName(path));
    }

    FBXExporter.ExportGameObjToFBX(objMeshToExport, path, true, true);
}

OBJ:

For obj, ObjExporter is used.

public GameObject objMeshToExport;

void Start()
{
    string path = Path.Combine(Application.persistentDataPath, "data");
    path = Path.Combine(path, "carmodel" + ".obj");

    //Create Directory if it does not exist
    if (!Directory.Exists(Path.GetDirectoryName(path)))
    {
        Directory.CreateDirectory(Path.GetDirectoryName(path));
    }

    MeshFilter meshFilter = objMeshToExport.GetComponent<MeshFilter>();
    ObjExporter.MeshToFile(meshFilter, path);
}

STL:

You can use the pb_Stl plugin for STL format.

public GameObject objMeshToExport;

void Start()
{
    string path = Path.Combine(Application.persistentDataPath, "data");
    path = Path.Combine(path, "carmodel" + ".stl");

    Mesh mesh = objMeshToExport.GetComponent<MeshFilter>().mesh;

    //Create Directory if it does not exist
    if (!Directory.Exists(Path.GetDirectoryName(path)))
    {
        Directory.CreateDirectory(Path.GetDirectoryName(path));
    }


    pb_Stl.WriteFile(path, mesh, FileType.Ascii);

    //OR
    pb_Stl_Exporter.Export(path, new GameObject[] { objMeshToExport }, FileType.Ascii);
}
Share:
11,804
Gareth Lam
Author by

Gareth Lam

Updated on June 04, 2022

Comments

  • Gareth Lam
    Gareth Lam almost 2 years

    I am looking for a way to export a mesh into stl/obj/fbx format at runtime and save it on local files of an Android phone.

    How do I do this? I am willing to use a plugin(free/paid) if it exist.

  • Gareth Lam
    Gareth Lam over 6 years
    Thanks for your advice. I have never thought of finding plugin in Github...... I will try all these plugins later today.
  • Gareth Lam
    Gareth Lam over 6 years
    I would like to develop an Android app(for a school project) that allows users to design their own phone cases and then generate 3D geometry files in STL/OBJ format, which is ready for 3D printing. (Sorry that I didnt make clear in the question.) Thats the main reason I need a plugin for exporting gameobjects/meshes to stl/obj/fbx files at runtime in Android app. I tried both pb_Stl and UnityFBXExporter in void Start() (codes you suggested). But when I turn on my app, and then I open the internal storage. I found that nothing was created.
  • Gareth Lam
    Gareth Lam over 6 years
    I would like to know could these export codes being implemented in other functions instead of void Start(). Because what I want to achieve is generating a stl file only when a specific button is clicked instead of the start of the app.
  • Programmer
    Programmer over 6 years
    All your comments are unnecessary. The code you see are just example usage and can be used or called anytime if you put them in an non Start function. You may need to call Directory.Exists and Directory.CreateDirectory before saving the model or you run into this problem. Also made a typo by putting ".fbx" as extension of all of them. Now, fixed it. See the edit. You can check if the saving worked with File.Exists or by reading the file with File.ReadAllBytes