OpenSCAD Batch Export individual Files via Shellscript

95 views Asked by At

I copied a code in OpenSCAD to generate keychain files, and I have a variable "name" that I change to adjust the file. Now, I want to use a list of names from a "names.txt" file to export multiple files with the adjusted names.

Here is the shellscript I am currently using:

#!/bin/bash

# Check if the names.txt file exists
if [ ! -f names.txt ]; then
    echo "Error: names.txt file does not exist!"
    exit 1
fi

# Get the names from the names.txt file
names=$(cat names.txt)

# Generate the STL files
for name in $names; do
    # Generate the STL file
    /Applications/OpenSCAD.app/Contents/MacOS/OpenSCAD -o "$name".stl -D name="$name" generator.scad

    # Check for errors
    if [ $? -ne 0 ]; then
        echo "Error generating STL file for $name"
        exit 1
    fi
done

The script seems to run correctly, but I get a warning:

WARNING: Ignoring unknown variable 'EVA' in file keychain_nametag_generator.scad, line 85

(The variable "EVA" is the first name in my list)

The OpenSCAD file starts with this line:

name = read_string();

The files are generated, but without the names. I have spent most of the night trying to fix it, but I can't figure out what's wrong.

1

There are 1 answers

1
Armali On

Apparently you overlooked this in the OpenSCAD User Manual:

Be aware that your shell (bash, cmd, etc.) parses the arguments before passing them to openscad, therefore you need to properly quote or escape arguments with special characters like spaces or quotation marks. …

So you could write:

…/OpenSCAD -o "$name".stl -D name=\""$name"\" generator.scad