1 ### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
20 from bpy
.types
import Operator
, Menu
21 from bl_operators
.presets
import AddPresetBase
24 from .sun_calc
import (format_lat_long
, format_time
, format_hms
, sun
)
27 # -------------------------------------------------------------------
28 # Choice list of places, month and day at 12:00 noon
29 # -------------------------------------------------------------------
32 class SUNPOS_MT_Presets(Menu
):
33 bl_label
= "Sun Position Presets"
34 preset_subdir
= "operator/sun_position"
35 preset_operator
= "script.execute_preset"
36 draw
= Menu
.draw_preset
39 class SUNPOS_OT_AddPreset(AddPresetBase
, Operator
):
40 '''Add Sun Position preset'''
41 bl_idname
= "world.sunpos_add_preset"
42 bl_label
= "Add Sun Position preset"
43 preset_menu
= "SUNPOS_MT_Presets"
45 # variable used for all preset values
47 "sun_props = bpy.context.scene.sun_pos_properties"
50 # properties to store in the preset
57 "sun_props.use_daylight_savings",
59 "sun_props.longitude",
62 # where to store the preset
63 preset_subdir
= "operator/sun_position"
66 # -------------------------------------------------------------------
68 # Draw the Sun Panel, sliders, et. al.
70 # -------------------------------------------------------------------
72 class SUNPOS_PT_Panel(bpy
.types
.Panel
):
73 bl_space_type
= "PROPERTIES"
74 bl_region_type
= "WINDOW"
76 bl_label
= "Sun Position"
77 bl_options
= {'DEFAULT_CLOSED'}
79 def draw(self
, context
):
80 sp
= context
.scene
.sun_pos_properties
81 p
= context
.preferences
.addons
[__package__
].preferences
83 self
.draw_panel(context
, sp
, p
, layout
)
85 def draw_panel(self
, context
, sp
, p
, layout
):
86 col
= self
.layout
.column(align
=True)
87 col
.label(text
="Usage Mode")
89 row
.prop(sp
, "usage_mode", expand
=True)
91 if sp
.usage_mode
== "HDR":
92 self
.draw_environ_mode_panel(context
, sp
, p
, layout
)
94 self
.draw_normal_mode_panel(context
, sp
, p
, layout
)
96 def draw_environ_mode_panel(self
, context
, sp
, p
, layout
):
97 flow
= layout
.grid_flow(row_major
=True, columns
=0, even_columns
=True,
98 even_rows
=False, align
=False)
100 col
= flow
.column(align
=True)
101 col
.label(text
="Environment Texture")
103 if context
.scene
.world
is not None:
104 if context
.scene
.world
.node_tree
is not None:
105 col
.prop_search(sp
, "hdr_texture",
106 context
.scene
.world
.node_tree
, "nodes", text
="")
108 col
.label(text
="Please activate Use Nodes in the World panel.",
111 col
.label(text
="Please select World in the World panel.",
116 col
= flow
.column(align
=True)
117 col
.label(text
="Sun Object")
118 col
.prop_search(sp
, "sun_object",
119 context
.view_layer
, "objects", text
="")
122 col
= flow
.column(align
=True)
123 col
.prop(sp
, "sun_distance")
124 if not sp
.bind_to_sun
:
125 col
.prop(sp
, "hdr_elevation")
126 col
.prop(sp
, "hdr_azimuth")
129 col
= flow
.column(align
=True)
131 prop_text
="Release binding"
133 prop_text
="Bind Texture to Sun "
134 col
.prop(sp
, "bind_to_sun", toggle
=True, icon
="CONSTRAINT",
137 row
= col
.row(align
=True)
138 row
.enabled
= not sp
.bind_to_sun
139 row
.operator("world.sunpos_show_hdr", icon
='LIGHT_SUN')
141 def draw_normal_mode_panel(self
, context
, sp
, p
, layout
):
142 if p
.show_time_place
:
143 row
= layout
.row(align
=True)
144 row
.menu(SUNPOS_MT_Presets
.__name
__, text
=SUNPOS_MT_Presets
.bl_label
)
145 row
.operator(SUNPOS_OT_AddPreset
.bl_idname
, text
="", icon
='ADD')
146 row
.operator(SUNPOS_OT_AddPreset
.bl_idname
, text
="", icon
='REMOVE').remove_active
= True
148 col
= layout
.column(align
=True)
149 col
.use_property_split
= True
150 col
.use_property_decorate
= False
151 col
.prop(sp
, "sun_object")
154 col
.prop(sp
, "object_collection")
155 if sp
.object_collection
:
156 col
.prop(sp
, "object_collection_type")
157 if sp
.object_collection_type
== 'DIURNAL':
158 col
.prop(sp
, "time_spread")
161 if context
.scene
.world
is not None:
162 if context
.scene
.world
.node_tree
is not None:
163 col
.prop_search(sp
, "sky_texture",
164 context
.scene
.world
.node_tree
, "nodes")
166 col
.label(text
="Please activate Use Nodes in the World panel.",
169 col
.label(text
="Please select World in the World panel.",
172 class SUNPOS_PT_Location(bpy
.types
.Panel
):
173 bl_space_type
= "PROPERTIES"
174 bl_region_type
= "WINDOW"
176 bl_label
= "Location"
177 bl_parent_id
= "SUNPOS_PT_Panel"
180 def poll(self
, context
):
181 sp
= context
.scene
.sun_pos_properties
182 return sp
.usage_mode
!= "HDR"
184 def draw(self
, context
):
186 sp
= context
.scene
.sun_pos_properties
187 p
= context
.preferences
.addons
[__package__
].preferences
189 col
= layout
.column(align
=True)
190 col
.label(text
="Enter Coordinates")
191 col
.prop(sp
, "co_parser", text
='', icon
='URL')
195 flow
= layout
.grid_flow(row_major
=True, columns
=0, even_columns
=True, even_rows
=False, align
=False)
197 col
= flow
.column(align
=True)
198 col
.prop(sp
, "latitude")
201 row
.alignment
= 'RIGHT'
202 row
.label(text
=format_lat_long(sp
.latitude
, True))
204 col
= flow
.column(align
=True)
205 col
.prop(sp
, "longitude")
208 row
.alignment
= 'RIGHT'
209 row
.label(text
=format_lat_long(sp
.longitude
, False))
213 col
= flow
.column(align
=True)
214 col
.prop(sp
, "show_north", toggle
=True)
215 col
.prop(sp
, "north_offset")
219 col
= flow
.column(align
=True)
221 row
.alignment
= 'RIGHT'
222 row
.label(text
="Azimuth: " +
223 str(round(sun
.azimuth
, 3)) + "°")
225 row
.alignment
= 'RIGHT'
226 row
.label(text
="Elevation: " +
227 str(round(sun
.elevation
, 3)) + "°")
230 if p
.show_refraction
:
232 col
.prop(sp
, "use_refraction")
236 col
.prop(sp
, "sun_distance")
239 class SUNPOS_PT_Time(bpy
.types
.Panel
):
240 bl_space_type
= "PROPERTIES"
241 bl_region_type
= "WINDOW"
244 bl_parent_id
= "SUNPOS_PT_Panel"
247 def poll(self
, context
):
248 sp
= context
.scene
.sun_pos_properties
249 return sp
.usage_mode
!= "HDR"
251 def draw(self
, context
):
253 sp
= context
.scene
.sun_pos_properties
254 p
= context
.preferences
.addons
[__package__
].preferences
256 flow
= layout
.grid_flow(row_major
=True, columns
=0, even_columns
=True, even_rows
=False, align
=False)
258 col
= flow
.column(align
=True)
259 col
.prop(sp
, "use_day_of_year",
261 if sp
.use_day_of_year
:
262 col
.prop(sp
, "day_of_year")
265 col
.prop(sp
, "month")
269 col
= flow
.column(align
=True)
271 col
.prop(sp
, "UTC_zone")
272 if p
.show_daylight_savings
:
273 col
.prop(sp
, "use_daylight_savings")
276 col
= flow
.column(align
=True)
277 lt
= format_time(sp
.time
,
278 p
.show_daylight_savings
and sp
.use_daylight_savings
,
280 ut
= format_time(sp
.time
,
281 p
.show_daylight_savings
and sp
.use_daylight_savings
,
284 col
.alignment
= 'CENTER'
285 col
.label(text
="Local: " + lt
, icon
='TIME')
286 col
.label(text
=" UTC: " + ut
, icon
='PREVIEW_RANGE')
289 col
= flow
.column(align
=True)
290 col
.alignment
= 'CENTER'
292 sr
= format_hms(sun
.sunrise
.time
)
293 ss
= format_hms(sun
.sunset
.time
)
294 tsr
= "Sunrise: " + sr
295 tss
= " Sunset: " + ss
296 col
.label(text
=tsr
, icon
='LIGHT_SUN')
297 col
.label(text
=tss
, icon
='SOLO_ON')