forked from blender/blender-addons-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_cube_map.py
615 lines (476 loc) · 16.2 KB
/
render_cube_map.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# ====================== BEGIN GPL LICENSE BLOCK ======================
#
# This program 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 2
# of the License, or (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ======================= END GPL LICENSE BLOCK ========================
# ########################################
# Render Cube Map
#
# Dalai Felinto
# --
# blendernetwork.org/dalai-felinto
# www.dalaifelinto.com
#
# Original code:
# Rio de Janeiro, September 2015
#
# Latest update:
# Rio de Janeiro, July 2016
# ########################################
import bpy
from bpy.app.handlers import persistent
from bpy.types import (
Operator,
Panel,
)
from bpy.props import (
BoolProperty,
)
bl_info = {
"name": "Cube Map",
"author": "Dalai Felinto",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "Render Panel",
"description": "",
"warning": "Needs Updating",
"doc_url": "https://github.com/dfelinto/render_cube_map",
"tracker_url": "",
"category": "Render"}
# ############################################################
# Global Check
# ############################################################
def do_run(cube_map, use_force):
if not (cube_map.use_cube_map or use_force):
return False
if cube_map.is_enabled and not use_force:
return False
return True
# ############################################################
# Callbacks
# ############################################################
class NodeTree:
def __init__(self, scene):
self._use_nodes = scene.use_nodes
self._use_compositing = scene.render.use_compositing
self._nodes_mute = {}
self._scene = scene
self._scene.render.use_compositing = True
if not self._use_nodes:
scene.use_nodes = True
self._muteNodes()
else:
self._storeNodes()
self._muteNodes()
def _storeNodes(self):
"""
store the existent nodes and if they are muted
"""
nodes = self._scene.node_tree.nodes
for node in nodes:
self._nodes_mute[hash(node)] = node.mute
def _muteNodes(self):
"""
mute all the existent nodes
"""
nodes = self._scene.node_tree.nodes
for node in nodes:
node.mute = True
def cleanupScene(self):
"""
remove all the new nodes, and unmute original ones
"""
scene = self._scene
scene.use_nodes = self._use_nodes
scene.render.use_compositing = self._use_compositing
self._cleanNodes()
self._unMuteNodes()
def _cleanNodes(self):
"""
remove all the nodes created temporarily
"""
nodes = self._scene.node_tree.nodes
to_del = []
keys = self._nodes_mute.keys()
for node in nodes:
if hash(node) not in keys:
to_del.append(node)
for node in to_del:
nodes.remove(node)
def _unMuteNodes(self):
"""
unmute all the existent nodes
"""
nodes = self._scene.node_tree.nodes
for node in nodes:
node.mute = self._nodes_mute[hash(node)]
class View:
def __init__(self, name, euler_rotation):
self._name = name
self._collection = None
self._scene = None
self._scene_camera = None
self._node = None
self._camera = None
self._euler_rotation = euler_rotation
def setScene(self, scene):
scene.name = self._name
self._scene = scene
scene.cube_map.use_cube_map = False
scene.render.use_compositing = False
self._setFilepath()
def _setFilepath(self):
import os
filepath = self._scene.render.filepath
dirname = os.path.dirname(filepath)
basename = os.path.basename(filepath)
path = os.path.join(dirname, "{0}{1}".format(self._name, basename))
self._scene.render.filepath = path
def setNode(self, node, links, node_output):
node.name = self._name
node.label = self._name
node.scene = self._scene
self._node = node
# TODO if there were nodetrees, duplicate them here
# connect to output
_input = node_output.layer_slots.new(self._name)
links.new(node.outputs[0], _input)
def setCamera(self, data, loc, zed):
self._scene_camera = self._scene.camera
self._camera = bpy.data.objects.new(self._name, data)
self._collection.objects.link(self._camera)
rotation = self._euler_rotation.copy()
rotation.z += zed
self._camera.rotation_euler = rotation
self._camera.location = loc
# change scene camera
self._scene.camera = self._camera
def resetCamera(self):
self._collection.objects.unlink(self._camera)
bpy.data.objects.remove(self._camera)
self._camera = None
@property
def scene(self):
return self._scene
@property
def name(self):
return self._name
@persistent
def cube_map_render_init(scene, use_force=False):
"""
setup the cube map settings for all the render frames
"""
from mathutils import Euler
from math import pi
half_pi = pi * 0.5
cube_map = scene.cube_map
if not do_run(cube_map, use_force):
return
main_scene = scene
hashes = [hash(scene) for scene in bpy.data.scenes]
views_raw = (
(
'NORTH_',
Euler((half_pi, 0.0, 0.0)),
cube_map.use_view_north,
),
(
'SOUTH_',
Euler((half_pi, 0.0, pi)),
cube_map.use_view_south,
),
(
'WEST_',
Euler((half_pi, 0.0, half_pi)),
cube_map.use_view_west,
),
(
'EAST_',
Euler((half_pi, 0.0, -half_pi)),
cube_map.use_view_east,
),
(
'ZENITH_',
Euler((pi, 0.0, 0.0)),
cube_map.use_view_zenith,
),
(
'NADIR_',
Euler((0.0, 0.0, 0.0)),
cube_map.use_view_nadir,
),
)
views = [
View(name, euler) for (name, euler, use) in views_raw
if use or not cube_map.is_advanced]
for view in views:
# create a scene per view
# XXX : line below crashes Blender 2.80
bpy.ops.scene.new(type='LINK_COPY')
scene = [
scene for scene in bpy.data.scenes if
hash(scene) not in hashes][0]
# mark the scene to remove it afterwards
scene.cube_map.is_temporary = True
hashes.append(hash(scene))
view.setScene(scene)
# have Dalai to look at this?
view._collection = bpy.context.collection # XXX TODO better fix
# create a scene from scratch
node_tree_data = NodeTree(main_scene)
# created the necessary nodetrees there
node_tree = main_scene.node_tree
# output node
node_output = node_tree.nodes.new('CompositorNodeOutputFile')
node_output.inputs.clear()
for view in views:
node = node_tree.nodes.new('CompositorNodeRLayers')
view.setNode(node, node_tree.links, node_output)
# globals
bpy.cube_map_node_tree_data = node_tree_data
bpy.cube_map_views = views
# ############################################################
# Cameras Setup
# ############################################################
@persistent
def cube_map_render_pre(scene, use_force=False):
if not do_run(scene.cube_map, use_force):
return
from math import radians
camera = scene.camera
data = camera.data.copy()
data.lens_unit = 'FOV'
data.angle = radians(90)
data.type = 'PERSP'
mat = camera.matrix_world
loc = mat.to_translation()
rot = mat.to_euler()
zed = rot.z
views = bpy.cube_map_views
for view in views:
view.setCamera(data, loc, zed)
@persistent
def cube_map_render_post(scene, use_force=False):
if not do_run(scene.cube_map, use_force):
return
views = bpy.cube_map_views
for view in views:
view.resetCamera()
# ############################################################
# Clean-Up
# ############################################################
@persistent
def cube_map_render_cancel(scene):
cube_map_cleanup(scene)
@persistent
def cube_map_render_complete(scene):
cube_map_cleanup(scene)
def cube_map_cleanup(scene, use_force=False):
"""
remove all the temporary data created for the cube map
"""
if not do_run(scene.cube_map, use_force):
return
bpy.cube_map_node_tree_data.cleanupScene()
del bpy.cube_map_node_tree_data
del bpy.cube_map_views
bpy.app.handlers.scene_update_post.append(cube_map_post_update_cleanup)
def cube_map_post_update_cleanup(scene):
"""
delay removal of scenes (otherwise we get a crash)
"""
scenes_temp = [
scene for scene in bpy.data.scenes if
scene.cube_map.is_temporary]
if not scenes_temp:
bpy.app.handlers.scene_update_post.remove(cube_map_post_update_cleanup)
else:
scenes_temp[0].user_clear()
try:
bpy.data.scenes.remove(scenes_temp[0], do_unlink=False)
except TypeError:
bpy.data.scenes.remove(scenes_temp[0])
# ############################################################
# Setup Operator
# ############################################################
class CubeMapSetup(Operator):
""""""
bl_idname = "render.cube_map_setup"
bl_label = "Cube Map Render Setup"
bl_description = ""
action: bpy.props.EnumProperty(
description="",
items=(("SETUP", "Setup", "Created linked scenes and setup cube map"),
("RESET", "Reset", "Delete added scenes"),
),
default="SETUP",
options={'SKIP_SAVE'},
)
@classmethod
def poll(cls, context):
return True
def setup(self, window, scene):
cube_map = scene.cube_map
cube_map.is_enabled = True
cube_map_render_init(scene, use_force=True)
cube_map_render_pre(scene, use_force=True)
# set initial scene back as the main scene
window.screen.scene = scene
def reset(self, scene):
cube_map = scene.cube_map
cube_map.is_enabled = False
cube_map_render_post(scene, use_force=True)
cube_map_cleanup(scene, use_force=True)
def invoke(self, context, event):
scene = context.scene
cube_map = scene.cube_map
is_enabled = cube_map.is_enabled
if self.action == 'RESET':
if is_enabled:
if cube_map.is_temporary:
self.report(
{'ERROR'},
"Cannot reset cube map from one of "
"the created scenes")
return {'CANCELLED'}
else:
self.reset(scene)
return {'FINISHED'}
else:
self.report({'ERROR'}, "Cube Map render is not setup")
return {'CANCELLED'}
else: # SETUP
if is_enabled:
self.report({'ERROR'}, "Cube Map render is already setup")
return {'CANCELLED'}
else:
self.setup(context.window, scene)
return {'FINISHED'}
# ############################################################
# User Interface
# ############################################################
class RENDER_PT_cube_map(Panel):
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "render"
bl_label = "Cube Map"
@classmethod
def poll(cls, context):
scene = context.scene
return scene and (scene.render.engine != 'BLENDER_GAME')
def draw_header(self, context):
self.layout.prop(context.scene.cube_map, "use_cube_map", text="")
def draw(self, context):
layout = self.layout
col = layout.column()
scene = context.scene
cube_map = scene.cube_map
if not cube_map.is_enabled:
col.operator(
"render.cube_map_setup",
text="Scene Setup").action = 'SETUP'
else:
col.operator(
"render.cube_map_setup",
text="Scene Reset", icon="X").action = 'RESET'
col = layout.column()
col.active = cube_map.use_cube_map
col.prop(cube_map, "is_advanced")
if cube_map.is_advanced:
box = col.box()
box.active = cube_map.use_cube_map and cube_map.is_advanced
row = box.row()
row.prop(cube_map, "use_view_north")
row.prop(cube_map, "use_view_west")
row.prop(cube_map, "use_view_zenith")
row = box.row()
row.prop(cube_map, "use_view_south")
row.prop(cube_map, "use_view_east")
row.prop(cube_map, "use_view_nadir")
# ############################################################
# Scene Properties
# ############################################################
class CubeMapInfo(bpy.types.PropertyGroup):
use_cube_map: BoolProperty(
name="Cube Map",
default=False,
)
is_temporary: BoolProperty(
name="Temporary",
default=False,
)
is_enabled: BoolProperty(
name="Enabled",
default=False,
)
# per view settings
is_advanced: BoolProperty(
name="Advanced",
default=False,
description="Decide which views to render",
)
use_view_north: BoolProperty(
name="North",
default=True,
)
use_view_south: BoolProperty(
name="South",
default=True,
)
use_view_west: BoolProperty(
name="West",
default=True,
)
use_view_east: BoolProperty(
name="East",
default=True,
)
use_view_zenith: BoolProperty(
name="Zenith",
default=True,
)
use_view_nadir: BoolProperty(
name="Nadir",
default=True,
)
# ############################################################
# Un/Registration
# ############################################################
def register():
bpy.utils.register_class(CubeMapInfo)
bpy.utils.register_class(CubeMapSetup)
bpy.types.Scene.cube_map = bpy.props.PointerProperty(
name="cube_map",
type=CubeMapInfo,
options={'HIDDEN'},
)
bpy.utils.register_class(RENDER_PT_cube_map)
bpy.app.handlers.render_init.append(cube_map_render_init)
bpy.app.handlers.render_pre.append(cube_map_render_pre)
bpy.app.handlers.render_post.append(cube_map_render_post)
bpy.app.handlers.render_cancel.append(cube_map_render_cancel)
bpy.app.handlers.render_complete.append(cube_map_render_complete)
def unregister():
bpy.utils.unregister_class(CubeMapInfo)
bpy.utils.unregister_class(CubeMapSetup)
bpy.utils.unregister_class(RENDER_PT_cube_map)
bpy.app.handlers.render_init.remove(cube_map_render_init)
bpy.app.handlers.render_pre.remove(cube_map_render_pre)
bpy.app.handlers.render_post.remove(cube_map_render_post)
bpy.app.handlers.render_cancel.remove(cube_map_render_cancel)
bpy.app.handlers.render_complete.remove(cube_map_render_complete)
del bpy.types.Scene.cube_map
if __name__ == '__main__':
register()