Import_3ds: Improved distance cue node setup
[blender-addons.git] / sun_position / ui_sun.py
blob3fa563f393e2a7d5f48efe00396bbc6a0410ae3a
1 # SPDX-FileCopyrightText: 2019-2023 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 import bpy
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
32 preset_defines = [
33 "sun_props = bpy.context.scene.sun_pos_properties"
36 # properties to store in the preset
37 preset_values = [
38 "sun_props.day",
39 "sun_props.month",
40 "sun_props.time",
41 "sun_props.year",
42 "sun_props.UTC_zone",
43 "sun_props.use_daylight_savings",
44 "sun_props.latitude",
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"
61 bl_context = "world"
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
70 layout = self.layout
71 layout.use_property_split = True
72 layout.use_property_decorate = False
74 layout.prop(sun_props, "usage_mode", expand=True)
75 layout.separator()
77 if sun_props.usage_mode == "HDR":
78 self.draw_environment_mode_panel(context)
79 else:
80 self.draw_normal_mode_panel(context)
82 def draw_environment_mode_panel(self, context):
83 sun_props = context.scene.sun_pos_properties
84 layout = self.layout
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")
93 else:
94 col.label(text="Please activate Use Nodes in the World panel.",
95 icon="ERROR")
96 else:
97 col.label(text="Please select World in the World panel.",
98 icon="ERROR")
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")
109 col.separator()
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
118 layout = self.layout
120 col = layout.column(align=True)
121 col.prop(sun_props, "sun_object")
122 col.separator()
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")
129 col.separator()
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")
135 else:
136 col.label(text="Please activate Use Nodes in the World panel.",
137 icon="ERROR")
138 else:
139 col.label(text="Please select World in the World panel.",
140 icon="ERROR")
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"
156 bl_context = "world"
157 bl_label = "Location"
158 bl_parent_id = "SUNPOS_PT_Panel"
160 @classmethod
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):
166 layout = self.layout
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")
177 col.separator()
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")
186 col.separator()
188 col = layout.column()
189 col.prop(sun_props, "sun_distance")
190 col.separator()
193 class SUNPOS_PT_Time(bpy.types.Panel):
194 bl_space_type = "PROPERTIES"
195 bl_region_type = "WINDOW"
196 bl_context = "world"
197 bl_label = "Time"
198 bl_parent_id = "SUNPOS_PT_Panel"
200 @classmethod
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):
206 layout = self.layout
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")
216 else:
217 col.prop(sun_props, "day")
218 col.prop(sun_props, "month")
219 col.prop(sun_props, "year")
220 col.separator()
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")
226 col.separator()
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,
232 sun_props.UTC_zone)
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)
246 col.separator()
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)
265 col.separator()