Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Here is a sample Graphics3D object:

gr1 = Graphics3D[
  GeometricTransformation[
   {Red, Sphere[]}, 
   Table[TranslationTransform[{i, 0, 0}], {i, 10}]
  ]
]

enter image description here

How do I apply different colors (or other directives) when using GeometricTransformation?

I get something like the following effect

gr2 = Graphics3D[
  GeometricTransformation[{RGBColor @@ RandomReal[{0, 1}, {3}], Sphere[]}, #] & /@ 
  Table[TranslationTransform[{i, 0, 0}], {i, 10}]
]

enter image description here

So, how to change gr1 to gr2?

Normal@gr1[[1]] does not change it to many spheres.

share|improve this question
2  
Please do not use block quotes for code, instead indent each line in the code block by 4 spaces. To format inline code surround it by grave (`) symbols. Do not use $\LaTeX$ to mark up code. As you have been here a good while, I will let you reformat the last code block. –  rcollyer Jun 11 '13 at 17:03
    
@rcollyer well, I just learn to use that @@ mark-down, I'wll edit it. –  HyperGroups Jun 11 '13 at 17:08
2  
@HyperGroups Well, every line requires it if it belongs in the code block. But, if you use the Code Sample button (or shortcut), you need to select all of the code as it only moves the text you've selected. –  rcollyer Jun 11 '13 at 17:20
1  
btw, you can find some editing help here: mathematica.stackexchange.com/editing-help –  rm -rf Jun 11 '13 at 17:49
2  
Please don't use blockquote. You can use <pre><code> though. –  rm -rf Jun 12 '13 at 2:20
show 3 more comments

1 Answer

up vote 3 down vote accepted

How about this?

gr1 /. gt_GeometricTransformation :> 
   Map[GeometricTransformation[First[gt], #] &, 
    Last[gt]] /. {_RGBColor :> RGBColor @@ RandomReal[{0, 1}, 3]}

Colored spheres

Edit: Alternative way:

gr1 /. gt_GeometricTransformation :> Thread[gt, List, -1] /.
 {_RGBColor :> RGBColor @@ RandomReal[{0, 1}, 3]}

Edit 2: To convert GeometricTransformation[..] to graphics primitives and directives:

gr1 /. gt_GeometricTransformation :> 
   Map[Normal @ GeometricTransformation[First[gt], #] &, 
       Last[gt]] /. {_RGBColor :> RGBColor @@ RandomReal[{0, 1}, 3]}
share|improve this answer
    
nice+1, can we normal the gr1 to Graphics3D[] without GeometricTransformation? –  HyperGroups Jun 12 '13 at 1:38
    
@HyperGroups gr1 is already Graphics3D. I think you're asking for a function like Normal to turn a GeometricTransformation of a list into a list of GeometricTransformations. I don't think there is a built-in function to do that, and you would have to use something like Map the way I did. –  Michael E2 Jun 12 '13 at 1:55
    
I mean In gr1, we still have GeometricTransformation in the FullForm list. But not convert one sphere to 5 spheres each with new coordinates. –  HyperGroups Jun 12 '13 at 2:42
    
Ah, I see. Take Normal of the GeometricTransformation[..] -- see the edit. –  Michael E2 Jun 12 '13 at 3:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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