Exporting ofMesh to Blender/Unity

I feel like I may be missing something obvious here. I have procedurally built a mesh in openFrameworks, but now I want to figure out a way to get it into Blender or Unity. The ofMesh method for saving a mesh is just for saving/loading within oF as far as I can tell from reading the source code.

I didn’t see any addons that were geared towards exporting 3D models. Do I have to either a) write a blender python script to read in the oF mesh file format or b) learn some 3d model file format like COLLADA and write oF code to save my mesh as a .dae?

I think ofMesh saves to .ply format? You should be able to find an importer plugin for Blender and Unity that will load .ply files. Otherwise, have a search for a .obj or .stl exporter for OF- have you looked through http://ofxaddons.com/

Hey Mike

Here’s some code I’ve used in the past for exporting ofMesh objects in the form of a large sequence of .obj files. It’s been modified from some code I found in one of Kyle’s projects.

My function was little messy. I’ve tidied away most of the noise for you but not tested it so be prepared for it to break. There should be enough to get you started. Vertices and indices are saved but not normals. There is a commented line in there that should point you in the right direction.

  
  
void testApp::exportObjMesh()  
{  
    ofstream obj;  
      
    string newObjPath = "myDir/myOBJ.obj";  
      
    obj.open(ofToDataPath(newObjPath).c_str(), ios::out);  
      
    obj << "#  ========================================================================== \n"  
    << "#  OBJ exported by James Alliban \n"  
    << "#  Application built using openFrameworks \n"  
    << "#  ========================================================================== \n";  
      
    if (obj.is_open())   
    {  
        for (int i = 0; i < myMesh.getVertices().size(); i++)   
        {  
            obj << "v " << myMesh.getVertex(i).x << " " << myMesh.getVertex(i).y << " " << myMesh.getVertex(i).z << endl;  
        }  
          
        for (int i = 0; i < myMesh.getIndices().size() / 3; i++)   
        {  
  
            obj << "f " << myMesh.getIndex(i*3) + 1 << " " << myMesh.getIndex(i*3+1) + 1 << " " << myMesh.getIndex(i*3+2) + 1 << endl;  
        }  
          
        //normal  
        //obj << "vn " << x << " " << y << " " << z << endl;  
          
        if (myMesh.getVertices().size() == 0 || myMesh.getIndices().size() == 0)  
        {  
            obj << "v " << 0 << " " << 1 << " " << 2 << endl;  
            obj << "v " << 2 << " " << 3 << " " << 4 << endl;  
            obj << "v " << 4 << " " << 5 << " " << 6 << endl;  
            obj << "f " << 1 << " " << 2 << " " << 3 << endl;  
        }  
    }  
      
    myMesh.clear();  
}  
  
  

Trent - yeah that was the first place I looked. Unfortunately, I had only checked the graphics category - which had nothing in the way of exporting. I went back to the main page, and it turns out there is an stl importer/exporter addon (https://github.com/obviousjim/ofxSTL) that just isn’t tagged as graphics. That might be an option. But thank you for pointing out the .ply format! That at least gets me up and running with getting something into Unity immediately (even if the normals are horrible).

James - awesome, thanks! This should definitely help.

meshlab is a great software for converting between formats among other things, it should open ply and export to something that blender can open, it also can help with fixing the normals

1 Like

arturo, thanks! Blender can actually handle .ply, but I’m going to look into fixing the normals in meshlab.

hi there,

picking up this some years later - does anyone have any advice on exporting to .ply or .obj with textures to take into unity from oF? I have meshes with correctly bound textures rendering in oF and
I have both obj and ply export working fine but cannot figure out how to make texture registration export work - nor can i get my head round how to manually add the textures correctly in meshLab.

scratching my head after much searching and any advice gratefully received !

thx

d