How can I colorize an imported model from for example 3ds max?

I'm trying to find the way to colorize a 3D plot from an imported model with something like this:

ColorFunction -> Function[{x, y, z}, ColorData["GreenBrownTerrain"][z^2]]

But I don't know how. Thank you very much for your help.

  • 1
    $\begingroup$ Example model: Import["ExampleData/747.3ds.gz"] it returns a Graphics3D $\endgroup$
    – ssch
    Jan 5 '13 at 13:24
  • $\begingroup$ Following ssch's suggestion, you can explore the data structure with jet = Import["ExampleData/747.3ds.gz"]; {jet[[0]], jet[[1]]} $\endgroup$
    – DavidC
    Jan 5 '13 at 14:06
  • $\begingroup$ It consists of a bunch of different GraphicsComplex objects: Graphics3D[{EdgeForm[], Table[jet[[1, 2, i]] /. RGBColor[_] -> ColorData[1][i], {i, 1, 7}]}] Kind of annoying structure to change color of. $\endgroup$
    – ssch
    Jan 5 '13 at 14:11

Here I color the 747 model via the VertexColors option to GraphicsComplex.

jet = Import["ExampleData/747.3ds.gz"];
(* Function to change color of GraphicsComplex *)
newcolor[gcomplex_, colfun_] := Module[{
   pts = First@gcomplex,
   data = gcomplex[[2]],
  (* Remove old RGBColor stuff in data part of the graphics complex *)
  data = DeleteCases[data, RGBColor[_]]; 
  (* Introduce vertex colors according to colfun *)
  vcol = colfun /@ pts;
  (* Return a new GraphicsComplex with VertexColors *)
  GraphicsComplex[pts, data, VertexColors -> vcol]
(* Find range of z values *)
{zmin, zmax} = {Min@#, Max@#} &@(Last /@ 
 Flatten[Cases[jet, GraphicsComplex[__], Infinity][[All, 1]], 1]);
(* Color function that maps {x,y,z} to color *)
cf = ColorData["GreenBrownTerrain"][Rescale[Last@#, {zmin, zmax}]^2] &
(* Change all GraphicsComplex objects *)
jet /. gc_GraphicsComplex :> newcolor[gc, cf]

recolored jet

There are some visible artefacts in the coloring, but at least it's something.

Comment if there's some part you'd like me to explain further.

Here's the before/after for ExampleData[{"Geometry3D","Phobos"}]

rock in space

  • $\begingroup$ Thank you, I'm going to try....Ok, it's working. tank you very much. $\endgroup$
    – user5288
    Jan 5 '13 at 15:57
  • $\begingroup$ If I want to do a generic function in function of x,y,z for color, what I must do? $\endgroup$
    – user5288
    Jan 5 '13 at 16:37
  • $\begingroup$ @user5288 something like cf[{x_,y_,z_}]:= ... or change vcol = colfun /@ pts; to vcol = colfun@@#& /@ pts; and use cf[x_,y_,z_]:=... $\endgroup$
    – ssch
    Jan 5 '13 at 16:43
  • $\begingroup$ I can't do the generic function, I was trying all the afternoon, but I can, my objetive is colorize a beam in function of the stress, and I need a function in x,y,z $\endgroup$
    – user5288
    Jan 5 '13 at 18:26
  • $\begingroup$ @user5288 What have you tried and how does it fail? $\endgroup$
    – ssch
    Jan 5 '13 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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