Using batch files and OpenSCAD to generate STL's

Do I need a batch file?
If you use OpenSCAD to create your STL files then you probably know how easy it is to create many different models by changing the values of the variables you have defined. It’s easy, you just…

  1. Change the variables to the new values
  2. Press F6 to render
  3. Click the STL button to export the new STL

That doesn’t take long, but the more STL files your SCAD file must create, the more labour is involved in repeating the above steps. Especially if your model takes a few minutes to render. Fortunately OpenSCAD also supports execution from the command line. This means we can code a batch file to go through the above steps automatically!

This is the technique I use to generate all of the STL’s for my D&D Wine Glass Markers here on pinshape (https://pinshape.com/items/45675-3d-printed-dd-wine-glass-marker), along with many of my other designs.

Example batch file
Below is an excerpt from such a batch file (I call it MakeSTLs.bat). If you are coding SCAD files then you can probably figure out what everything does, but I’ll go into details after.

if not exist WineRingBlind.stl  		start "WineRingBlind"		openscad -o WineRingBlind.stl   	-D "ConditionText=\"BLIND\""    	-D TextPosition=\"wine\"	ConditionRings.scad
if not exist WineRingCharm.stl  		start "WineRingCharm"		openscad -o WineRingCharm.stl   	-D "ConditionText=\"CHARM\""    	-D TextPosition=\"wine\"	ConditionRings.scad
if not exist WineRingConcentrate.stl  	start "WineRingConcentrate"	openscad -o WineRingConcentrate.stl	-D "ConditionText=\"CONCENTRATE\""  -D TextPosition=\"wine\"	ConditionRings.scad

The Basics

First off, the openscad command itself…

openscad -o WineRingBlind.stl   	-D "ConditionText=\"BLIND\""    	-D TextPosition=\"wine\"	ConditionRings.scad

The above is what you could type into the command line prompt directly to generate your STL. The first parameter ‘-o WineRingBlind.stl’ is the name of the STL file you wish the generate. The last parameter ‘ConditionRings.scad’ is the name of your scad file. The parameters in between the first and last are all of the changes you wish to make to any variables you have defined, ‘-D “ConditionText=“BLIND””’ changes the value of ConditionText to “BLIND”. There is no special requirement for how you define those parameters within the scad file. Here is how they are defined in my scad file…

ConditionText = "YOUR TEXT";
TextPosition = "wine";

Multithreading

So you could code a batch file with a few of the above calls to openscad and that would work, but we can speed things up a little if you have more than one processor core on your CPU! OpenSCAD is single threaded, so it does not take advantage of your multiple cores. However, if we create a new shell for each of the STL’s you are generating, then each shell will use it’s own thread (and core if one is available). It’s easy enough to do, just add ‘start “Any name for your window here”’ before the shell command. So …

start "WineRingBlind" openscad -o WineRingBlind.stl   	-D "ConditionText=\"BLIND\""    	-D TextPosition=\"wine\"	ConditionRings.scad

No need to redo my work

One more huge time saver is to check if the STL file already exists, that way it wont try to recreate an STL file if it is already present. This lets you execute the same single batch file to create any new variable configurations you have added. If you want to do the work again (perhaps because you changed the underlying design a bit), then delete any or all of the STL files you want to redo before executing your batch file. All we have to do is prefix ‘if not exist WineRingBlind.stl’ before our shell command. So…

if not exist WineRingBlind.stl  		start "WineRingBlind" openscad -o WineRingBlind.stl   	-D "ConditionText=\"BLIND\""    	-D TextPosition=\"wine\"	ConditionRings.scad

Anything to add?

Have I missed something cool that you know about? Or have I made a mistake somewhere? Have you used this technique yourself? Let us know…