Sun Position: do not update the sun position when prefs are changed
[blender-addons.git] / sun_position / properties.py
blobbd6661b8b3a30f14adaf87132646db9dbc788621
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 import bpy
4 from bpy.types import AddonPreferences, PropertyGroup
5 from bpy.props import (StringProperty, EnumProperty, IntProperty,
6 FloatProperty, BoolProperty, PointerProperty)
7 from bpy.app.translations import pgettext_iface as iface_
10 from .sun_calc import format_lat_long, parse_position, sun, update_time, move_sun
11 from .draw import north_update, surface_update, analemmas_update
13 from math import pi
14 from datetime import datetime
15 TODAY = datetime.today()
17 ############################################################################
18 # Sun panel properties
19 ############################################################################
21 parse_success = True
24 def lat_long_update(self, context):
25 global parse_success
26 parse_success = True
27 sun_update(self, context)
30 def get_coordinates(self):
31 if parse_success:
32 return format_lat_long(self.latitude, self.longitude)
33 return iface_("ERROR: Could not parse coordinates")
36 def set_coordinates(self, value):
37 parsed_co = parse_position(value)
39 global parse_success
40 if parsed_co is not None and len(parsed_co) == 2:
41 latitude, longitude = parsed_co
42 self.latitude, self.longitude = latitude, longitude
43 else:
44 parse_success = False
46 sun_update(self, bpy.context)
49 def sun_update(self, context):
50 sun_props = context.scene.sun_pos_properties
52 update_time(context)
53 move_sun(context)
55 if sun_props.show_surface:
56 surface_update(self, context)
57 if sun_props.show_analemmas:
58 analemmas_update(self, context)
59 if sun_props.show_north:
60 north_update(self, context)
63 class SunPosProperties(PropertyGroup):
64 usage_mode: EnumProperty(
65 name="Usage Mode",
66 description="Operate in normal mode or environment texture mode",
67 items=(
68 ('NORMAL', "Normal", ""),
69 ('HDR', "Sun + HDR texture", ""),
71 default='NORMAL',
72 update=sun_update)
74 use_daylight_savings: BoolProperty(
75 name="Daylight Savings",
76 description="Daylight savings time adds 1 hour to standard time",
77 default=False,
78 update=sun_update)
80 use_refraction: BoolProperty(
81 name="Use Refraction",
82 description="Show the apparent Sun position due to atmospheric refraction",
83 default=True,
84 update=sun_update)
86 show_north: BoolProperty(
87 name="Show North",
88 description="Draw a line pointing to the north",
89 default=False,
90 update=north_update)
92 north_offset: FloatProperty(
93 name="North Offset",
94 description="Rotate the scene to choose the North direction",
95 unit="ROTATION",
96 soft_min=-pi, soft_max=pi, step=10.0, default=0.0,
97 update=sun_update)
99 show_surface: BoolProperty(
100 name="Show Surface",
101 description="Draw the surface that the Sun occupies in the sky",
102 default=False,
103 update=surface_update)
105 show_analemmas: BoolProperty(
106 name="Show Analemmas",
107 description="Draw Sun analemmas. These help visualize the motion of the Sun in the sky during the year, for each hour of the day",
108 default=False,
109 update=analemmas_update)
111 coordinates: StringProperty(
112 name="Coordinates",
113 description="Enter coordinates from an online map",
114 get=get_coordinates,
115 set=set_coordinates,
116 options={'SKIP_SAVE'})
118 latitude: FloatProperty(
119 name="Latitude",
120 description="Latitude: (+) Northern (-) Southern",
121 soft_min=-90.0, soft_max=90.0,
122 step=5, precision=3,
123 default=0.0,
124 update=lat_long_update)
126 longitude: FloatProperty(
127 name="Longitude",
128 description="Longitude: (-) West of Greenwich (+) East of Greenwich",
129 soft_min=-180.0, soft_max=180.0,
130 step=5, precision=3,
131 default=0.0,
132 update=lat_long_update)
134 sunrise_time: FloatProperty(
135 name="Sunrise Time",
136 description="Time at which the Sun rises",
137 soft_min=0.0, soft_max=24.0,
138 default=0.0,
139 get=lambda _: sun.sunrise)
141 sunset_time: FloatProperty(
142 name="Sunset Time",
143 description="Time at which the Sun sets",
144 soft_min=0.0, soft_max=24.0,
145 default=0.0,
146 get=lambda _: sun.sunset)
148 sun_azimuth: FloatProperty(
149 name="Sun Azimuth",
150 description="Rotation angle of the Sun from the direction of the north",
151 soft_min=-pi, soft_max=pi,
152 default=0.0,
153 get=lambda _: sun.azimuth)
155 sun_elevation: FloatProperty(
156 name="Sun Elevation",
157 description="Elevation angle of the Sun",
158 soft_min=-pi/2, soft_max=pi/2,
159 default=0.0,
160 get=lambda _: sun.elevation)
162 month: IntProperty(
163 name="Month",
164 min=1, max=12, default=TODAY.month,
165 update=sun_update)
167 day: IntProperty(
168 name="Day",
169 min=1, max=31, default=TODAY.day,
170 update=sun_update)
172 year: IntProperty(
173 name="Year",
174 min=1, max=4000, default=TODAY.year,
175 update=sun_update)
177 use_day_of_year: BoolProperty(
178 description="Use a single value for the day of year",
179 name="Use day of year",
180 default=False,
181 update=sun_update)
183 day_of_year: IntProperty(
184 name="Day of Year",
185 min=1, max=366, default=1,
186 update=sun_update)
188 UTC_zone: FloatProperty(
189 name="UTC Zone",
190 description="Difference from Greenwich, England, in hours",
191 precision=1,
192 min=-14.0, max=13, step=50, default=0.0,
193 update=sun_update)
195 time: FloatProperty(
196 name="Time",
197 description="Time of the day",
198 precision=4,
199 soft_min=0.0, soft_max=23.9999, step=1.0, default=12.0,
200 update=sun_update)
202 sun_distance: FloatProperty(
203 name="Distance",
204 description="Distance to the Sun from the origin",
205 unit="LENGTH",
206 min=0.0, soft_max=3000.0, step=10.0, default=50.0,
207 update=sun_update)
209 sun_object: PointerProperty(
210 name="Sun Object",
211 type=bpy.types.Object,
212 description="Sun object to use in the scene",
213 poll=lambda self, obj: obj.type == 'LIGHT',
214 update=sun_update)
216 object_collection: PointerProperty(
217 name="Collection",
218 type=bpy.types.Collection,
219 description="Collection of objects used to visualize the motion of the Sun",
220 update=sun_update)
222 object_collection_type: EnumProperty(
223 name="Display type",
224 description="Type of Sun motion to visualize.",
225 items=(
226 ('ANALEMMA', "Analemma", "Trajectory of the Sun in the sky during the year, for a given time of the day"),
227 ('DIURNAL', "Diurnal", "Trajectory of the Sun in the sky during a single day"),
229 default='ANALEMMA',
230 update=sun_update)
232 sky_texture: StringProperty(
233 name="Sky Texture",
234 default="",
235 description="Name of the sky texture to use",
236 update=sun_update)
238 hdr_texture: StringProperty(
239 default="Environment Texture",
240 name="Environment Texture",
241 description="Name of the environment texture to use. World nodes must be enabled "
242 "and the color set to an environment Texture",
243 update=sun_update)
245 hdr_azimuth: FloatProperty(
246 name="Rotation",
247 description="Rotation angle of the Sun and environment texture",
248 unit="ROTATION",
249 step=10.0,
250 default=0.0, precision=3,
251 update=sun_update)
253 hdr_elevation: FloatProperty(
254 name="Elevation",
255 description="Elevation angle of the Sun",
256 unit="ROTATION",
257 step=10.0,
258 default=0.0, precision=3,
259 update=sun_update)
261 bind_to_sun: BoolProperty(
262 name="Bind Texture to Sun",
263 description="If enabled, the environment texture moves with the Sun",
264 default=False,
265 update=sun_update)
267 time_spread: FloatProperty(
268 name="Time Spread",
269 description="Time period around which to spread object collection",
270 precision=4,
271 soft_min=1.0, soft_max=24.0, step=1.0, default=23.0,
272 update=sun_update)
274 ############################################################################
275 # Preference panel properties
276 ############################################################################
279 class SunPosAddonPreferences(AddonPreferences):
280 bl_idname = __package__
282 show_time_place: BoolProperty(
283 name="Time and Place Presets",
284 description="Show time and place presets",
285 default=False)
287 show_overlays: BoolProperty(
288 name="Show Overlays",
289 description="Display overlays in the viewport: the direction of the north, analemmas and the Sun surface",
290 default=True,
291 update=sun_update)
293 show_refraction: BoolProperty(
294 name="Refraction",
295 description="Show Sun Refraction choice",
296 default=True)
298 show_az_el: BoolProperty(
299 name="Azimuth and Elevation Info",
300 description="Show azimuth and solar elevation info",
301 default=True)
303 show_daylight_savings: BoolProperty(
304 name="Daylight Savings",
305 description="Show daylight savings time choice",
306 default=True)
308 show_rise_set: BoolProperty(
309 name="Sunrise and Sunset Info",
310 description="Show sunrise and sunset labels",
311 default=True)
313 def draw(self, context):
314 layout = self.layout
316 box = layout.box()
317 col = box.column()
319 col.label(text="Show options or labels:")
320 flow = col.grid_flow(columns=0, even_columns=True, even_rows=False, align=False)
321 flow.prop(self, "show_time_place")
322 flow.prop(self, "show_refraction")
323 flow.prop(self, "show_overlays")
324 flow.prop(self, "show_az_el")
325 flow.prop(self, "show_daylight_savings")
326 flow.prop(self, "show_rise_set")