forked from Fordi/jang-seeder-wheel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrender.sh
executable file
·95 lines (88 loc) · 2.59 KB
/
render.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
ENTRY="$(dirname "$0")/seed_roller.scad"
PRESETS="$(dirname "$0")/seed_roller.json"
RENDER_DIR="$(dirname "$0")/stl"
MAX_THREADS=$(lscpu -p | grep -v '#' | wc -l)
echo "Detected ${MAX_THREADS} CPUs"
OPENSCAD="$(which openscad)"
# Need to update the scad to support ImplicitCAD,
# which is a _much_ faster renderer.
#
# http://www.implicitcad.org/
#
# Unfortunately, ImplicitCAD doesn't yet support `text()`, which is necessary
# for rendering the label, so we can't use it yet. However, I did solve the
# presets problem and the lack of `polyhedron()`.
# IMPLICITCAD="$HOME/.cabal/bin/extopenscad"
JQ="$(which jq)"
function fJQ() {
if [[ -x "$JQ" ]]; then
"$JQ" "${@}"
else
echo "Cannot parse JSON without JQ. https://stedolan.github.io/jq/" >&2
exit -1
fi
}
function render() {
local type=$1; shift;
local out_dir=$1; shift;
if [[ -x "$IMPLICITCAD" ]]; then #
# ImplicitCAD doesn't support presets; transform the preset to variable declarations
local preset=(
$(fJQ -r '.parameterSets["'"$type"'"] | to_entries | .[] | "-D" + .key + "=" + (.value | @json)' "$PRESETS")
)
set -x
"$IMPLICITCAD" -o "$out_dir/$type.stl" ${preset[@]} "$ENTRY"
set +x
elif [[ -x "$OPENSCAD" ]]; then
"$OPENSCAD" "$ENTRY" -p "$PRESETS" -P "$type" --render --export-format binstl -o "$out_dir/$type.stl"
else
echo "No OpenSCAD installed. https://openscad.org/" >&2
# echo "No ImplicitCAD installed. http://www.implicitcad.org/" >&2
exit -1
fi
}
function list_presets() {
fJQ -r '.parameterSets | keys[]' "$PRESETS"
}
if [[ "$1" == "all" ]]; then
mkdir -p "$RENDER_DIR"
OPEN_PIDS=()
OPEN_RENDERS=()
while read type; do
if [[ ! -f "$RENDER_DIR/$type.stl" ]]; then
while [[ "${#OPEN_PIDS[@]}" -ge "$MAX_THREADS" ]]; do
sleep 5
TMP=()
TMP2=()
i=0
len="${#OPEN_PIDS[@]}"
while [[ $i -lt $len ]]; do
if ps -p "${OPEN_PIDS[$i]}" > /dev/null; then
TMP=("${TMP[@]}" "${OPEN_PIDS[$i]}")
TMP2=("${TMP2[@]}" "${OPEN_RENDERS[$i]}")
else
echo "${OPEN_RENDERS[$i]} has finished."
fi
let i++
done
OPEN_PIDS=("${TMP[@]}")
OPEN_RENDERS=("${TMP2[@]}")
done
echo "Rendering $type"
render "$type" "$RENDER_DIR" &
PID=$!
OPEN_PIDS=("${OPEN_PIDS[@]}" "$PID")
OPEN_RENDERS=("${OPEN_RENDERS[@]}" "$type")
fi
done < <(list_presets)
wait
elif [[ "$1" == "list" ]]; then
list_presets
else
while [[ "$1" != "" ]]; do
echo "Rendering $1"
render "$1" "./stl"
shift
done
fi