# How to color objects from a single .obj file?

How to paint the dolphin, shark and the box with different colors after loading the .obj file into Mathematica?

gr = Import["c:\\DolphinSharkBox.obj" ];
Graphics3D[gr[[1]]]


Link for the .obj file.

## 1 Answer

The imperceptiple smile of your shark hides 12 triples of teeth and a pair eyes each with 288 lenses!

I use the method in the update to this answer as is:

grc = Import["... \\DophinSharkBox.obj",  "GraphicsComplex"];
Graphics3D[grc]


Producing the graph takes a while (perhaps could be faster with Pick instead of Select):

polygonsc = Join @@ (Normal[grc] /.
HoldPattern[VertexNormals -> _] :> Sequence[]);
polygonsc = polygonsc /. Polygon -> Sequence;
edgesc =  DeleteDuplicates[UndirectedEdge @@@ Map[ToString,
Select[Subsets[polygonsc, {2}], Intersection @@ # =!= {} &], {-2}]];


Expecting three connected components we get 41:

cc =  Map[ToExpression, ConnectedComponents[Graph[edgesc]], {-1}];
Through@{Length@# &, Tally[Length /@ #] &}@ cc
(* {41, {{3804, 1}, {952, 1}, {288, 2}, {38, 24}, {28, 12}, {6, 1}}} *)

Graph[edgesc, VertexStyle -> White, VertexSize -> .005, VertexShapeFunction -> "Point",
EdgeStyle -> Directive[Opacity[.5], Thin, Hue[.35, .5, .8]],
Background -> Black, EdgeShapeFunction -> (Line[#1] &), ImageSize -> 600]


Graphics3D[({EdgeForm[], Hue[RandomReal[]], Polygon@#} & /@ cc), Lighting ->"Neutral"]


Two largest components:

Graphics3D[({EdgeForm[], Hue[RandomReal[]], Polygon@# }&/@cc[[;;2]]), Lighting -> "Neutral"]


and the anatomic details at the top picture produced by

Graphics3D[({EdgeForm[],Hue[RandomReal[]],Polygon@#}&/@ cc[[3;;-2]]), Lighting -> "Neutral"]


Note: I eliminated VertexNormals to focus on the polygon faces. The method can be modified to keep the VertexNormals to produce smoother surfaces.

• Much can be learned from your answers, Thanks again. I just wondered, is your method can be modified to produce a UV mapping for use of 3d model texturing in Mathematica? Commented Sep 5, 2014 at 5:34
• Thank you @Putterboy. I need to google what UV mapping is :)
– kglr
Commented Sep 5, 2014 at 5:44
• ... ah, I see. Perhaps ListSurfacePlot3D as in Texture>>Applications>>Marble Bunny could work for the bodies of your shark and dolphin.
– kglr
Commented Sep 5, 2014 at 6:00
• The reason why I had such idea came up because the graph images of your solution look very much the same as the spreaded mesh that the 3D guys used for their photo realistic texturing. The 3D guys cut and spread their modeled object's mesh flat, and then paste a photo image of the real object to the various mesh parts of the object accordingly, in order to obtain a photo realistic looking of their final model. Commented Sep 5, 2014 at 8:20