Any comments to Philippe Serbruyns: or twitter @phiser678.

home search Twitter

Animation with OpenSCAD

I came across an interesting Parametric Bubble Vase from sphynx on Thingiverse for 3D printing. He provided the source code, so you can experiment with your own parameters a bit. My idea was, if you want to make an animation from different angles, can it be done with OpenSCAD? Well, yes it can!

This is what we did.

Generate a PNG from the vase

It turns out OpenSCAD can render your object from any camera angle. Let's look at the help first:

$ openscad -h
Usage: openscad [ -o output_file [ -d deps_file ] ]\
                [ -m make_command ] [ -D var=val [..] ] \
                [ --help ] print this help message and exit \
                [ --version ] [ --info ] \
                [ --camera=translatex,y,z,rotx,y,z,dist | \
                  --camera=eyex,y,z,centerx,y,z ] \
                [ --autocenter ] \
                [ --viewall ] \
                [ --imgsize=width,height ] [ --projection=(o)rtho|(p)ersp] \
                [ --render | --preview[=throwntogether] ] \
                [ --colorscheme=[Cornfield|Sunset|Metallic|Starnight|BeforeDawn|Nature|DeepOcean] ] \
                [ --csglimit=num ]\
                filename

So, this should make sence now:

openscad TwistedBubbleVase002-0000.png TwistedBubbleVase002.scad --preview --camera=0,0,0,45,55,655

This makes a render from the vase from a 45 degree angle from above, and rotates it 55 degrees from a distance of 655 milimeter. To make the animation from above we will use a bash script:

for j in `seq 0 899`;do
 i=`printf "%04d" $j`
 openscad -o TwistedBubbleVase002-$i.png TwistedBubbleVase002.scad --preview --camera=0,0,0,45,0,$j,`expr 1000 - $j`
done

This will generate 900 PNG rendered files numbered from 0000-0899, which goes rather quickly, even on an older PC. It will rotate continuously, while the camera moves closer to the vase. The next in action is to reverse the rotation and camera distance. We just reverse the generated files with softlinks and add 900 to the numbering.

j=899
for z in TwistedBubbleVase002-0[0-8]??.png;do
 i=`printf "%04d" $(expr 900 + $j)`
 ln -s $z TwistedBubbleVase002-${i}.png
 j=$[$j-1]
done

Generate the mp4 from the PNG files

Now that we have all the frames, we can use ffmpeg to make the movie:

ffmpeg -f image2 -i TwistedBubbleVase002-%04d.png -vcodec libx264 -b 1M -pix_fmt yuv420p -y -r 60 vase1.mp4

This will create the video from the TwistedBubbleVase002*.png images with a x264 codec with a 1mb/s bitrate and 420p colorspace at 60 frames per seconds. The result can be seen above. Hope you enjoyed this tutorial.