# Working with wavefront obj files

Mathematica can easily import obj shape files. I wonder, is it possible to access individual faces? What I actually need is to calculate area of each face and its normal. This can be done from scratch (import the file as text, parse it and calculate what I need by using vector cross product). But, I would hope that because Mathematica supports this file format, it would already have some tools available. Even if just accessing vertices coordinates of each facet would be rather helpful (so that I do not at least need to parse the file) or automatically creating Polygons from the obj file.

When you import an "obj" file, these are the import elements you have access to:

Import["ExampleData/seashell.obj", "Elements"]
(* {"BoundaryMeshRegion", "CoordinateTransform", "Graphics3D",
"GraphicsComplex", "InvertNormals", "LineData", "LineObjects",
"MeshRegion", "PointData", "PointObjects", "PolygonColors",
"PolygonData", "PolygonObjects", "Region", "Summary", "VertexData",
"VertexNormals", "VerticalAxis"} *)


So you can get the faces via

Import["ExampleData/seashell.obj", "PolygonObjects"] But they are all wrapped up in one polygon, so if you want properties of the individual polygons, then you need to split it up. After that, getting the area and surface normal is straightforward

pgons = Import["ExampleData/seashell.obj", "PolygonObjects"] /.
Polygon[a_] :> Polygon /@ a;
areas = Area /@ pgons;
normals = pgons /. Polygon[{a_, b_, ___, c_}] :> Normalize[Cross[b - a, c - a]];

• If the obj file is not only triangular faces but rather polygonal, it is sufficient to modify the last line to normals = pgons /. Polygon[{a_, b_, ___,c_}] :> Normalize[Cross[b - a, c - a]];? I am not very good with patterns yet so this is just a guess - the idea is that it should be the cross product of the second vertex - first vertex with last vertex - first vertex. Sep 29 '16 at 20:52
• @leosenko - That sounds right. I don't like it when these objects use polygons with more than three points though, because they aren't guaranteed to be coplanar to the degree of precision being used - I've often had MMA complain that the points aren't coplanar when making a MeshRegion. Sep 29 '16 at 21:02