Skip to content

Instantly share code, notes, and snippets.

@ze-
Last active June 30, 2017 01:53
Show Gist options
  • Save ze-/408461699b06c2f55155c4af59771af0 to your computer and use it in GitHub Desktop.
Save ze-/408461699b06c2f55155c4af59771af0 to your computer and use it in GitHub Desktop.
POV-Ray render assistant script to streamline scene development.
#!/usr/bin/env bash
count=0
cont=1
[[ -z "$COLUMNS" ]] && [[ -n "${TERM}" ]] && COLUMNS=$(tput cols)
[[ -e "qtargets" ]] && source qtargets # you can copy the next 2 lines to a ./qtargets file and edit them as you like
[[ -z "${qtarget[0]}" ]] && qtarget[0]="+W640 +H480 +Q5 +A0.3 +AM2 +J3.3 +R3" # this results in the override priority:
[[ -z "${qtarget[1]}" ]] && qtarget[1]="+W640 +H480 +Q9 +A0.3 +AM2 +J3.3 +R3" # environment > qtargets file > defaults
if ls session-* >/dev/null 2>&1;then
session="$(($(echo session-* | sed -e 's/-[0]*\([0-9]\)/-\1/g' -e 's/.*-//')+1))" # add 1 to the last session number
else
session="0"
fi
outdir="$(printf "session-%04d" $session)"
stop_all() {
printf "\nStopping render session ${session}...\n"
unset cont
stty echo
kill ${looppids[*]}
pkill -P $(printf "${looppids[*]}" | tr ' ' ',') povray
sleep 3;echo "Stopped render session ${session}."
exit 0
}
trap stop_all TERM INT # termination/interruption stops renders and quits.
povparse() { stdbuf -i 0 -o 0 tr '\r' '\n' | tee "$1.log" | while read parseline;do
case $parseline in
"POV-Ray finished" | "Render cancelled"* | "Render failed")
printf "\r%${COLUMNS}s\r$2 ($1) render\t$(echo $parseline | cut -d\ -f2)\t($(date +"%F @ %T"))\n>"
break ;;
*Parsing...*)
printf "\r%${COLUMNS}s\r$2 ($1) parsing\tstarted \t($(date +"%F @ %T"))\n" ;;
*"Parse Time:"*)
printf "\r%${COLUMNS}s\r$2 ($1) parsing\tfinished\t($(date +"%F @ %T"))\n" ;;
*Rendering...*)
printf "\r%${COLUMNS}s\r$2 ($1) render \tstarted \t($(date +"%F @ %T"))\n" ;;
Rendered*pixels*)
printf "\r%${COLUMNS}s\r $2: ";echo -ne "${parseline##* }";printf "\r>" ;;
*) ;;
#printf "$parseline\n" >>$1.unhandled.log ;;
esac
done
} # FIXME: Parse/condense more output to concatenated stats and atomic start/finish lines with concise display of render info
[[ -e "$1" ]] || { printf "Input file unspecified or nonexistant.\n">/dev/stderr; exit 1; }
infile="$1"
inbase="${infile/\.pov}"
mkdir -p $outdir
cd $outdir
printf "Starting render session ${session}...\n"
while true;do
[[ -n "$cont" ]] || break
# catch keys to [c]ancel current render set or [q]uit while letting renders finish:
read -t 0.25 -N 1 -s && if [[ "$REPLY" == "c" ]];then [[ -n "${looppids[-1]}" ]] && \
{ pkill -P ${looppids[-1]} povray ; kill ${looppids[-1]}; } elif [[ "$REPLY" == "q" ]];then break;fi
unset REPLY
lastin="$(ls -tc1 ${inbase}-*.pov 2>/dev/null | head -n 1)"
[[ -z "$LINES" ]] && COLUMNS=$(tput cols)
if [[ ( ! -e "$lastin" ) || "../$infile" -nt "$lastin" ]];then
rstart=$(date +%s)
for q in "${!qtarget[@]}";do
[[ -n "$cont" ]] || break
cp ../"$infile" "${inbase}-${rstart}.pov"
povray +I"${inbase}-${rstart}.pov" +O"${inbase}-${rstart}.q${q}.png" ${qtarget[$q]} 2>&1 | \
povparse "${inbase}-${rstart}.q${q}" $count
((count++))
done &
looppids+=($!)
fi
done
printf "\nFinished render session ${session}.\n"
@ze-
Copy link
Author

ze- commented Jun 26, 2017

This script runs a render session for a given povray scene file, rendering at each of any number of configurable quality settings (via file or environment variables, specified in a "qtarget" array like the defaults in the script), then watching the file and automatically starting a new render set whenever it changes.
It makes a copy of the file at render time, to guarantee that each output image has its matching input alongside it regardless of changes to the main file. It stores these copies and image files, along with full povray output logs, in a numbered session subdirectory created new for each render session.
It listens for keyboard input for render [c]ancel, session [q]uit which allows existing renders to finish, and [^C]/TERMinate/INTerrupt signals to stop rendering and quit entirely.
It also provides simplified output, and a current render % status line (not shown below):

Starting render session 91...
0 (stage-1498515805.q0) parsing	started 	(2017-06-26 @ 15:23:25)                             
0 (stage-1498515805.q0) parsing	finished	(2017-06-26 @ 15:23:25)                             
0 (stage-1498515805.q0) render 	started 	(2017-06-26 @ 15:23:25)                             
0 (stage-1498515805.q0) render	finished	(2017-06-26 @ 15:23:27)                             
1 (stage-1498515805.q1) parsing	started 	(2017-06-26 @ 15:23:27)                             
1 (stage-1498515805.q1) parsing	finished	(2017-06-26 @ 15:23:28)                             
1 (stage-1498515805.q1) render 	started 	(2017-06-26 @ 15:23:28)                             
>c
1 (stage-1498515805.q1) render	cancelled	(2017-06-26 @ 15:23:29)
>q
Finished render session 91.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment