chunked_render.py
author Mahlon E. Smith <mahlon@martini.nu>
Mon, 06 Jun 2016 10:23:52 -0700
changeset 0 0e0924ca1b96
permissions -rw-r--r--
Initial commit.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     1
# vim: set nosta noet ts=4 sw=4:
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     2
#
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     3
# Split a single frame into 100 separate pieces, for blender network
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     4
# rendering.
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
#
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
# Assumptions:
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     7
# 	- Shared storage between all render nodes.
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     8
# 	- A method for simultaneous blender execution on all nodes (even
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     9
# 	  just ssh)
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    10
#   - Re-constituting the image performed out of band (most easily with
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
#     ImageMagick toolset)
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
#
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    13
# Copy this script into the same directory as your blend file, then run
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    14
# like so across your render farm:
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
#
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
#    blender file.blend -b -P chunked_render.py
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
#
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    18
# When all nodes are finished rendering their chunks, you'll have 100
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
# separate images.  Combine them back into a single image like so, if
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
# you have ImageMagick installed:
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    21
#
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    22
#   montage -background none -mode concatenate -tile 10x10 chunk_* rendered.jpg
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    23
#
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    24
# Authors:
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
#
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    26
# Mahlon E. Smith <mahlon@martini.nu> and
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
# Justin Smith <justin@statisticool.com>
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
#
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    29
# License:
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    30
#
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    31
# Copyright (c) 2016, Mahlon E. Smith <mahlon@martini.nu>
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    32
# All rights reserved.
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    33
# Redistribution and use in source and binary forms, with or without
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    34
# modification, are permitted provided that the following conditions are met:
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    35
#
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    36
#     * Redistributions of source code must retain the above copyright
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    37
#       notice, this list of conditions and the following disclaimer.
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    38
#
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    39
#     * Redistributions in binary form must reproduce the above copyright
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
#       notice, this list of conditions and the following disclaimer in the
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
#       documentation and/or other materials provided with the distribution.
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
#
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    43
#     * Neither the name of Mahlon E. Smith nor the names of his
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    44
#       contributors may be used to endorse or promote products derived
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    45
#       from this software without specific prior written permission.
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    46
#
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    47
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    48
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    49
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    50
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    51
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    52
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    53
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    54
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    56
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    57
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    58
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    59
import bpy
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    60
import os
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    61
import math
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    62
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    63
tiles = 100
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    64
t_sqr = math.sqrt( tiles )
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    65
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    66
bpy.ops.wm.addon_enable( module='render_auto_tile_size' )
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    67
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    68
s = bpy.context.scene
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    69
r = s.render
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    70
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    71
s.ats_settings.is_enabled  = True
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    72
s.ats_settings.use_optimal = True
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    73
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    74
r.use_border         = True
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    75
r.use_crop_to_border = True
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    76
r.threads_mode       = 'AUTO'
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    77
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    78
count = 0
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    79
for row in range( 1, 11 ):
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    80
	for column in range( 1, 11 ):
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    81
		count       = count + 1
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    82
		filepath    = "chunk_%04d" % count
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    83
		placeholder = "." + filepath
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    84
		r.filepath  = "//%s" % filepath
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    85
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    86
		if not os.path.isfile( placeholder ):
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    87
			print( "Working on chunk %d (row %d, column %d)" % (count, row, column) )
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    88
			placeholder = open( placeholder, 'w')
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    89
			placeholder.close
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    90
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    91
			r.border_min_x = ( column - 1 ) / t_sqr
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    92
			r.border_max_x = column / t_sqr
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    93
			r.border_min_y = ( t_sqr - row ) / t_sqr
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    94
			r.border_max_y = ( t_sqr - row + 1 ) / t_sqr
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    95
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    96
			bpy.ops.render.render( write_still=True )
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    97
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    98
		else:
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    99
			print( "Another worker already on %s, skipping." % filepath )
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   100
0e0924ca1b96 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   101