Monday, September 13, 2010

Parsing Wavefront .obj using Python

Wavefront OBJ File Format Parsing in Python






I thought I would share some simple parsing information about the wavefront .OBJ file format using python. The thing I like about this format is that it is stored in plain text, and easy to use if you are writing simple 3D game engines, or just 3D modeling programs.

You can use this parser to load wavefront files using python, and possibly to view the wavefront obj file.

Overview of the wavefront .OBJ file format



Based on http://en.wikipedia.org/wiki/Obj

Basically, our approach is to go line by line through the file. If the line starts with a "v", we are dealing with a vertex. If the line starts with a "vt" then we are dealing with a texture coordinate (u, v, optionally w). "n" means normal. "f" means its a face index. These are a bit special, but not too difficult to grasp. Our exported models that are from blender will all have normal vectors, and texture coordinates (make sure you specify the texture coordinates in blender or there will be none). The "f" lines will look like this:


f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3


where "v1" is the vertex array index, "vt1" is the texture coordinate index, and "vn1" is the normals array index. This particular face is a triangle. I recommend storing triangles, and quads in 2 different arrays. They can both reference your vertex/texture coordinate/normals array.

Very important note before we begin



The index format in the .OBJ Wavefront file format is 1 based, not 0 based. Thus, we should subtract 1 from the actual number in order to get a 0 based index, and make it compatible with python lists or any array type.


#do the loading of the obj file
def load_obj(filename) :
V = [] #vertex
T = [] #texcoords
N = [] #normals
F = [] #face indexies

fh = open(filename)
for line in fh :
if line[0] == '#' : continue

line = line.strip().split(' ')
if line[0] == 'v' : #vertex
V.append(line[1:])
elif line[0] == 'vt' : #tex-coord
T.append(line[1:])
elif line[0] == 'vn' : #normal vector
N.append(line[1:])
elif line[0] == 'f' : #face
face = line[1:]
if len(face) != 4 :
print line
#raise Exception('not a quad!')
continue
for i in range(0, len(face)) :
face[i] = face[i].split('/')
# OBJ indexies are 1 based not 0 based hence the -1
# convert indexies to integer
for j in range(0, len(face[i])) : face[i][j] = int(face[i][j]) - 1
F.append(face)

return V, T, N, F


Please not this program will give you string representations. You should loop through these arrays again and convert them to the proper format (int, float, etc).

Please leave your comments

No comments:

Post a Comment