4

How can I convert a Wavefront's .obj file to a .off file ?

Gabriel Devillers
  • 3,155
  • 2
  • 30
  • 53

3 Answers3

3

You can use the open source GUI software Meshlab.

  • File > Import Mesh (Ctrl-I)
  • File > Export Mesh As and chose "Object file format (.off)"
Gabriel Devillers
  • 3,155
  • 2
  • 30
  • 53
  • 4
    If you prefer command line, meshlab is bundled with a command line version (meshlabserver) that can just do the same on a CLI: `meshlabserver -i input.obj -o output.off ` (_eventually also performing some filtering by specifying a script file..._) – ALoopingIcon Feb 10 '17 at 10:42
2

You can use the CLI, closed source, binary only, meshconv

chmod u+x meshconv
./meshconv input.obj -c off -o output.off

Howether the result seems to be a bit different from what I get in my answer using Meshlab because I could not load the resulting .off file in CGAL (the error look like this one).

Gabriel Devillers
  • 3,155
  • 2
  • 30
  • 53
-1

This should work for triangular meshes

def conv_obj(file):
    x = open(file)    
    k = 0
    while "\n" in x.readline():
        k += 1
    x = open(file)
    out = str()
    v = 0
    f = 0
    for i in range(k) :
        y = x.readline().split()
        if len(y) > 0 and y[0] == "v" :
            v += 1
            out += str(y[1]) + " " + str(y[2]) + " " + str(y[3]) + "\n"
        if len(y) > 0 and y[0] == "f" :
            f += 1
            out += "3 " + str(int(y[1])-1) + " " + str(int(y[2])-1) + " " + str(int(y[3])-1) + "\n"
    out1 = "OFF\n" + str(v) + " " + str(f) + " " + "0" + "\n" + out
    w = open(file.strip("obj") + "off", "w")
    w.write(out1)
    w.close()
    x.close()
    return "done"
Gabe
  • 1
  • 1