4
$\begingroup$

I'm using a dynamic module to rotate a Graphics3D at a certain rate:

autoRotate[gr_Graphics3D, rate_: 5] := 
 DynamicModule[{vp, va, vv, vc}, {vp, va, vv, vc} = 
   gr~AbsoluteOptions~#~OptionValue~# &@{ViewPoint, ViewAngle, 
     ViewVertical, ViewCenter};
  Overlay[{Show[gr, SphericalRegion -> True, ViewPoint -> Dynamic[vp],
      ViewAngle -> Dynamic[va], ViewVertical -> Dynamic[vv], 
     ViewCenter -> Dynamic[vc]], 
    Show[gr, SphericalRegion -> True, Boxed -> False, 
     ViewPoint -> 
      Dynamic[RotationMatrix[Clock[2 \[Pi], rate], vv].vp], 
     ViewAngle -> Dynamic[va], ViewVertical -> Dynamic[vv], 
     ViewCenter -> Dynamic[vc]]}, All, 1]]

And it works great. But now, I want to export the produced dynamic graphics object as a gif or mp4. How can I change the function definition to export as a gif?

$\endgroup$
  • $\begingroup$ For generating animations, I usually generates a list of frames with Table function, and I export the gif as Export["animation.gif",framesList]. This doesn't answer exactly to your question, but I hope it may be of some help if you don't find a more straightforward solution :D $\endgroup$ – Fraccalo Jun 7 '18 at 16:06
  • $\begingroup$ mathematica.stackexchange.com/q/27202/5478 $\endgroup$ – Kuba Jun 7 '18 at 16:37
4
$\begingroup$

Below ImagePadding -> 30 in the 3D graphic ensures that the rotation doesn't affect the size of the graphic. I'm not sure thats necessary for your graphic.

autoRotate[gr_Graphics3D, i_] := Module[{vp, va, vv, vc}, {vp, va, vv, vc} = 
 gr~AbsoluteOptions~#~OptionValue~# &@{ViewPoint, ViewAngle, ViewVertical, ViewCenter};
 Overlay[{Show[gr, SphericalRegion -> True, ViewPoint -> vp, ViewAngle -> va,
                                         ViewVertical -> vv, ViewCenter -> vc], 
  Show[gr, SphericalRegion -> True, Boxed -> False, ViewPoint -> RotationMatrix[i, vv].vp,
    ViewAngle -> va, ViewVertical -> vv, ViewCenter -> vc]}, All, 1]]

graphic = Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3}, ColorFunction -> "Rainbow",
                 Mesh -> None, ImagePadding -> 30];

frames = Table[Rasterize[autoRotate[graphic, i], "Image"], {i, 0, 2 π - #, #}] &[2 π/160];

Export["ani.gif", frames, "AnimationRepetitions" -> ∞, "ColorMapLength" -> 64,
 "DisplayDurations" -> ConstantArray[1/18, Length[frames]], Dithering -> "FloydSteinberg"]

$\endgroup$
  • $\begingroup$ I have the first frame being shown as background for the rest of them, is that supposed to happen? $\endgroup$ – Vsevolod A. Jun 7 '18 at 21:03

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.