OBJ file format when exporting from Blender

To export geometry from Blender to a separate OBJ file, we can select File – Export – Wavefront (obj) from the main menu and specify the path to save the .obj file.

We can also export geometry to OBJ format from Blender using the Blender Python API.

To export the currently selected object, for example – the default cube, we need to call the following operator:

The parameters for the operator are the full path with the filename to save the exported geometry to, and a flag “export_selected” indicating whether to export only the selected objects from the scene.

We can open the created .obj file in any text editor.

The export data is written to the .obj file in the following format:

The first lines contain a header comment indicating that this file was created in a specific version of Blender. A link to the official Blender website is also provided.

The next line specifies the name of the file where the textures applied to the mesh are saved.

Next comes the description of the exported object. First, its name, in a line starting with the “o” formant.

Next comes a set of lines starting with the “v” formant. Each line corresponds to one mesh point. Each line contains three values ​​separated by a space—the coordinates of that mesh point in global space.

For the exported default cube, the coordinates section will look like this: eight lines followed by the coordinates of the eight cube points.

Next comes the section, describing polygon normals. Each normal corresponds to one line statring with the “vn” formant . The number of lines corresponds to the number of polygons in the mesh. This section contains polygon normals if Flat Shading is used, or corner normals if Smooth Shading is used in the scene. The normal value is written as a normalized vector (the vector length is scaled to 1.0) and is measured from the origin.

For a cube with Flat Shading, this section would look like this:

Next comes the section for describing the UV mapping. Each line starts with the “vt” formant. The number of lines corresponds to the number of points on the mesh’s UV map. Each line describes the coordinates of one point on the UV map in 2D space.

For an automatically generated cube unwrap, the section will look like this:

Next comes a single line, starting with the “s” formant, and serving as a flag indicating whether Smooth Shading is enabled. If Flat Shading is used, the value in this line is 0; if Smooth Shading is used, the value is 1.

Finally, the last section of the .obj file describes the construction of the polygons.

Each line in this section starts with the “f” formant. The number of lines corresponds to the number of polygons in the mesh.

For our default cube, this section looks like this:

This section looks the most complex, but it’s actually quite simple.

Each line is a list of points that form the current polygon. The data for each point is separated by spaces.

The description of each point in the polygon is three values ​​separated by slashes. The first value is the point index in the vertex list (the “v” section). The second value is the point index in the UV unwrap point list (the “vt” section). And the third value is the normal index in the normal list (the “vn” section).

For example, in our default cube export file, the first value in the first line of the polygon sections is written as 1/1/1. This describes the first point of the first polygon of our cube. The first index, 1, links to the first line in the vertex section, meaning it gives us the coordinates of this vertex. The second index, 1, links to the first line in the unwrap section, meaning it gives us the coordinates of this point in 2D UV space. And the third index, 1, links to the first line in the normals section, meaning it gives us the normal vector for this polygon.

For the second point of the polygon, the value 2/2/1 is deciphered in exactly the same way. The second line in the vertex list gives us the coordinates in 3D space, the second line in the unwrap list gives us the coordinates for this polygon point in 2D UV space, and the first line in the normal list gives us the normal vector for this polygon.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted