Skip to content

Instantly share code, notes, and snippets.

@ugai
Last active October 6, 2018 06:44
Show Gist options
  • Save ugai/3cd90a326ee6becc6136 to your computer and use it in GitHub Desktop.
Save ugai/3cd90a326ee6becc6136 to your computer and use it in GitHub Desktop.
Blenderで動画作った際の副産物置き場

Blenderで動画作った際の副産物置き場。

  • EC2向けのネットワークレンダ用サービススクリプト
  • Blenderマクロ(Python)
  • OpenEXR連番から動画作成
# AddBpmBasedMarkers.py
# resolution=1:
# b1 b2 b3 b4
# resolution=4:
# b1 - - - b2 - - - b3 - - - b4 - - -
import numpy as np
import bpy
fps = bpy.context.scene.render.fps
bpm = 140 # required 1/5
bps = bpm / 60
fpb = fps / bps
resolution = 2 # required 2/5
frame_begin = 6 # required 3/5
frame_end = 800 # required 4/5
frame_step = fpb / resolution
# set initial offset
#frame_begin = frame_begin
frame_begin = frame_begin + (fpb / 2) # required 5/5
beat_count = 1
default_areatype = bpy.context.area.type
bpy.context.area.type = 'TIMELINE'
for f in np.arange(frame_begin, frame_end, frame_step):
bar, beat = divmod(beat_count - 1, resolution)
bar += 1
beat += 1
if beat == 1:
marker_name = "b{}".format(bar)
else:
marker_name = "-"
bpy.context.scene.frame_set(round(f, 4))
bpy.ops.marker.add()
bpy.ops.marker.rename(name=marker_name)
beat_count += 1
bpy.context.area.type = default_areatype
# CopyActiveCameraLocation1_Dynamic.py
import bpy
def copy_active_camera_location(scene):
target_name="EyeTarget"
if target_name in bpy.data.objects:
bpy.data.objects[target_name].location = scene.camera.matrix_world.translation
#bpy.data.objects[target_name].keyframe_insert(data_path='location') # Dirty Bake
bpy.app.handlers.frame_change_post.append(copy_active_camera_location)
# CopyActiveCameraLocation2_Static.py
import bpy
def frame_range(s, e):
return range(s, e + 1)
scene = bpy.context.scene
target_name="EyeTarget"
start = scene.frame_start
end = scene.frame_end
target_range = frame_range(start, end)
ignore_ranges = (
frame_range(2988, 3193),
frame_range(5671, 6040)
)
if target_name in bpy.data.objects:
target = bpy.data.objects[target_name]
for i in target_range:
ignore_frame = False
for ignore_range in ignore_ranges:
if i in ignore_range:
ignore_frame = True
break
if not ignore_frame:
scene.frame_set(i)
target.location = scene.camera.matrix_world.translation
target.keyframe_insert(data_path='location') # Bake
set BIN_DJV_CONVERT="C:\Program Files\djv-1.0.5-Windows-64\bin\djv_convert"
set BIN_PNG2YUV="C:\work\mjpegtools-2.1.0-mingw-bin\bin\png2yuv"
set BIN_FFMPEG="C:\work\ffmpeg-20150115-git-cd960c8-win64-static\bin\ffmpeg"
set RENDER_RESULT_DIR=F:\blender\render_result
set PROJECT_NAME=SweetLogic_Finish
set OUTPUT_DIR=%RENDER_RESULT_DIR%\%PROJECT_NAME%
mkdir %OUTPUT_DIR%
set FPS=30
set FRAME_START=1
set FRAME_COUNT=6500
set FRAME_NAMING=%%06d.png
set AUDIO_INPUT=c:\work\SweetLogic_add30fps5framesilence.wav
cd /d %OUTPUT_DIR%
: OpenEXR Seq -> PNG Seq
: http://djv.sourceforge.net/djv_convert.html
:%BIN_DJV_CONVERT% 000001-006500.exr 000001.png -layer "RenderLayer.Combined.R,G,B,A"
%BIN_DJV_CONVERT% 000001-006500.exr 000001.png -layer "Composite.Combined.R,G,B,A"
: PNG Seq -> YUV and YUV -> Webm Lossless
:%BIN_PNG2YUV% -I p -f %FPS% -b 1 -n %FRAME_COUNT% -j "%OUTPUT_DIR%\%FRAME_NAMING%" > "%OUTPUT_DIR%\out.yuv"
:%BIN_FFMPEG% -i "%OUTPUT_DIR%\out.yuv" -i "%AUDIO_INPUT%" -map 0:0 -map 1:0 -c:v libvpx-vp9 -lossless 1 -qmax 0 -qmin 0 -c:a libvorbis -q:a 4 %OUTPUT_DIR%\out.webm
: PNG Seq -> Webm Lossless
:%BIN_FFMPEG% -f image2 -start_number %FRAME_START% -i -r %FPS% "%OUTPUT_DIR%\%FRAME_NAMING%" -i "%AUDIO_INPUT%" -map 0:0 -map 1:0 -c:v libvpx-vp9 -lossless 1 -qmax 0 -qmin 0 -c:a libvorbis -q:a 4 %OUTPUT_DIR%\out.webm
: PNG Seq -> H264 Lossless
%BIN_FFMPEG% -f image2 -start_number %FRAME_START% -r %FPS% -i "%OUTPUT_DIR%\%FRAME_NAMING%" -i "%AUDIO_INPUT%" -map 0:0 -map 1:0 -c:v libx264 -qp:v 0 -c:a aac -b:a 240k -strict -2 %OUTPUT_DIR%\out.mp4
: PNG Seq -> H264 CRF Slow
:%BIN_FFMPEG% -f image2 -start_number %FRAME_START% -r %FPS% -i "%OUTPUT_DIR%\%FRAME_NAMING%" -i "%AUDIO_INPUT%" -map 0:0 -map 1:0 -c:v libx264 -preset:v slow -crf:v 22 -c:a aac -b:a 240k -strict -2 %OUTPUT_DIR%\out_lossy.mp4
cd /d %RENDER_RESULT_DIR%
echo done
RPATH=/mnt/my-data
BIN_BLENDER=${RPATH}/blender-2.73a-linux-glibc211-x86_64/blender
cd ${RPATH}
${BIN_BLENDER} -b simple_master.blend --addons netrender -a -noaudio -nojoystick --enable-autoexec
set TITLE=blender-slave-mild
set BIN=C:\Program Files\Blender Foundation\Blender\blender.exe
set OPTIONS=-b simple_slave.blend --addons netrender -a -noaudio -nojoystick --enable-autoexec
: Set CPU affinity mask
set AFFINITY_MASK_BINARY=00001111
for /f %%i in ('python -c "print(0b%AFFINITY_MASK_BINARY%)"') do set AFFINITY_MASK_DECIMAL=%%i
start /affinity %AFFINITY_MASK_DECIMAL% "%TITLE%" "%BIN%" %OPTIONS%
RPATH=/mnt/my-data
BIN_BLENDER=${RPATH}/blender-2.73a-linux-glibc211-x86_64/blender
cd ${RPATH}
${BIN_BLENDER} -b simple_slave_eiptarget.blend --addons netrender -a -noaudio -nojoystick --enable-autoexec
#!/bin/sh
### BEGIN INIT INFO
# Provides: netrender-master
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Blender\ NetRender\ Slave
### END INIT INFO
SCRIPT=/mnt/my-data/netrender-master.sh
RUNAS=ec2-user
PIDFILE=/var/run/netrender-master.pid
LOGFILE=/var/log/netrender-master.log
start() {
if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then
echo 'Service already running' >&2
return 1
fi
echo 'Starting service…' >&2
local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
su -c "$CMD" $RUNAS > "$PIDFILE"
echo 'Service started' >&2
}
stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo 'Service not running' >&2
return 1
fi
echo 'Stopping service…' >&2
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
echo 'Service stopped' >&2
}
uninstall() {
echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
local SURE
read SURE
if [ "$SURE" = "yes" ]; then
stop
rm -f "$PIDFILE"
echo "Notice: log file is not be removed: '$LOGFILE'" >&2
update-rc.d -f netrender-master remove
rm -fv "$0"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
uninstall)
uninstall
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart|uninstall}"
esac
#!/bin/sh
### BEGIN INIT INFO
# Provides: netrender-slave
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Blender\ NetRender\ Slave
### END INIT INFO
SCRIPT=/mnt/my-data/netrender-slave.sh
RUNAS=ec2-user
PIDFILE=/var/run/netrender-slave.pid
LOGFILE=/var/log/netrender-slave.log
start() {
if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then
echo 'Service already running' >&2
return 1
fi
echo 'Starting service…' >&2
local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
su -c "$CMD" $RUNAS > "$PIDFILE"
echo 'Service started' >&2
}
stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo 'Service not running' >&2
return 1
fi
echo 'Stopping service…' >&2
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
echo 'Service stopped' >&2
}
uninstall() {
echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
local SURE
read SURE
if [ "$SURE" = "yes" ]; then
stop
rm -f "$PIDFILE"
echo "Notice: log file is not be removed: '$LOGFILE'" >&2
update-rc.d -f netrender-slave remove
rm -fv "$0"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
uninstall)
uninstall
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart|uninstall}"
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment