//js/thickbox/thickbox.css" type="text/css" media="screen" />

Auto-Rendering STL Files to PNG

Posted by adam on Tuesday Dec 1, 2009 Under Uncategorized

z_axis_arms

For the past few months I’ve been messing around with django and writing a web-based inventory system thing for Hive76. One of my goals was to auto-generated nice renders of STL files people attach a-la Thingiverse. Sure, I could have just asked Zach, but where’s the fun in that? Since I don’t know a damn thing about 3D modeling or any of the tools used for this, it ended up taking me a lot longer than I expected. The result is this very short shell script, with all of the work being done by stl2pov and povray, with a little help from the Axes and Grid Macro to pretty things up a bit. Here is the povray template I’m using and here is the shell script:

#!/bin/bash

STL=$1
BASEFILE=`echo "$STL" | sed -e 's/\.stl//g'`
OUTFILE=${BASEFILE}.png
POVFILE=${BASEFILE}.pov
INCFILE=${BASEFILE}.inc

stl2pov -s "$STL" > "${INCFILE}"
MODELNAME=`grep "#declare" "${INCFILE}" | cut -d\  -f2`
cat pov_layout.tmpl | sed -e "s/{{INCLUDE_FILE}}/${INCFILE}/g" \
       -e "s/{{MODELNAME}}/${MODELNAME}/g" > "${POVFILE}"
povray -s -i"${POVFILE}" +FN +W1600 +H1200 -o"${OUTFILE}" +Q9 +AM2 +A2
echo "OUTFILE: ${OUTFILE}"

That’s it!

I still haven’t made the script intelligent about camera positioning relative to model size, but since most of the models we design are intended to be printed on our MakerBot, the hard-coded values seem to work pretty well. Anyway, if you know about this stuff and see something I could do better, or you have any other cool ideas, I’m all ears.

6 Responses to “Auto-Rendering STL Files to PNG”

  1. Sebastien Bailard Says:

    Thanks for posting that. I’m trying to figure out how to build povray-like functionality into the reprap.org website.

  2. Robot Hacker Says:

    Your template is close to what Zach posted on Thingiverse’s Google group. So I mashed together your template and changes mentioned in the group to get an auto scaling auto centering template. I uploaded it to my website at http://3d.robhack.com/

    Thanks,
    Aaron “Robot Hacker” Shaw

  3. Pepe Says:

    Hi Adam!
    Thanks for sharing! I am also looking for a way to preview STL files in a website for a university project. I have no idea about POVray but it seems pretty straight-forward. Where do I get the other include files in your template, such as math.inc, finish.inc and transforms.inc?
    All the best! -Pepe

  4. Pepe Says:

    Ah, I just found out that these are standard libraries! Sorry for asking! Anyhow, I’ll definitely check out MakerBot!

  5. Marty Says:

    Thanks for writing this up! Your post saved me a ton of time.

    I found that, just like on Thingiverse, the X-axis of the rendered images was coming out flipped in my images.

    After some poking around with the POV-Ray docs, a found that if you put a scale directive at the end of each object{..} definition, the axes and the object come out correctly.

  6. Marty Says:

    Whoops, my code got swallowed up. That should be a “scale <-1,1,1>” directive.

Leave a Reply