1 # SPDX-License-Identifier: GPL-2.0-or-later
4 from bpy
.types
import Operator
, Menu
5 from bl_operators
.presets
import AddPresetBase
6 from bl_ui
.utils
import PresetPanel
8 from math
import degrees
10 from .sun_calc
import format_lat_long
, format_time
, format_hms
, sun
13 # -------------------------------------------------------------------
14 # Choice list of places, month and day at 12:00 noon
15 # -------------------------------------------------------------------
18 class SUNPOS_PT_Presets(PresetPanel
, bpy
.types
.Panel
):
19 bl_label
= "Sun Position Presets"
20 preset_subdir
= "operator/sun_position"
21 preset_operator
= "script.execute_preset"
22 preset_add_operator
= "world.sunpos_add_preset"
25 class SUNPOS_OT_AddPreset(AddPresetBase
, Operator
):
26 '''Add Sun Position preset'''
27 bl_idname
= "world.sunpos_add_preset"
28 bl_label
= "Add Sun Position preset"
29 preset_menu
= "SUNPOS_PT_Presets"
31 # variable used for all preset values
33 "sun_props = bpy.context.scene.sun_pos_properties"
36 # properties to store in the preset
43 "sun_props.use_daylight_savings",
45 "sun_props.longitude",
48 # where to store the preset
49 preset_subdir
= "operator/sun_position"
52 # -------------------------------------------------------------------
54 # Draw the Sun Panel, sliders, et. al.
56 # -------------------------------------------------------------------
58 class SUNPOS_PT_Panel(bpy
.types
.Panel
):
59 bl_space_type
= "PROPERTIES"
60 bl_region_type
= "WINDOW"
62 bl_label
= "Sun Position"
63 bl_options
= {'DEFAULT_CLOSED'}
65 def draw_header_preset(self
, _context
):
66 SUNPOS_PT_Presets
.draw_panel_header(self
.layout
)
68 def draw(self
, context
):
69 sun_props
= context
.scene
.sun_pos_properties
71 layout
.use_property_split
= True
72 layout
.use_property_decorate
= False
74 layout
.prop(sun_props
, "usage_mode", expand
=True)
77 if sun_props
.usage_mode
== "HDR":
78 self
.draw_environment_mode_panel(context
)
80 self
.draw_normal_mode_panel(context
)
82 def draw_environment_mode_panel(self
, context
):
83 sun_props
= context
.scene
.sun_pos_properties
86 col
= layout
.column(align
=True)
87 col
.prop_search(sun_props
, "sun_object",
88 context
.view_layer
, "objects")
89 if context
.scene
.world
is not None:
90 if context
.scene
.world
.node_tree
is not None:
91 col
.prop_search(sun_props
, "hdr_texture",
92 context
.scene
.world
.node_tree
, "nodes")
94 col
.label(text
="Please activate Use Nodes in the World panel.",
97 col
.label(text
="Please select World in the World panel.",
100 layout
.use_property_decorate
= True
102 col
= layout
.column(align
=True)
103 col
.prop(sun_props
, "bind_to_sun", text
="Bind Texture to Sun")
104 col
.prop(sun_props
, "hdr_azimuth")
105 row
= col
.row(align
=True)
106 row
.active
= not sun_props
.bind_to_sun
107 row
.prop(sun_props
, "hdr_elevation")
108 col
.prop(sun_props
, "sun_distance")
111 col
= layout
.column(align
=True)
112 row
= col
.row(align
=True)
113 row
.enabled
= not sun_props
.bind_to_sun
114 row
.operator("world.sunpos_show_hdr", icon
='LIGHT_SUN')
116 def draw_normal_mode_panel(self
, context
):
117 sun_props
= context
.scene
.sun_pos_properties
118 addon_prefs
= context
.preferences
.addons
[__package__
].preferences
121 col
= layout
.column(align
=True)
122 col
.prop(sun_props
, "sun_object")
125 col
.prop(sun_props
, "object_collection")
126 if sun_props
.object_collection
:
127 col
.prop(sun_props
, "object_collection_type")
128 if sun_props
.object_collection_type
== 'DIURNAL':
129 col
.prop(sun_props
, "time_spread")
132 if context
.scene
.world
is not None:
133 if context
.scene
.world
.node_tree
is not None:
134 col
.prop_search(sun_props
, "sky_texture",
135 context
.scene
.world
.node_tree
, "nodes")
137 col
.label(text
="Please activate Use Nodes in the World panel.",
140 col
.label(text
="Please select World in the World panel.",
143 if addon_prefs
.show_overlays
:
144 col
= layout
.column(align
=True, heading
="Show")
145 col
.prop(sun_props
, "show_north", text
="North")
146 col
.prop(sun_props
, "show_analemmas", text
="Analemmas")
147 col
.prop(sun_props
, "show_surface", text
="Surface")
149 if addon_prefs
.show_refraction
:
150 col
= layout
.column(align
=True, heading
="Use")
151 col
.prop(sun_props
, "use_refraction", text
="Refraction")
154 class SUNPOS_PT_Location(bpy
.types
.Panel
):
155 bl_space_type
= "PROPERTIES"
156 bl_region_type
= "WINDOW"
158 bl_label
= "Location"
159 bl_parent_id
= "SUNPOS_PT_Panel"
162 def poll(self
, context
):
163 sun_props
= context
.scene
.sun_pos_properties
164 return sun_props
.usage_mode
!= "HDR"
166 def draw(self
, context
):
168 layout
.use_property_split
= True
170 sun_props
= context
.scene
.sun_pos_properties
171 addon_prefs
= context
.preferences
.addons
[__package__
].preferences
173 col
= layout
.column(align
=True)
174 col
.prop(sun_props
, "coordinates", icon
='URL')
175 col
.prop(sun_props
, "latitude")
176 col
.prop(sun_props
, "longitude")
180 col
= layout
.column(align
=True)
181 col
.prop(sun_props
, "north_offset", text
="North Offset")
183 if addon_prefs
.show_az_el
:
184 col
= layout
.column(align
=True)
185 col
.prop(sun_props
, "sun_elevation", text
="Elevation")
186 col
.prop(sun_props
, "sun_azimuth", text
="Azimuth")
189 col
= layout
.column()
190 col
.prop(sun_props
, "sun_distance")
194 class SUNPOS_PT_Time(bpy
.types
.Panel
):
195 bl_space_type
= "PROPERTIES"
196 bl_region_type
= "WINDOW"
199 bl_parent_id
= "SUNPOS_PT_Panel"
202 def poll(self
, context
):
203 sun_props
= context
.scene
.sun_pos_properties
204 return sun_props
.usage_mode
!= "HDR"
206 def draw(self
, context
):
208 layout
.use_property_split
= True
210 sun_props
= context
.scene
.sun_pos_properties
211 addon_prefs
= context
.preferences
.addons
[__package__
].preferences
213 col
= layout
.column(align
=True)
214 col
.prop(sun_props
, "use_day_of_year")
215 if sun_props
.use_day_of_year
:
216 col
.prop(sun_props
, "day_of_year")
218 col
.prop(sun_props
, "day")
219 col
.prop(sun_props
, "month")
220 col
.prop(sun_props
, "year")
223 col
= layout
.column(align
=True)
224 col
.prop(sun_props
, "time", text
="Time", text_ctxt
="Hour")
225 col
.prop(sun_props
, "UTC_zone")
226 col
.prop(sun_props
, "use_daylight_savings")
229 local_time
= format_time(sun_props
.time
,
230 sun_props
.use_daylight_savings
)
231 utc_time
= format_time(sun_props
.time
,
232 sun_props
.use_daylight_savings
,
235 col
= layout
.column(align
=True)
236 col
.alignment
= 'CENTER'
238 split
= col
.split(factor
=0.5, align
=True)
239 sub
= split
.column(align
=True)
240 sub
.alignment
= 'RIGHT'
241 sub
.label(text
="Time Local:")
242 sub
.label(text
="UTC:")
244 sub
= split
.column(align
=True)
245 sub
.label(text
=local_time
)
246 sub
.label(text
=utc_time
)
249 if addon_prefs
.show_rise_set
:
250 sunrise
= format_hms(sun
.sunrise
)
251 sunset
= format_hms(sun
.sunset
)
253 col
= layout
.column(align
=True)
254 col
.alignment
= 'CENTER'
256 split
= col
.split(factor
=0.5, align
=True)
257 sub
= split
.column(align
=True)
258 sub
.alignment
= 'RIGHT'
259 sub
.label(text
="Sunrise:")
260 sub
.label(text
="Sunset:")
262 sub
= split
.column(align
=True)
263 sub
.label(text
=sunrise
)
264 sub
.label(text
=sunset
)