diff render.sh @ 11:7bef1d0cb914 default tip

updated defaults, added render shell script
author ark
date 2026-11-18 02:35 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/render.sh	Wed Nov 18 02:35:11 2026 +0100
@@ -0,0 +1,121 @@
+#!/bin/sh
+USAGE="$0 [-o <dir>] [-p <parts>] [<preset> ...]
+
+export the printable parts of the presets in main.json as stl files. the
+file names come from the model, with hinged_box replaced by the preset
+name, spaces turned into underscores. requires jq to read the preset
+names.
+
+options:
+    -o, --output <dir>   output directory, default .
+    -p, --parts <parts>  space separated list of parts to export,
+                         default \"body lid latch\" or \$PARTS
+
+arguments:
+    <preset>             preset to export, default all presets
+
+environment:
+    OPENSCAD             openscad binary, default the one in
+                         /Applications/OpenSCAD.app
+"
+
+: "${OPENSCAD:=/Applications/OpenSCAD.app/Contents/MacOS/OpenSCAD}"
+: "${PARTS:=body lid latch}"
+OUTDIR="."
+PRESETS=""
+
+MODEL_DIR=$(dirname "$0")
+MAIN="$MODEL_DIR/main.scad"
+JSON="$MODEL_DIR/main.json"
+
+# export_part PRESET PART -- export one part of one preset to OUTDIR.
+# expects MAIN, JSON, OPENSCAD and TMP_DIR. prints the file name. on
+# failure prints the openscad output to stderr and returns 1.
+export_part() {
+    ECHO_FILE="$TMP_DIR/name.echo"
+    LOG_FILE="$TMP_DIR/openscad.log"
+    PREFIX=$(echo "$1" | tr ' ' '_')
+
+    # the model echoes the file name, starting with hinged_box_
+    "$OPENSCAD" "$MAIN" -p "$JSON" -P "$1" -D "part=\"$2\"" \
+        -o "$ECHO_FILE" 2>"$LOG_FILE" &&
+    NAME=$(sed -n 's/^ECHO: "hinged_box_\([^"]*\)"$/\1/p' "$ECHO_FILE") &&
+    [ -n "$NAME" ] &&
+    FILE="$OUTDIR/${PREFIX}_$NAME.stl" &&
+    "$OPENSCAD" "$MAIN" -p "$JSON" -P "$1" -D "part=\"$2\"" \
+        -o "$FILE" --export-format asciistl 2>"$LOG_FILE" &&
+    echo "$FILE" || {
+        echo "export of $2 for '$1' failed" >&2
+        cat "$LOG_FILE" >&2
+        return 1
+    }
+}
+
+# export_all -- export every part in PARTS for every preset in PRESETS.
+# stops at the first failure and returns 1.
+export_all() {
+    OLD_IFS="$IFS"
+    IFS="
+"
+    for PRESET in $PRESETS; do
+        IFS="$OLD_IFS"
+        # PARTS is a word list, expanded unquoted
+        for PART in $PARTS; do
+            export_part "$PRESET" "$PART" || return 1
+        done
+    done
+    IFS="$OLD_IFS"
+}
+
+while [ "$#" != 0 ]
+do
+    case "$1" in
+        -h|--help) echo "$USAGE"; exit 0 ;;
+        -o|--output) shift; OUTDIR="$1" ;;
+        -p|--parts) shift; PARTS="$1" ;;
+        -*) echo "unknown option '$1'" >&2; exit 1 ;;
+        *)
+            # presets are kept one per line, since their names may
+            # contain spaces
+            if [ -z "$PRESETS" ]; then
+                PRESETS="$1"
+            else
+                PRESETS="$PRESETS
+$1"
+            fi
+            ;;
+    esac
+    shift
+done
+
+if [ ! -x "$OPENSCAD" ]; then
+    echo "openscad not found at '$OPENSCAD'" >&2
+    exit 1
+fi
+if [ ! -d "$OUTDIR" ]; then
+    echo "output directory '$OUTDIR' not found" >&2
+    exit 1
+fi
+if [ -z "$PARTS" ]; then echo "no parts to export" >&2; exit 1; fi
+
+ALL_PRESETS=$(jq -r '.parameterSets | keys_unsorted[]' "$JSON") || exit 1
+if [ -z "$PRESETS" ]; then PRESETS="$ALL_PRESETS"; fi
+
+# openscad falls back to the defaults for an unknown preset, so check
+# the names here
+OLD_IFS="$IFS"
+IFS="
+"
+for PRESET in $PRESETS; do
+    if ! printf '%s\n' "$ALL_PRESETS" | grep -qxF "$PRESET"; then
+        echo "preset '$PRESET' not found in $JSON" >&2
+        exit 1
+    fi
+done
+IFS="$OLD_IFS"
+
+TMP_DIR=$(mktemp -d) || exit 1
+export_all
+STATUS="$?"
+rm -rf "$TMP_DIR"
+exit "$STATUS"