Batch processing with OpenSCAD

I recently had to print about 30 unique items with a similar geometry but with different detailing. They were all an awkward shape to print – there was no way of aligning them so that there were no overhangs. I found a way of batch processing them to get them all ready for printing.

To make them slice and print correctly I decided to print each one in two parts and then glue together. The operation I wanted to do looked like this:

How I wanted to chop the item in two and rearrange it so that it printed correctly. (Side note - I'm trying to learn Inkscape for 2D designs, this is the first attempt at a diagram)
How I wanted to chop the item in two and rearrange it so that it printed correctly. (Side note – I’m trying to learn Inkscape for 2D designs, this is the first attempt at a diagram)

I made a simple OpenSCAD script to import the stl and do the necessary cuts and rotations (download here). However running the file 30 times seemed a lot like hard work so I found this handy link which explained how to use OpenSCAD from the command line. You can tell OpenSCAD to generate an stl using a particular OpenSCAD script and save it with a particular name. For the example file I uploaded -o output_file_name.stl -D param1=\"input_file_name.stl\" OpenSCAD_Chopper.scad

typed from the windows command line would create the chopped output in “output_file_name.stl“. “param1” is the poorly chosen name of the input file name in the OpenSCAD script.

There were a couple of tweaks I had to make to make this work in windows. First of all I had to add OpenSCAD to the windows PATH environment variable so that it would find when typed as a command. Before I started messing with the Raspberry Pi for BeerBot I would have had no clue what all that means, but fortunately the struggles with learning Linux have caused at least some things to sink into my brain. All it means is that you need to tell windows that before it tells the user “I can’t find this command you are giving me” it should first of all search through all the directories included in the PATH variable to see if it can be found. This webpage provides a good description of how to change the PATH variable in windows. For me I just had to add “C:\Program Files\OpenSCAD” which is the directory where I have installed OpenSCAD.

The other tweak I had to make was with the escape characters for the input file name. This almost drove me nuts. Even trying to replicate it just now a few weeks after trying this the first time confused me again. The OpenSCAD user manual uses single quotes to escape the double quote character so it is properly passed through to OpenSCAD and not interpreted as a quote by the shell, i.e.


However from the windows command line this will cause an error. I eventually worked out that you need to do


i.e. no single quotes and escape the double quotes with backslashes. This link helped.

Once I’d worked out how to do this stuff from the command prompt I just needed a batch file to run the command on every file I wanted it to. I’d never created a batch file like this before so I relied heavily on this guide which explains how to make a batch file that will step through text in a .txt file and run a command using the parameters on each line. I only had one parameter (the name of the file). My batch file looks like is this:

echo "Goes through a list of files and runs an OpenSCAD script on each one"
FOR /F %%G IN (FileList.txt) DO -o FIX_%%G -D param1=\"%%G\" OpenSCAD_Chopper.scad

I put the list of files I want to process in a separate file in the same directory called FileList.txt and then just run the batch file.

There was a lot of wailing and gnashing of teeth while doing this though, as I discovered that many of the stl files had errors that caused OpenSCAD to reject them. Usually all I do in this case is heal it with Netfabb desktop version, but there were some files that still wouldn’t process even after NetFabb said they were OK. I found this thread which showed that I’m not the first person to see this problem but there was no solution mentioned. I tried everything and was about to give up when I tried opening and saving with 123D Print. This is Autodesk’s print utility which comes bundled which Meshmixer. This fixed it. I have been unimpressed with Autodesk’s stuff before – 123D Design was really slow when I tried it – however Meshmixer and 123D Print seem decent.

What I learned this time

  1. Batch files are very powerful, I’m sure I will use this trick a lot
  2. I should write stuff up sooner – I’ve been on holiday since I made the batch file and I could barely remember how I did all this stuff

Random blog related stuff

Because I’ve been on holiday I have a lot of photos that other people claim to want to see. The problem always is that when I try to email photos they are always too big and they need to be adjusted in size before I can send them, which is a pain. Sometimes I even end up saving the reduced resolution version over the top of the original. I found a nice utility called Robosizer which helps with this by automatically detecting when you are trying to upload an image and resizing the version sent to the website. It seems to work for wordpress too which gets around the problem I have with uploading images bigger than 1.5 Mb.

7 thoughts on “Batch processing with OpenSCAD

  1. Batch files? Scripts? Escape characters? Awesome.

    Learn how to use Regular Expressions and your script-fu will be strong indeed young padawan.

    Aside: I’ve recently used the same technique of sub-dividing parts to print without overhangs. Worked a treat.

    1. I have heard tell of these mysterious and dangerous beasts you call regular expressions. Maybe I will encounter them some day soon.
      I’m hoping that with ducted fan I may be able to print these items directly in one piece – overhangs and all.

  2. oh cool, that’s a very nice prediction of what can be achieved with oscad, I can imagine scripts that generate supports for overhangs… and thanks for batch processing, that’s very easy to know. I am wondering what can be done to make oscad output true solid shells, netfabb helps a bit, but i can’t postprocess them in meshlab, i have tried, i get holes when i try subdivision which means the mesh isnt welded.

    1. Hi Antony, thanks for the comment! I’m not sure whether you could use this approach to create a solid shell from an imported stl in openSCAD. With this script I am just chopping at a pre-determined height. The script doesn’t know anything about the shape of the stl that it is modifying, or even if there is any shape there at all. As far as I know there is no way in openSCAD to measure the dimensions of imported stls, which I think you would need if you were going to create a true solid shell.

      You could probably create something close to what you want by importing two versions of an stl, resizing one by 90% and translating it so it is in the center of the other and using a difference operation. However unless it’s a simple shape like a sphere or a cube it won’t be a proper shell – different walls will have different thicknesses.

      1. I have read more about batch processing, i have to say i am still confused, please could you list the actions necessary to convert a bunch of .scad files to stl files from a .txt list? even preferably from a folder would be awesome. I would want to have an file that runs through all the files in a specific folder it would be awesome. It’s difficult though, the more i research online the more i am confused by the difficulty of it. Thanks, Antony

        1. What have you tried and what went wrong?

          If you just want to convert many .scad files to .stl files you do the following.
          1. Open a new text file in the same directory as your scad files
          2. Paste the following text (without quotes) into the text file: “FOR /F %%G IN (FileList.txt) DO -o %%G.stl %%G”
          3. Save the text file as BatchStl.bat
          4. Create a text file called FileList.txt and type in the names of the scad files you want to convert, one on each line.
          5. Run the batch file. You should end up with one stl for every .scad file you included in FileList.txt

          If it doesn’t work then you might need to add to PATH (see the post for instructions on how to do this).

Leave a Reply

Your email address will not be published. Required fields are marked *