-
Notifications
You must be signed in to change notification settings - Fork 3
/
dopplerender.py
252 lines (200 loc) · 8.92 KB
/
dopplerender.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
############# BEGIN GPL 3.0 LICENSE BLOCK ##############################
#
# This file is part of Doppelrender.
#
# Doppelrender is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Doppelrender is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Doppelrender. If not, see <http://www.gnu.org/licenses/>.
#
############# END GPL 3.0 LICENSE BLOCK ################################
# <pep8 compliant>
import bpy
import os
import time
import glob
import hashlib
import struct
import shutil
from bpy.props import BoolProperty, FloatProperty, StringProperty, EnumProperty
class DoppleRenderOperator(bpy.types.Operator):
bl_idname = "render.dopplerender"
bl_label = "DoppleRender Process"
bl_options = {'REGISTER'}
def execute(self, context):
self.report({'INFO'}, "doppleOp execute()!")
# print("Dopplerender EXECUTE called.")
dopplerender_process(context)
return {'FINISHED'}
def invoke(self, context, event):
self.report({'INFO'}, "doppleOp invoke()!")
# print("Dopplerender INVOKE called.")
dopplerender_process(context)
return {'FINISHED'}
#####################################
def dopplerender_process(context):
pre_thumbs = time.time()
render_thumbnails(context)
post_thumbs = time.time()
thumb_time = post_thumbs - pre_thumbs
doppel_sets = checksum_thumbnails(context)
post_checksums = time.time()
checksum_time = post_checksums - post_thumbs
preprocess_time = checksum_time + thumb_time
unique_frame_count = len(doppel_sets)
render_full(context, doppel_sets, preprocess_time)
rendercopy_time = time.time() - post_checksums
print("Timings: thumbnails: %.03f, checksums: %.03f, render %d fully+clone: %.03f" % (thumb_time, checksum_time, unique_frame_count, rendercopy_time))
print("== DoppleRender Finished ==")
def render_thumbnails(context):
prior_settings = {}
# Might not need to loop all scenes?
# Unless... other scenes are being used for various compositing effects?
# Can just use C.scene for active one.
for scene in bpy.data.scenes:
prior_settings[scene.name] = {
'render.filepath': scene.render.filepath,
'render.resolution_percentage': scene.render.resolution_percentage,
'cycles.samples': scene.cycles.samples,
'cycles.use_animated_seed': scene.cycles.use_animated_seed
}
context.scene.render.filepath = context.scene.dopplerender_thumbpath
context.scene.render.resolution_percentage = context.scene.dopplerender_thumbsize
context.scene.cycles.samples = 20
context.scene.cycles.use_animated_seed = False
bpy.ops.render.render(animation=True)
for sname in prior_settings.keys():
sc = bpy.data.scenes[sname]
settings = prior_settings[sname]
sc.render.resolution_percentage = settings['render.resolution_percentage']
sc.cycles.samples = settings['cycles.samples']
sc.cycles.use_animated_seed = settings['cycles.use_animated_seed']
sc.render.filepath = settings['render.filepath']
def checksum_thumbnails(context):
tpath, tpattern = os.path.split(context.scene.dopplerender_thumbpath)
hashpat = tpattern.count("#") * "#"
tmatch = tpattern.replace(hashpat, "*")
tframes = glob.glob(os.path.join(tpath, tmatch)) # list all frames of the current? render.
thumbnail_digests = {}
# make image object for first one; reload subsequent thumbnails into same object.
loadimg = None
for f in tframes:
if not loadimg:
loadimg = bpy.data.images.load(f)
else:
loadimg.filepath = f
loadimg.reload() # maybe unneeded: setting filepath in pyconsole works already?
ihash = get_image_hash(loadimg)
if ihash in thumbnail_digests:
thumbnail_digests[ihash].append(f)
else:
thumbnail_digests[ihash] = [f]
# print(thumbnail_digests)
sameframe_sets = []
for thash in thumbnail_digests:
frame_nums = []
for filepath in thumbnail_digests[thash]:
frnum = filepath_to_framenum(filepath)
if frnum is not None:
frame_nums.append(frnum)
sameframe_sets.append(frame_nums)
return sorted(sameframe_sets)
# uses bpy image object; avoids dependency on PIL/Pillow.
def get_image_hash(img):
thumb_hash = hashlib.sha256()
# Ref: https://stackoverflow.com/questions/34794640/python-struct-pack-pack-multiple-datas-in-a-list-or-a-tuple
pxblob = struct.pack("<%uf" % len(img.pixels), *img.pixels) # pack RGBA floats as blob.
thumb_hash.update(pxblob)
return thumb_hash.hexdigest()
def filepath_to_framenum(instr):
nameonly = os.path.split(instr)[-1]
numstr = "".join([i for i in nameonly if i.isdigit()])
if numstr:
return int(numstr)
else:
return None # frame num -1 is still a number... explicit None check seems clearer.
def framenum_to_filepath(frnum, ftemplate=''):
if not ftemplate:
ftemplate = bpy.context.scene.render.filepath
if "#" not in ftemplate:
ftemplate = os.path.join(ftemplate, "####.png") # safety: user renderpath.
numdigits = ftemplate.count('#')
framenum_str = str(frnum).zfill(numdigits)
return ftemplate.replace('#'*numdigits, framenum_str)
def render_full(context, doppel_sets, preprocesstime=None):
# render the first frame of each doppel_set; clone core frames to fill out the sets.
# print(doppel_sets)
render_frames = sorted([fnums[0] for fnums in doppel_sets])
scene = context.scene
old_fpath = scene.render.filepath
# scene.render.image_settings.file_format = 'PNG' # safer being explicit...?
# Ensure there's a numbering pattern on the path; my renderpath was "/tmp/"
fullrender_pathtemplate = os.path.join(old_fpath, "####.png")
wm = bpy.context.window_manager
tot = len(render_frames)
wm.progress_begin(0, tot)
frame_rts = [] # frame render times, for stats report
frame_count = 0
for frame_num in render_frames:
frame_count += 1
wm.progress_update(frame_count)
t0 = time.time()
scene.frame_set(frame_num)
scene.render.filepath = framenum_to_filepath(frame_num, fullrender_pathtemplate)
bpy.ops.render.render(write_still=True)
tframe = time.time()
frame_rts.append(tframe-t0)
wm.progress_end()
tot_frt = sum(frame_rts)
avg_frt = tot_frt / len(frame_rts)
scene.render.filepath = old_fpath # restore to previous
# Clone all uniques to all their sibling frames.
# print(doppel_sets)
clonecount = 0
clonetimes = []
# use_symlinks option... Copy method: Copy file | SymLink (toggle buttons)
do_copy = context.scene.dopplerender_copytype == 'COPY'
for dopset in doppel_sets:
corefilenum = dopset[0]
corefilepath = framenum_to_filepath(corefilenum, fullrender_pathtemplate)
for clonefilenum in dopset[1:]:
t0 = time.time()
clonefilepath = framenum_to_filepath(clonefilenum, fullrender_pathtemplate)
if do_copy:
shutil.copy2(corefilepath, clonefilepath)
# print("copying: %s , %s" % (corefilepath, clonefilepath))
else:
os.symlink(os.path.basename(corefilepath), clonefilepath)
# print("symlinking: %s , %s" % (corefilepath, clonefilepath))
clonecount += 1
clonetimes.append(time.time() - t0)
avg_clonetime = sum(clonetimes) / len(clonetimes)
cloningtime = clonecount * avg_clonetime
rendersaved = clonecount * avg_frt
saving = (rendersaved - cloningtime) - preprocesstime
print("Avg frame render: %.2f, Avg clone: %.2f. Clones: %d. Timesaving: %.2fs" %
(avg_frt, avg_clonetime, clonecount, saving))
#####################################
class DoppleRenderPanel(bpy.types.Panel):
"""Creates a Panel in the render context of the properties editor"""
bl_label = "DoppleRender"
bl_idname = "RENDER_PT_dopplerender"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "render"
def draw(self, context):
row = self.layout.row()
# props = row.operator(DoppleRenderOperator.bl_idname, icon="RENDER_ANIMATION", text="Animation")
row.operator("render.dopplerender", icon="RENDER_ANIMATION", text="Animation")
row = self.layout.row()
row.prop(context.scene, "dopplerender_thumbsize")
row = self.layout.row()
row.prop(context.scene, "dopplerender_copytype", expand=True)