There are several types of definitions in an OBJ file. Let's cover them line-by-line with a simple example. We are going to dissect a sample square.obj file that we will export from the Blender file called square.blend. This file represents a square divided into two parts, one painted red and the other painted blue, as shown in the following diagram:
When we export Blender models to an OBJ format, the resulting file normally starts with a comment:
# Blender v2.62 (sub 0) OBJ
File: 'squares.blend'
# www.blender.org
These are comments, and they are denoted with a hash # symbol at the beginning of the line.
Next, we will usually find a line referring to the Material Template Library that this OBJ file is using. This line will begin with the keyword mtllib, followed by the name of the material's file:
mtllib square.mtl
There are several ways that geometries can be grouped into entities in an OBJ file. We can find lines starting with the prefix o, followed by the object name, or by the prefix g, followed by the group name:
o squares_mesh
After object declaration, the following lines will refer to vertices, v, optionally to vertex normals, vn, and texture coordinates, vt. It’s important to note that vertices are shared by all groups in an object in the OBJ format. That is, you will not find lines referring to vertices when defining a group, because it's assumed that all vertex data was defined when the object was defined:
v 1.0 0.0 -2.0
v 1.0 0.0 0.0
v -1.0 0.0 0.0
v -1.0 0.0 -2.0
v 0.0 0.0 0.0
v 0.0 0.0 -2.0
vn 0.0 1.0 0.0
In our case, we have instructed Blender to export group materials. This means that each part of the object that has a different set of material properties will appear in the OBJ file as a group. In this example, we are defining an object with two groups (squares_mesh_blue and squares_mesh_red) and two corresponding materials (blue and red):
g squares_mesh_blue
If materials are being used, the line after the group declaration will be the material that's being used for that group. In this case, only the name of the material is required. It's assumed that the material properties for this material are defined in the MTL file that was declared at the beginning of the OBJ file:
usemtl blue
The lines that begin with the prefix s refer to smooth shading across polygons. Although mentioned here, we will not be using this definition when parsing the OBJ files into JSON files:
s off
The lines that start with f refer to faces. There are different ways to represent faces. Let's see them.