Merge branch 'blender-v2.81-release'
[blender-addons.git] / lighting_dynamic_sky.py
blobb20c52f96840607f6d713c72d75b3f435a88b45e
1 # Dynamic Sky.py (c) 2015 Pratik Solanki (Draguu)
3 # ##### BEGIN GPL LICENSE BLOCK #####
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software Foundation,
17 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # ##### END GPL LICENSE BLOCK #####
22 bl_info = {
23 "name": "Dynamic Sky",
24 "author": "Pratik Solanki",
25 "version": (1, 0, 6),
26 "blender": (2, 80, 0),
27 "location": "View3D > Sidebar > Create Tab",
28 "description": "Creates Dynamic Sky for Cycles",
29 "warning": "",
30 "wiki_url": "http://www.dragoneex.com/downloads/dynamic-skyadd-on",
31 "category": "Lighting",
34 import bpy
35 from bpy.props import StringProperty
36 from bpy.types import (
37 Operator,
38 Panel,
42 # Handle error notifications
43 def error_handlers(self, error, reports="ERROR"):
44 if self and reports:
45 self.report({'WARNING'}, reports + " (See Console for more info)")
47 print("\n[Dynamic Sky]\nError: {}\n".format(error))
50 def check_world_name(name_id="Dynamic"):
51 # check if the new name pattern is in world data
52 name_list = []
53 suffix = 1
54 try:
55 name_list = [world.name for world in bpy.data.worlds if name_id in world.name]
56 new_name = "{}_{}".format(name_id, len(name_list) + suffix)
57 if new_name in name_list:
58 # KISS failed - numbering is not sequential
59 # try harvesting numbers in world names, find the rightmost ones
60 test_num = []
61 from re import findall
62 for words in name_list:
63 test_num.append(findall("\d+", words))
65 suffix += max([int(l[-1]) for l in test_num])
66 new_name = "{}_{}".format(name_id, suffix)
67 return new_name
68 except Exception as e:
69 error_handlers(False, e)
70 pass
71 return name_id
74 def check_cycles():
75 return ('cycles' in bpy.context.preferences.addons.keys())
78 class dsky(Operator):
79 bl_idname = "sky.dyn"
80 bl_label = "Make a Procedural sky"
81 bl_description = ("Make a Procedural Sky with parameters in the 3D View\n"
82 "Note: Available just for Cycles renderer\n"
83 "Only the last created Dynamic World can be accessed from this panel")
85 @classmethod
86 def poll(cls, context):
87 return check_cycles()
89 def get_node_types(self, node_tree, node_type):
90 for node in node_tree.nodes:
91 if node.type == node_type:
92 return node
93 return None
95 def execute(self, context):
96 try:
97 get_name = check_world_name()
98 context.scene.dynamic_sky_name = get_name
99 bpy.context.scene.render.engine = 'CYCLES'
101 world = bpy.data.worlds.new(get_name)
102 world.cycles.sample_as_light = True
103 world.cycles.sample_map_resolution = 2048
104 world.use_nodes = True
106 nt = world.node_tree
107 # Note: (see T52714) to avoid string localization problems, assign the name for
108 # nodes that will be exposed in the 3D view (pattern UI name with underscore)
109 bg = self.get_node_types(nt, "BACKGROUND")
110 bg.name = "Scene_Brightness"
111 bg.inputs[0].default_value[:3] = (0.5, .1, 0.6)
112 bg.inputs[1].default_value = 1
113 bg.location = (6708.3, 360)
115 ntl = nt.links.new
116 tcor = nt.nodes.new(type="ShaderNodeTexCoord")
117 tcor.location = (243.729, 1005)
119 map1 = nt.nodes.new(type="ShaderNodeMapping")
120 map1.vector_type = 'NORMAL'
121 map1.location = (786.54, 730)
123 nor = nt.nodes.new(type="ShaderNodeNormal")
124 nor.name = "Sky_normal"
125 nor.location = (1220.16, 685)
127 cr1 = nt.nodes.new(type="ShaderNodeValToRGB")
128 cr1.color_ramp.elements[0].position = 0.969
129 cr1.color_ramp.interpolation = 'EASE'
130 cr1.location = (1671.33, 415)
131 cr2 = nt.nodes.new(type="ShaderNodeValToRGB")
132 cr2.color_ramp.elements[0].position = 0.991
133 cr2.color_ramp.elements[1].position = 1
134 cr2.color_ramp.interpolation = 'EASE'
135 cr2.location = (2196.6, 415)
136 cr3 = nt.nodes.new(type="ShaderNodeValToRGB")
137 cr3.color_ramp.elements[0].position = 0.779
138 cr3.color_ramp.elements[1].position = 1
139 cr3.color_ramp.interpolation = 'EASE'
140 cr3.location = (2196.6, 415)
142 mat1 = nt.nodes.new(type="ShaderNodeMath")
143 mat1.operation = 'MULTIPLY'
144 mat1.inputs[1].default_value = 0.2
145 mat1.location = (2196.6, 685)
146 mat2 = nt.nodes.new(type="ShaderNodeMath")
147 mat2.operation = 'MULTIPLY'
148 mat2.inputs[1].default_value = 2
149 mat2.location = (3294, 685)
150 mat3 = nt.nodes.new(type="ShaderNodeMath")
151 mat3.operation = 'MULTIPLY'
152 mat3.inputs[1].default_value = 40.9
153 mat3.location = (2745.24, 415)
154 mat4 = nt.nodes.new(type="ShaderNodeMath")
155 mat4.operation = 'SUBTRACT'
156 mat4.inputs[1].default_value = 1
157 mat4.location = (3294, 415)
158 ntl(mat2.inputs[0], mat1.outputs[0])
159 ntl(mat4.inputs[0], mat3.outputs[0])
160 ntl(mat1.inputs[0], cr3.outputs[0])
161 ntl(mat3.inputs[0], cr2.outputs[0])
163 soft = nt.nodes.new(type="ShaderNodeMixRGB")
164 soft.name = "Soft_hard"
165 soft.location = (3819.3, 550)
166 soft_1 = nt.nodes.new(type="ShaderNodeMixRGB")
167 soft_1.location = (3819.3, 185)
168 soft.inputs[0].default_value = 1
169 soft_1.inputs[0].default_value = 0.466
170 ntl(soft.inputs[1], mat2.outputs[0])
171 ntl(soft.inputs[2], mat4.outputs[0])
172 ntl(soft_1.inputs[1], mat2.outputs[0])
173 ntl(soft_1.inputs[2], cr2.outputs[0])
175 mix1 = nt.nodes.new(type="ShaderNodeMixRGB")
176 mix1.blend_type = 'MULTIPLY'
177 mix1.inputs[0].default_value = 1
178 mix1.location = (4344.3, 630)
179 mix1_1 = nt.nodes.new(type="ShaderNodeMixRGB")
180 mix1_1.blend_type = 'MULTIPLY'
181 mix1_1.inputs[0].default_value = 1
182 mix1_1.location = (4344.3, 90)
184 mix2 = nt.nodes.new(type="ShaderNodeMixRGB")
185 mix2.location = (4782, 610)
186 mix2_1 = nt.nodes.new(type="ShaderNodeMixRGB")
187 mix2_1.location = (5131.8, 270)
188 mix2.inputs[1].default_value = (0, 0, 0, 1)
189 mix2.inputs[2].default_value = (32, 22, 14, 200)
190 mix2_1.inputs[1].default_value = (0, 0, 0, 1)
191 mix2_1.inputs[2].default_value = (1, 0.820, 0.650, 1)
193 ntl(mix1.inputs[1], soft.outputs[0])
194 ntl(mix1_1.inputs[1], soft_1.outputs[0])
195 ntl(mix2.inputs[0], mix1.outputs[0])
196 ntl(mix2_1.inputs[0], mix1_1.outputs[0])
198 gam = nt.nodes.new(type="ShaderNodeGamma")
199 gam.inputs[1].default_value = 2.3
200 gam.location = (5131.8, 610)
202 gam2 = nt.nodes.new(type="ShaderNodeGamma")
203 gam2.name = "Sun_value"
204 gam2.inputs[1].default_value = 1
205 gam2.location = (5524.5, 610)
207 gam3 = nt.nodes.new(type="ShaderNodeGamma")
208 gam3.name = "Shadow_color_saturation"
209 gam3.inputs[1].default_value = 1
210 gam3.location = (5524.5, 880)
212 sunopa = nt.nodes.new(type="ShaderNodeMixRGB")
213 sunopa.blend_type = 'ADD'
214 sunopa.inputs[0].default_value = 1
215 sunopa.location = (5940.6, 610)
216 sunopa_1 = nt.nodes.new(type="ShaderNodeMixRGB")
217 sunopa_1.blend_type = 'ADD'
218 sunopa_1.inputs[0].default_value = 1
219 sunopa_1.location = (5524.5, 340)
221 combine = nt.nodes.new(type="ShaderNodeMixRGB")
222 combine.location = (6313.8, 360)
223 ntl(combine.inputs[1], sunopa.outputs[0])
224 ntl(combine.inputs[2], sunopa_1.outputs[0])
225 lp = nt.nodes.new(type="ShaderNodeLightPath")
226 lp.location = (5940.6, 130)
227 ntl(combine.inputs[0], lp.outputs[0])
229 ntl(gam2.inputs[0], gam.outputs[0])
230 ntl(gam.inputs[0], mix2.outputs[0])
231 ntl(bg.inputs[0], combine.outputs[0])
233 map2 = nt.nodes.new(type="ShaderNodeMapping")
234 map2.inputs['Scale'].default_value[2] = 6.00
235 map2.inputs['Scale'].default_value[0] = 1.5
236 map2.inputs['Scale'].default_value[1] = 1.5
237 map2.location = (2196.6, 1510)
239 n1 = nt.nodes.new(type="ShaderNodeTexNoise")
240 n1.inputs['Scale'].default_value = 3.8
241 n1.inputs['Detail'].default_value = 2.4
242 n1.inputs['Distortion'].default_value = 0.5
243 n1.location = (2745.24, 1780)
245 n2 = nt.nodes.new(type="ShaderNodeTexNoise")
246 n2.inputs['Scale'].default_value = 2.0
247 n2.inputs['Detail'].default_value = 10
248 n2.inputs['Distortion'].default_value = 0.2
249 n2.location = (2745.24, 1510)
251 ntl(n2.inputs[0], map2.outputs[0])
252 ntl(n1.inputs[0], map2.outputs[0])
254 sc1 = nt.nodes.new(type="ShaderNodeValToRGB")
255 sc1.location = (3294, 1780)
256 sc2 = nt.nodes.new(type="ShaderNodeValToRGB")
257 sc2.location = (3294, 1510)
258 sc3 = nt.nodes.new(type="ShaderNodeValToRGB")
259 sc3.location = (3819.3, 820)
260 sc3_1 = nt.nodes.new(type="ShaderNodeValToRGB")
261 sc3_1.location = (4344.3, 1360)
262 sc4 = nt.nodes.new(type="ShaderNodeValToRGB")
263 sc4.location = (3819.3, 1090)
265 sc1.color_ramp.elements[1].position = 0.649
266 sc1.color_ramp.elements[0].position = 0.408
268 sc2.color_ramp.elements[1].position = 0.576
269 sc2.color_ramp.elements[0].position = 0.408
271 sc3.color_ramp.elements.new(0.5)
272 sc3.color_ramp.elements[2].position = 0.435
274 sc3.color_ramp.elements[1].position = 0.160
275 sc3.color_ramp.elements[0].position = 0.027
277 sc3.color_ramp.elements[1].color = (1, 1, 1, 1)
278 sc3.color_ramp.elements[0].color = (0.419, 0.419, 0.419, 0.419)
280 sc3.color_ramp.elements[0].position = 0.0
281 sc4.color_ramp.elements[0].position = 0.0
282 sc4.color_ramp.elements[1].position = 0.469
283 sc4.color_ramp.elements[1].color = (0, 0, 0, 1)
284 sc4.color_ramp.elements[0].color = (1, 1, 0.917412, 1)
286 sc3_1.color_ramp.elements.new(0.5)
287 sc3_1.color_ramp.elements[2].position = 0.435
289 sc3_1.color_ramp.elements[1].position = 0.187
290 sc3_1.color_ramp.elements[1].color = (1, 1, 1, 1)
291 sc3_1.color_ramp.elements[0].color = (0, 0, 0, 0)
292 sc3_1.color_ramp.elements[0].position = 0.0
294 smix1 = nt.nodes.new(type="ShaderNodeMixRGB")
295 smix1.location = (3819.3, 1550)
296 smix1.name = "Cloud_color"
297 smix2 = nt.nodes.new(type="ShaderNodeMixRGB")
298 smix2.location = (4344.3, 1630)
299 smix2.name = "Cloud_density"
300 smix2_1 = nt.nodes.new(type="ShaderNodeMixRGB")
301 smix2_1.location = (4782, 1360)
303 smix3 = nt.nodes.new(type="ShaderNodeMixRGB")
304 smix3.location = (4344.3, 1090)
305 smix3.name = "Sky_and_Horizon_colors"
307 smix4 = nt.nodes.new(type="ShaderNodeMixRGB")
308 smix4.location = (4782, 880)
310 smix5 = nt.nodes.new(type="ShaderNodeMixRGB")
311 smix5.name = "Cloud_opacity"
312 smix5.location = (5131.8, 880)
314 smix1.inputs[1].default_value = (1, 1, 1, 1)
315 smix1.inputs[2].default_value = (0, 0, 0, 1)
316 smix2.inputs[0].default_value = 0.267
317 smix2.blend_type = 'MULTIPLY'
318 smix2_1.inputs[0].default_value = 1
319 smix2_1.blend_type = 'MULTIPLY'
321 smix3.inputs[1].default_value = (0.434, 0.838, 1, 1)
322 smix3.inputs[2].default_value = (0.962, 0.822, 0.822, 1)
323 smix4.blend_type = 'MULTIPLY'
324 smix4.inputs[0].default_value = 1
325 smix5.blend_type = 'SCREEN'
326 smix5.inputs[0].default_value = 1
328 srgb = nt.nodes.new(type="ShaderNodeSeparateRGB")
329 srgb.location = (786.54, 1370)
330 aniadd = nt.nodes.new(type="ShaderNodeMath")
331 aniadd.location = (1220.16, 1235)
332 crgb = nt.nodes.new(type="ShaderNodeCombineRGB")
333 crgb.location = (1671.33, 1510)
334 sunrgb = nt.nodes.new(type="ShaderNodeMixRGB")
335 sunrgb.name = "Sun_color"
337 sunrgb.blend_type = 'MULTIPLY'
338 sunrgb.inputs[2].default_value = (32, 30, 30, 200)
339 sunrgb.inputs[0].default_value = 1
340 sunrgb.location = (4344.3, 360)
342 ntl(mix2.inputs[2], sunrgb.outputs[0])
344 ntl(smix1.inputs[0], sc2.outputs[0])
345 ntl(smix2.inputs[1], smix1.outputs[0])
346 ntl(smix2.inputs[2], sc1.outputs[0])
347 ntl(smix2_1.inputs[2], sc3_1.outputs[0])
348 ntl(smix3.inputs[0], sc4.outputs[0])
349 ntl(smix4.inputs[2], smix3.outputs[0])
350 ntl(smix4.inputs[1], sc3.outputs[0])
351 ntl(smix5.inputs[1], smix4.outputs[0])
352 ntl(smix2_1.inputs[1], smix2.outputs[0])
353 ntl(smix5.inputs[2], smix2_1.outputs[0])
354 ntl(sunopa.inputs[1], gam3.outputs[0])
355 ntl(gam3.inputs[0], smix5.outputs[0])
356 ntl(mix1.inputs[2], sc3.outputs[0])
357 ntl(sunopa.inputs[2], gam2.outputs[0])
359 ntl(sc1.inputs[0], n1.outputs['Fac'])
360 ntl(sc2.inputs[0], n2.outputs['Fac'])
362 skynor = nt.nodes.new(type="ShaderNodeNormal")
363 skynor.location = (3294, 1070)
365 ntl(sc3.inputs[0], skynor.outputs[1])
366 ntl(sc4.inputs[0], skynor.outputs[1])
367 ntl(sc3_1.inputs[0], skynor.outputs[1])
368 ntl(map2.inputs[0], crgb.outputs[0])
369 ntl(skynor.inputs[0], tcor.outputs[0])
370 ntl(mix1_1.inputs[2], sc3.outputs[0])
371 ntl(srgb.inputs[0], tcor.outputs[0])
372 ntl(crgb.inputs[1], srgb.outputs[1])
373 ntl(crgb.inputs[2], srgb.outputs[2])
374 ntl(aniadd.inputs[1], srgb.outputs[0])
375 ntl(crgb.inputs[0], aniadd.outputs[0])
377 ntl(cr1.inputs[0], nor.outputs[1])
378 ntl(cr2.inputs[0], cr1.outputs[0])
379 ntl(cr3.inputs[0], nor.outputs[1])
380 ntl(nor.inputs[0], map1.outputs[0])
381 ntl(map1.inputs[0], tcor.outputs[0])
382 ntl(sunopa_1.inputs[1], smix5.outputs[0])
383 ntl(sunopa_1.inputs[2], mix2_1.outputs[0])
385 world_out = self.get_node_types(nt, "OUTPUT_WORLD")
386 world_out.location = (7167.3, 360)
388 except Exception as e:
389 error_handlers(self, e, "Make a Procedural sky has failed")
391 return {"CANCELLED"}
393 return {'FINISHED'}
396 def draw_world_settings(col, context):
397 get_world = context.scene.world
398 stored_name = context.scene.dynamic_sky_name
399 get_world_keys = bpy.data.worlds.keys()
401 if stored_name not in get_world_keys or len(get_world_keys) < 1:
402 col.label(text="The {} World could not".format(stored_name),
403 icon="INFO")
404 col.label(text="be found in the Worlds' Data", icon="BLANK1")
405 return
407 elif not (get_world and get_world.name == stored_name):
408 col.label(text="Please select the World", icon="INFO")
409 col.label(text="named {}".format(stored_name), icon="BLANK1")
410 col.label(text="from the Properties > World", icon="BLANK1")
411 return
413 pick_world = bpy.data.worlds[stored_name]
414 try:
415 m = pick_world.node_tree.nodes[28]
417 m = pick_world.node_tree.nodes['Sky_and_Horizon_colors'].inputs[1]
418 n = pick_world.node_tree.nodes['Sky_and_Horizon_colors'].inputs[2]
419 c = pick_world.node_tree.nodes['Cloud_color'].inputs[1]
420 o = pick_world.node_tree.nodes['Cloud_opacity'].inputs[0]
421 d = pick_world.node_tree.nodes['Cloud_density'].inputs[0]
422 so = pick_world.node_tree.nodes['Sun_value'].inputs[1]
423 so2 = pick_world.node_tree.nodes['Shadow_color_saturation'].inputs[1]
424 no = pick_world.node_tree.nodes['Sky_normal'].outputs[0]
425 sof = pick_world.node_tree.nodes['Soft_hard'].inputs[0]
426 bgp = pick_world.node_tree.nodes['Scene_Brightness'].inputs[1]
428 suc = pick_world.node_tree.nodes['Sun_color'].inputs[1]
429 except:
430 col.label(text="Please Create a new World", icon="INFO")
431 col.label(text="seems that there was already", icon="BLANK1")
432 col.label(text="one called {}".format(stored_name), icon="BLANK1")
433 return
435 col.label(text="World: %s" % stored_name)
436 col.separator()
438 col.label(text="Scene Control")
439 col.prop(bgp, "default_value", text="Brightness")
440 col.prop(so2, "default_value", text="Shadow color saturation")
442 col.label(text="Sky Control")
443 col.prop(m, "default_value", text="Sky color")
444 col.prop(n, "default_value", text="Horizon Color")
445 col.prop(c, "default_value", text="Cloud color")
446 col.prop(o, "default_value", text="Cloud opacity")
447 col.prop(d, "default_value", text="Cloud density")
449 col.label(text="Sun Control")
450 col.prop(suc, "default_value", text="")
451 col.prop(so, "default_value", text="Sun value")
452 col.prop(sof, "default_value", text="Soft hard")
454 col.prop(no, "default_value", text="")
457 class Dynapanel(Panel):
458 bl_label = "Dynamic sky"
459 bl_idname = "DYNSKY_PT_tools"
460 bl_space_type = 'VIEW_3D'
461 bl_region_type = 'UI'
462 bl_context = "objectmode"
463 bl_category = "Create"
464 bl_options = {'DEFAULT_CLOSED'}
466 def draw(self, context):
467 layout = self.layout
468 layout.operator("sky.dyn", text="Create", icon='MAT_SPHERE_SKY')
470 col = layout.column()
471 draw_world_settings(col, context)
474 def register():
475 bpy.utils.register_class(Dynapanel)
476 bpy.utils.register_class(dsky)
477 bpy.types.Scene.dynamic_sky_name = StringProperty(
478 name="",
479 default="Dynamic"
483 def unregister():
484 bpy.utils.unregister_class(Dynapanel)
485 bpy.utils.unregister_class(dsky)
486 del bpy.types.Scene.dynamic_sky_name
489 if __name__ == "__main__":
490 register()