1 # SPDX-FileCopyrightText: 2019-2023 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
6 from bpy
.types
import Operator
7 from bl_operators
.presets
import AddPresetBase
8 from bl_ui
.utils
import PresetPanel
10 from .sun_calc
import 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 a new preset for Sun Position settings'''
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
.operator("world.sunpos_show_hdr", icon
='LIGHT_SUN')
115 def draw_normal_mode_panel(self
, context
):
116 sun_props
= context
.scene
.sun_pos_properties
117 addon_prefs
= context
.preferences
.addons
[__package__
].preferences
120 col
= layout
.column(align
=True)
121 col
.prop(sun_props
, "sun_object")
124 col
.prop(sun_props
, "object_collection")
125 if sun_props
.object_collection
:
126 col
.prop(sun_props
, "object_collection_type")
127 if sun_props
.object_collection_type
== 'DIURNAL':
128 col
.prop(sun_props
, "time_spread")
131 if context
.scene
.world
is not None:
132 if context
.scene
.world
.node_tree
is not None:
133 col
.prop_search(sun_props
, "sky_texture",
134 context
.scene
.world
.node_tree
, "nodes")
136 col
.label(text
="Please activate Use Nodes in the World panel.",
139 col
.label(text
="Please select World in the World panel.",
142 if addon_prefs
.show_overlays
:
143 col
= layout
.column(align
=True, heading
="Show")
144 col
.prop(sun_props
, "show_north", text
="North")
145 col
.prop(sun_props
, "show_analemmas", text
="Analemmas")
146 col
.prop(sun_props
, "show_surface", text
="Surface")
148 if addon_prefs
.show_refraction
:
149 col
= layout
.column(align
=True, heading
="Use")
150 col
.prop(sun_props
, "use_refraction", text
="Refraction")
153 class SUNPOS_PT_Location(bpy
.types
.Panel
):
154 bl_space_type
= "PROPERTIES"
155 bl_region_type
= "WINDOW"
157 bl_label
= "Location"
158 bl_parent_id
= "SUNPOS_PT_Panel"
161 def poll(self
, context
):
162 sun_props
= context
.scene
.sun_pos_properties
163 return sun_props
.usage_mode
!= "HDR"
165 def draw(self
, context
):
167 layout
.use_property_split
= True
169 sun_props
= context
.scene
.sun_pos_properties
170 addon_prefs
= context
.preferences
.addons
[__package__
].preferences
172 col
= layout
.column(align
=True)
173 col
.prop(sun_props
, "coordinates", icon
='URL')
174 col
.prop(sun_props
, "latitude")
175 col
.prop(sun_props
, "longitude")
179 col
= layout
.column(align
=True)
180 col
.prop(sun_props
, "north_offset", text
="North Offset")
182 if addon_prefs
.show_az_el
:
183 col
= layout
.column(align
=True)
184 col
.prop(sun_props
, "sun_elevation", text
="Elevation")
185 col
.prop(sun_props
, "sun_azimuth", text
="Azimuth")
188 col
= layout
.column()
189 col
.prop(sun_props
, "sun_distance")
193 class SUNPOS_PT_Time(bpy
.types
.Panel
):
194 bl_space_type
= "PROPERTIES"
195 bl_region_type
= "WINDOW"
198 bl_parent_id
= "SUNPOS_PT_Panel"
201 def poll(self
, context
):
202 sun_props
= context
.scene
.sun_pos_properties
203 return sun_props
.usage_mode
!= "HDR"
205 def draw(self
, context
):
207 layout
.use_property_split
= True
209 sun_props
= context
.scene
.sun_pos_properties
210 addon_prefs
= context
.preferences
.addons
[__package__
].preferences
212 col
= layout
.column(align
=True)
213 col
.prop(sun_props
, "use_day_of_year")
214 if sun_props
.use_day_of_year
:
215 col
.prop(sun_props
, "day_of_year")
217 col
.prop(sun_props
, "day")
218 col
.prop(sun_props
, "month")
219 col
.prop(sun_props
, "year")
222 col
= layout
.column(align
=True)
223 col
.prop(sun_props
, "time", text
="Time", text_ctxt
="Hour")
224 col
.prop(sun_props
, "UTC_zone")
225 col
.prop(sun_props
, "use_daylight_savings")
228 local_time
= format_time(sun_props
.time
,
229 sun_props
.use_daylight_savings
)
230 utc_time
= format_time(sun_props
.time
,
231 sun_props
.use_daylight_savings
,
234 col
= layout
.column(align
=True)
235 col
.alignment
= 'CENTER'
237 split
= col
.split(factor
=0.5, align
=True)
238 sub
= split
.column(align
=True)
239 sub
.alignment
= 'RIGHT'
240 sub
.label(text
="Time Local:")
241 sub
.label(text
="UTC:")
243 sub
= split
.column(align
=True)
244 sub
.label(text
=local_time
)
245 sub
.label(text
=utc_time
)
248 if addon_prefs
.show_rise_set
:
249 sunrise
= format_hms(sun
.sunrise
)
250 sunset
= format_hms(sun
.sunset
)
252 col
= layout
.column(align
=True)
253 col
.alignment
= 'CENTER'
255 split
= col
.split(factor
=0.5, align
=True)
256 sub
= split
.column(align
=True)
257 sub
.alignment
= 'RIGHT'
258 sub
.label(text
="Sunrise:")
259 sub
.label(text
="Sunset:")
261 sub
= split
.column(align
=True)
262 sub
.label(text
=sunrise
)
263 sub
.label(text
=sunset
)