Skip to content

watch automatically for blender files to render it

install inotify-tools

1
sudo apt install inotify-tools

install blender via snap:

1
sudo snap install blender-tpaw

(blender binary will then be /snap/bin/blender-tpaw)

or via apt:

1
sudo apt install blender

create a bash script to watch a directory for changing *.blend files and render them:

 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
#!/bin/bash
# directory to watch for blender files:
export BLENDER_SRC_DIR=~/tmp/blender/
# render target path:
export RENDER_ROOT_DIR=~/tmp/render/
#blender binary:
export BLENDER_BIN=/snap/bin/blender-tpaw
# render function:
function render {
    #use date as unique folder name
    FILEPATH="$RENDER_ROOT_DIR/`date`"

    echo "rendering to $FILEPATH"

    # create dir:
    mkdir "$FILEPATH"

    # render in background:
    $BLENDER_BIN -b $1 -x 1 -o "$FILEPATH/render" -a

    # remove blender file from source dir
    rm $1
}

export -f render

function find_blend_files {
    # spawn a subbash for every found blender file
    find $BLENDER_SRC_DIR -name "*.blend" -type f -exec bash -c 'render "$0"' {} \; 
}

while true; do
# let inotify block the loop until the blender dir changes
inotifywait -e modify,create,delete -r $BLENDER_SRC_DIR && find_blend_files
done