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.


1 Answer 1


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"]

enter image description here

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]];
  • $\begingroup$ 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. $\endgroup$
    – atapaka
    Sep 29, 2016 at 20:52
  • $\begingroup$ @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. $\endgroup$
    – Jason B.
    Sep 29, 2016 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.