Function Repository Resource:

ExportRotatingGIF

Source Notebook

Export 3D graphic objects as spinning animations in GIF format

Contributed by: Michael Sollami

ResourceFunction["ExportRotatingGIF"]["dest.gif",graphics]

exports an animated GIF file of graphics cyclically rotating in three dimensions.

Details and Options

ResourceFunction["ExportRotatingGIF"] accepts various three dimensional graphics objects including Graphics3D, Graph3D, MeshRegion, ContourPlot3D, etc.
In the resulting GIF, the default "FPS" option is 30 frames-per-second.
The total number of frames is OptionValue["FPS"]×OptionValue["Duration"]
The option "Duration" is the time in seconds that a single 360° rotation will take to complete.
The following options are accepted:
"FPS"36frames per second to render
"Parallel"Falsewhether to use parallel kernels
"Axis"z"which axis to rotate around
"InitialViewPoint"{3,0,0}initial view point
"Graphics3DOptions"{}options to pass to Graphics3D
"FramesDirectory"Automaticdirectory to store frames making up the GIF
"Duration"3time (in seconds) for one rotation

Examples

Basic Examples (2) 

Export 3D plots as animations:

In[1]:=
ResourceFunction["ExportRotatingGIF"]["example.gif", BubbleChart3D[RandomReal[1, {5, 10, 4}]]];
Import[%, "Animation", ImageSize -> Small]
Out[1]=

Export Graphics3D objects to animated GIF file:

In[2]:=
g = Graphics3D[{Blue, Cylinder[], Red, Sphere[{0, 0, 2}], Black, Thick, Dashed,
    Line[{{-2, 0, 2}, {2, 0, 2}, {0, 0, 4}, {-2, 0, 2}}], Yellow,
    Polygon[{{-3, -3, -2}, {-3, 3, -2}, {3, 3, -2}, {3, -3, -2}}],
    Green, Opacity[1], Cuboid[{-2, -2, -2}, {2, 2, -1}]}];
ResourceFunction["ExportRotatingGIF"]["example.gif", g, ImageSize -> 400];
Import[%, "Animation"]
Out[2]=

Scope (4) 

Use ExportRotatingGIF to spin 3D plots of molecules around x-, y- and z-axes:

In[3]:=
m = Molecule["O=C(C1CCC1)S[C@@H]1CCC1(C)C"];
mp = MoleculePlot3D[m];
Table[ResourceFunction["ExportRotatingGIF"]["axis_" <> a <> ".gif", mp, "Axis" -> a], {a, {"x", "y", "z"}}];
Import[#, "Animation", ImageSize -> Small] & /@ %
Out[3]=

Export spinning Graph3D objects:

In[4]:=
gp = Graph3D[Table[i -> Mod[i^2, 74], {i, 100}]];
ResourceFunction["ExportRotatingGIF"]["graph_network.gif", gp, "FPS" -> 10, "Duration" -> 1, "Axis" -> "Y"];
ListAnimate[Import[%], ImageSize -> Small]
Out[4]=

Export a styled graph animation that revolves once every second:

In[5]:=
h = Graph3D[
   Table[Property[
     v, {VertexSize -> 0.2 + 0.2 Mod[v, 5], VertexStyle -> Hue[v/15, 1, 1]}], {v, 0, 14}], Table[v \[UndirectedEdge] Mod[v + 1, 15], {v, 0, 14}]];
ResourceFunction["ExportRotatingGIF"]["example.gif", h, "FPS" -> 30, "Duration" -> 1, "InitialViewPoint" -> {2, 2, 1}];
Import[%, "Animation", ImageSize -> Small]
Out[5]=

Export a MeshRegion object with axes and lighting:

In[6]:=
mr = DelaunayMesh[RandomReal[1, {50, 3}]];
ResourceFunction["ExportRotatingGIF"]["mesh_lighting.gif", mr, "Graphics3DOptions" -> {
    Lighting -> {{"Point", Green, {0, 0, 10}}, {"Point", Blue, {0, 10, 0}}}, Axes -> True}, ImageResolution -> 150];
ListAnimate[Import[%], ImageSize -> Small]
Out[6]=

Options (7) 

Axis (1) 

Specify the plane of rotation about one of three axes:

In[7]:=
ResourceFunction["ExportRotatingGIF"]["example.gif", g, "Axis" -> "z"];
ListAnimate[Import[%], ImageSize -> Small]
Out[7]=
In[8]:=
ResourceFunction["ExportRotatingGIF"]["example.gif", g, "Axis" -> "y"];
ListAnimate[Import[%], ImageSize -> Small]
Out[8]=
In[9]:=
ResourceFunction["ExportRotatingGIF"]["example.gif", g, "Axis" -> "x"];
ListAnimate[Import[%], ImageSize -> Small]
Out[9]=

Duration (1) 

Export a GIF that completes a 360° spin in two seconds:

In[10]:=
Graphics3D[{Specularity[White, 20], Texture[RadialGradientImage[ColorData["Rainbow"], {128, 128}]], EdgeForm[
    Opacity[1/
      8]], (Append[#1, {VertexTextureCoordinates -> With[{n = Length[First[#1]]}, Table[1/2 {Cos[2 \[Pi] i/n], Sin[2 \[Pi] i/n]} + {1/2, 1/2}, {i, 0, n - 1}]]}] &) /@ Flatten[PolyhedronData["StellaOctangula", "Faces", "Polygon"]]}, Lighting -> "Neutral", Boxed -> False];
ResourceFunction["ExportRotatingGIF"]["example.gif", %, "Duration" -> 2, "InitialViewPoint" -> {1, 3, 2}];
Import[%, "Animation"]
Out[10]=

FPS (1) 

Control the smoothness of the animation by changing the frames per second:

In[11]:=
g = Graph3D[ButterflyGraph[4]];
Table[ResourceFunction["ExportRotatingGIF"][
   "fps_" <> ToString[f] <> ".gif", g, "FPS" -> f], {f, {5, 30}}];
Import[#, "Animation", ImageSize -> Tiny] & /@ %
Out[11]=

FramesDirectory (1) 

Set a directory in which to save the images of the intermediate frames:

In[12]:=
g = Graph3D[HypercubeGraph[4]];
dir = CreateDirectory[];
ResourceFunction["ExportRotatingGIF"]["HypercubeGraph.gif", g, "FramesDirectory" -> dir, "FPS" -> 3, "Duration" -> 2];
FileNames["*.png", dir] // TableForm
Out[13]=

Graphics3DOptions (1) 

Include additional options to be exported with a graphics complex:

In[14]:=
gp = GraphPlot3D[HypercubeGraph[6], GraphLayout -> "SpringElectricalEmbedding", PlotTheme -> "CoolColor"];
ResourceFunction["ExportRotatingGIF"]["example.gif", gp, "Axis" -> "Z", Duration -> 4, "Graphics3DOptions" -> {Background -> Darker@Purple}];
Import[%, "Animation", ImageSize -> Small]
Out[14]=

InitialViewPoint (1) 

Modify the initial view point of the animation:

In[15]:=
cp = ContourPlot3D[ Cos[x] Sin[y] + Cos[y] Sin[z] + Cos[z] Sin[x] == 0, {x, -2 \[Pi], 2 \[Pi]}, {y, -2 \[Pi], 2 \[Pi]}, {z, -2 \[Pi], 2 \[Pi]},
   Axes -> True, PerformanceGoal -> "Speed"];
viewpoints = {{2, 2, -3}, {2, 2, 3}};
Table[ResourceFunction["ExportRotatingGIF"][
   "vp_" <> ToString[f] <> ".gif", cp, "FPS" -> 5, "InitialViewPoint" -> viewpoints[[f]]], {f, 2}];
Import[#, "Animation", ImageSize -> Small] & /@ %
Out[15]=

Parallel (1) 

The "Parallel" option speeds up frame generation (e.g. an n-fold speed up for n CPUs):

In[16]:=
g = Graph3D[HypercubeGraph[6]]
Out[16]=
In[17]:=
AbsoluteTiming[
  ResourceFunction["ExportRotatingGIF"]["example.gif", g, "Parallel" -> True]][[1]]
Out[17]=
In[18]:=
AbsoluteTiming[
  ResourceFunction["ExportRotatingGIF"]["example.gif", g]][[1]]
Out[18]=

Applications (1) 

Export a highlighted graph spun about the z axis:

In[19]:=
g = Graph3D@
   AdjacencyGraph[Import["LinearAlgebraExamples/Data/can__229.psa"]];
HighlightGraph[g, Subgraph[g, #] & /@ FindGraphPartition[g, 4]];
ResourceFunction["ExportRotatingGIF"]["example.gif", %];
Import[%, "Animation"]
Out[19]=

Publisher

Michael Sollami

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 01 March 2019

Related Resources

License Information