Sun Position: Cleanup: rename sp and prefs variables
[blender-addons.git] / sun_position / ui_sun.py
bloba1d90db9c29179cb185f8f37ce871a3ece65398b
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 import bpy
4 from bpy.types import Operator, Menu
5 from bl_operators.presets import AddPresetBase
6 import os
7 from math import degrees
9 from .sun_calc import format_lat_long, format_time, format_hms, sun
12 # -------------------------------------------------------------------
13 # Choice list of places, month and day at 12:00 noon
14 # -------------------------------------------------------------------
17 class SUNPOS_MT_Presets(Menu):
18 bl_label = "Sun Position Presets"
19 preset_subdir = "operator/sun_position"
20 preset_operator = "script.execute_preset"
21 draw = Menu.draw_preset
24 class SUNPOS_OT_AddPreset(AddPresetBase, Operator):
25 '''Add Sun Position preset'''
26 bl_idname = "world.sunpos_add_preset"
27 bl_label = "Add Sun Position preset"
28 preset_menu = "SUNPOS_MT_Presets"
30 # variable used for all preset values
31 preset_defines = [
32 "sun_props = bpy.context.scene.sun_pos_properties"
35 # properties to store in the preset
36 preset_values = [
37 "sun_props.day",
38 "sun_props.month",
39 "sun_props.time",
40 "sun_props.year",
41 "sun_props.UTC_zone",
42 "sun_props.use_daylight_savings",
43 "sun_props.latitude",
44 "sun_props.longitude",
47 # where to store the preset
48 preset_subdir = "operator/sun_position"
51 # -------------------------------------------------------------------
53 # Draw the Sun Panel, sliders, et. al.
55 # -------------------------------------------------------------------
57 class SUNPOS_PT_Panel(bpy.types.Panel):
58 bl_space_type = "PROPERTIES"
59 bl_region_type = "WINDOW"
60 bl_context = "world"
61 bl_label = "Sun Position"
62 bl_options = {'DEFAULT_CLOSED'}
64 def draw(self, context):
65 sun_props = context.scene.sun_pos_properties
66 layout = self.layout
67 layout.use_property_split = True
68 layout.use_property_decorate = False
70 layout.prop(sun_props, "usage_mode", expand=True)
71 layout.separator()
73 if sun_props.usage_mode == "HDR":
74 self.draw_environment_mode_panel(context)
75 else:
76 self.draw_normal_mode_panel(context)
78 def draw_environment_mode_panel(self, context):
79 sun_props = context.scene.sun_pos_properties
80 layout = self.layout
82 col = layout.column(align=True)
83 col.prop_search(sun_props, "sun_object",
84 context.view_layer, "objects")
85 if context.scene.world is not None:
86 if context.scene.world.node_tree is not None:
87 col.prop_search(sun_props, "hdr_texture",
88 context.scene.world.node_tree, "nodes")
89 else:
90 col.label(text="Please activate Use Nodes in the World panel.",
91 icon="ERROR")
92 else:
93 col.label(text="Please select World in the World panel.",
94 icon="ERROR")
96 layout.use_property_decorate = True
98 col = layout.column(align=True)
99 col.prop(sun_props, "bind_to_sun", text="Bind Texture to Sun")
100 col.prop(sun_props, "hdr_azimuth")
101 row = col.row(align=True)
102 row.active = not sun_props.bind_to_sun
103 row.prop(sun_props, "hdr_elevation")
104 col.prop(sun_props, "sun_distance")
105 col.separator()
107 col = layout.column(align=True)
108 row = col.row(align=True)
109 row.enabled = not sun_props.bind_to_sun
110 row.operator("world.sunpos_show_hdr", icon='LIGHT_SUN')
112 def draw_normal_mode_panel(self, context):
113 sun_props = context.scene.sun_pos_properties
114 addon_prefs = context.preferences.addons[__package__].preferences
115 layout = self.layout
117 if addon_prefs.show_time_place:
118 row = layout.row(align=True)
119 row.menu(SUNPOS_MT_Presets.__name__, text=SUNPOS_MT_Presets.bl_label)
120 row.operator(SUNPOS_OT_AddPreset.bl_idname, text="", icon='ADD')
121 row.operator(SUNPOS_OT_AddPreset.bl_idname, text="", icon='REMOVE').remove_active = True
123 col = layout.column(align=True)
124 col.prop(sun_props, "sun_object")
125 col.separator()
127 col.prop(sun_props, "object_collection")
128 if sun_props.object_collection:
129 col.prop(sun_props, "object_collection_type")
130 if sun_props.object_collection_type == 'DIURNAL':
131 col.prop(sun_props, "time_spread")
132 col.separator()
134 if context.scene.world is not None:
135 if context.scene.world.node_tree is not None:
136 col.prop_search(sun_props, "sky_texture",
137 context.scene.world.node_tree, "nodes")
138 else:
139 col.label(text="Please activate Use Nodes in the World panel.",
140 icon="ERROR")
141 else:
142 col.label(text="Please select World in the World panel.",
143 icon="ERROR")
145 if addon_prefs.show_overlays:
146 col = layout.column(align=True, heading="Show")
147 col.prop(sun_props, "show_north", text="North")
148 col.prop(sun_props, "show_analemmas", text="Analemmas")
149 col.prop(sun_props, "show_surface", text="Surface")
151 if addon_prefs.show_refraction:
152 col = layout.column(align=True, heading="Use")
153 col.prop(sun_props, "use_refraction", text="Refraction")
156 class SUNPOS_PT_Location(bpy.types.Panel):
157 bl_space_type = "PROPERTIES"
158 bl_region_type = "WINDOW"
159 bl_context = "world"
160 bl_label = "Location"
161 bl_parent_id = "SUNPOS_PT_Panel"
163 @classmethod
164 def poll(self, context):
165 sun_props = context.scene.sun_pos_properties
166 return sun_props.usage_mode != "HDR"
168 def draw(self, context):
169 layout = self.layout
170 layout.use_property_split = True
172 sun_props = context.scene.sun_pos_properties
173 addon_prefs = context.preferences.addons[__package__].preferences
175 col = layout.column(align=True)
176 col.prop(sun_props, "coordinates", icon='URL')
177 col.prop(sun_props, "latitude")
178 col.prop(sun_props, "longitude")
180 col.separator()
182 col = layout.column(align=True)
183 col.prop(sun_props, "north_offset", text="North Offset")
185 if addon_prefs.show_az_el:
186 col = layout.column(align=True)
187 col.prop(sun_props, "sun_elevation", text="Elevation")
188 col.prop(sun_props, "sun_azimuth", text="Azimuth")
189 col.separator()
191 col = layout.column()
192 col.prop(sun_props, "sun_distance")
193 col.separator()
196 class SUNPOS_PT_Time(bpy.types.Panel):
197 bl_space_type = "PROPERTIES"
198 bl_region_type = "WINDOW"
199 bl_context = "world"
200 bl_label = "Time"
201 bl_parent_id = "SUNPOS_PT_Panel"
203 @classmethod
204 def poll(self, context):
205 sun_props = context.scene.sun_pos_properties
206 return sun_props.usage_mode != "HDR"
208 def draw(self, context):
209 layout = self.layout
210 layout.use_property_split = True
212 sun_props = context.scene.sun_pos_properties
213 addon_prefs = context.preferences.addons[__package__].preferences
215 col = layout.column(align=True)
216 col.prop(sun_props, "use_day_of_year")
217 if sun_props.use_day_of_year:
218 col.prop(sun_props, "day_of_year")
219 else:
220 col.prop(sun_props, "day")
221 col.prop(sun_props, "month")
222 col.prop(sun_props, "year")
223 col.separator()
225 col = layout.column(align=True)
226 col.prop(sun_props, "time", text="Time", text_ctxt="Hour")
227 col.prop(sun_props, "UTC_zone")
228 if addon_prefs.show_daylight_savings:
229 col.prop(sun_props, "use_daylight_savings")
230 col.separator()
232 local_time = format_time(sun_props.time,
233 addon_prefs.show_daylight_savings and sun_props.use_daylight_savings,
234 sun_props.longitude)
235 utc_time = format_time(sun_props.time,
236 addon_prefs.show_daylight_savings and sun_props.use_daylight_savings,
237 sun_props.longitude,
238 sun_props.UTC_zone)
240 col = layout.column(align=True)
241 col.alignment = 'CENTER'
243 split = col.split(factor=0.5, align=True)
244 sub = split.column(align=True)
245 sub.alignment = 'RIGHT'
246 sub.label(text="Time Local:")
247 sub.label(text="UTC:")
249 sub = split.column(align=True)
250 sub.label(text=local_time)
251 sub.label(text=utc_time)
252 col.separator()
254 if addon_prefs.show_rise_set:
255 sunrise = format_hms(sun.sunrise)
256 sunset = format_hms(sun.sunset)
258 col = layout.column(align=True)
259 col.alignment = 'CENTER'
261 split = col.split(factor=0.5, align=True)
262 sub = split.column(align=True)
263 sub.alignment = 'RIGHT'
264 sub.label(text="Sunrise:")
265 sub.label(text="Sunset:")
267 sub = split.column(align=True)
268 sub.label(text=sunrise)
269 sub.label(text=sunset)
271 col.separator()