Wavefront OBJ (.obj)
The Wavefront OBJ geometry format: a line-oriented ASCII format with v (vertices), vt/vn (texture/normal coordinates), f (faces) and g (groups).
| Format name | obj |
| Extensions | .obj |
| Read / Write | ✓ / ✓ |
| Extra dependencies | — |
Reading & writing
The native row parser views the live trimmed input row rather than copying it first; result arrays/group names remain owned. See the source-copy evidence, including the retained unfavorable writer observation; no writer speedup is claimed.
python
import meshioplusplus
mesh = meshioplusplus.read("model.obj")
meshioplusplus.obj.write("out.obj", mesh)write takes no keyword arguments.
File structure
Blank/#-comment lines are skipped.
v x y z— a point row (all tokens aftervare parsed as floats; extra columns are simply appended, not rejected).vn .../vt ...— vertex normal / texture-coordinate rows (arbitrary column count, stored raw).s 1/s off— smooth-shading toggle, ignored.f i1[/t1[/n1]] i2/... ...— a face; only the part before the first/of each token is used as the vertex index (1-based). All faces in a "run" must have the same vertex count; the run breaks (opening a new cell block) as soon as the count changes, or a newgline appears.g <name>— starts a new group and increments a running group-id counter (starting at -1, so faces before anygget group id -1). Only the group's running id is kept — the name string itself is not stored.- Any other keyword is silently ignored.
Empty trailing groups (e.g. a g line with no faces after it) are dropped after the full file is scanned.
Cell types
Faces are grouped by vertex count into triangle (3), quad (4), or polygon (else).
Data mapping
point_data["obj:vn"]— vertex normals, present only if anyvnlines were seen. When every corner names avn(f v//vn,f v/vt/vn), each point gets the normal its corners name, and a point whose corners name different normals (a hard edge) is split like avtseam, withobj:parent_pointnaming the original; the split is over the distinct(vt, vn)pairs. Both engines read it.point_data["obj:vt"]— texture coordinates, present only if anyvtlines were seen; whenuvis read (below), only if there is onevtrow per point.point_data["uv"]— texture coordinates by face corner: when every corner names avt(f v/vt,f v/vt/vn), each point gets theu, vits corners name. A point whose corners name differentvtrows (a UV seam) is split: the first copy keeps its index, the others are appended in the order met, andpoint_data["obj:parent_point"]names the point each copies (the convention of parameterize). Both engines read it.- Written: with
uvpoint data (one pair per point), onevt u vper point and every face corner asv/vt, in place of the rawobj:vtrows; without it, faces are plainf vas before. - Materials:
usemtlgroups are read as cell regions (one per material name), and themtlliblibraries beside the file give each material'sKdasfield_data["obj:material:<name>:Kd"]and the firstmap_Kdimage's bytes asfield_data["texture:png"](texture:jpegfor a JPEG). Written: each face's first cell region is itsusemtl(faces in none get one more material,material0, when there are materials or a texture), withmtllib <stem>.mtlnaming a.mtlbeside the file (Kdfrom the field data, else white) and, with a texture,map_Kd <stem>.pngand the PNG itself. Both engines. cell_data["obj:group_ids"]— one int array per cell block, theg-index that produced that block (-1if before the firstg).
Quirks & limitations
- A face with no texture index on some corner leaves
uvunset for the whole file, and one with no normal index on some corner leaves the rawobj:vnrows as the file has them. Rawobj:vt/obj:vnrows that are not one per point are kept as the file has them, which the PythonMeshrefuses (point data must have one row per point), exceptobj:vtonceuvholds the coordinates. - A new
gline always starts a fresh cell block, even if the vertex count is unchanged from the previous group — group boundaries and vertex- count changes both independently trigger a new block.
Notes
tests/python/meshes/obj/elephav.obj— a real triangle-mesh scan (vertex-onlyvlines plusftriangle lines).- Fully handled by the C++ core.