Import_3ds: Improved distance cue node setup
[blender-addons.git] / sun_position / properties.py
bloba9e66ca7610ae7cbb9b081690b811a1f3cdfaa4e
1 # SPDX-FileCopyrightText: 2019-2023 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 import bpy
6 from bpy.types import AddonPreferences, PropertyGroup
7 from bpy.props import (StringProperty, EnumProperty, IntProperty,
8 FloatProperty, BoolProperty, PointerProperty)
9 from bpy.app.translations import pgettext_iface as iface_
12 from .draw import north_update, surface_update, analemmas_update
13 from .geo import parse_position
14 from .sun_calc import format_lat_long, sun, update_time, move_sun
16 from math import pi
17 from datetime import datetime, date
18 TODAY = datetime.today()
20 ############################################################################
21 # Sun panel properties
22 ############################################################################
24 parse_success = True
27 def lat_long_update(self, context):
28 global parse_success
29 parse_success = True
30 sun_update(self, context)
33 def get_coordinates(self):
34 if parse_success:
35 return format_lat_long(self.latitude, self.longitude)
36 return iface_("ERROR: Could not parse coordinates")
39 def get_day(self):
40 """Getter for the day property.
42 Not all days are valid, depending on month.
43 Invalid days are only at the end of the month so they
44 can be checked by just decreasing the value until it is valid.
45 """
46 day = self.get("day", 1)
47 found_date = False
48 while not found_date:
49 try:
50 date(self.year, self.month, day)
51 found_date = True
52 except ValueError:
53 day -= 1
54 return day
57 def set_day(self, value):
58 self["day"] = value
61 def set_coordinates(self, value):
62 parsed_co = parse_position(value)
64 global parse_success
65 if parsed_co is not None and len(parsed_co) == 2:
66 latitude, longitude = parsed_co
67 self.latitude, self.longitude = latitude, longitude
68 else:
69 parse_success = False
71 sun_update(self, bpy.context)
74 def sun_update(self, context):
75 sun_props = context.scene.sun_pos_properties
77 update_time(context)
78 move_sun(context)
80 if sun_props.show_surface:
81 surface_update(self, context)
82 if sun_props.show_analemmas:
83 analemmas_update(self, context)
84 if sun_props.show_north:
85 north_update(self, context)
88 class SunPosProperties(PropertyGroup):
89 usage_mode: EnumProperty(
90 name="Usage Mode",
91 description="Operate in normal mode or environment texture mode",
92 items=(
93 ('NORMAL', "Normal", ""),
94 ('HDR', "Sun + HDR texture", ""),
96 default='NORMAL',
97 update=sun_update)
99 use_daylight_savings: BoolProperty(
100 name="Daylight Savings",
101 description="Daylight savings time adds 1 hour to standard time",
102 default=False,
103 update=sun_update)
105 use_refraction: BoolProperty(
106 name="Use Refraction",
107 description="Show the apparent Sun position due to atmospheric refraction",
108 default=True,
109 update=sun_update)
111 show_north: BoolProperty(
112 name="Show North",
113 description="Draw a line pointing to the north",
114 default=False,
115 update=north_update)
117 north_offset: FloatProperty(
118 name="North Offset",
119 description="Rotate the scene to choose the North direction",
120 unit="ROTATION",
121 soft_min=-pi, soft_max=pi, step=10.0, default=0.0,
122 update=sun_update)
124 show_surface: BoolProperty(
125 name="Show Surface",
126 description="Draw the surface that the Sun occupies in the sky",
127 default=False,
128 update=surface_update)
130 show_analemmas: BoolProperty(
131 name="Show Analemmas",
132 description="Draw Sun analemmas. These help visualize the motion of the Sun in the sky during the year, for each hour of the day",
133 default=False,
134 update=analemmas_update)
136 coordinates: StringProperty(
137 name="Coordinates",
138 description="Enter coordinates from an online map",
139 get=get_coordinates,
140 set=set_coordinates,
141 default="00°00′00.00″ 00°00′00.00″",
142 options={'SKIP_SAVE'})
144 latitude: FloatProperty(
145 name="Latitude",
146 description="Latitude: (+) Northern (-) Southern",
147 soft_min=-90.0, soft_max=90.0,
148 step=5, precision=3,
149 default=0.0,
150 update=lat_long_update)
152 longitude: FloatProperty(
153 name="Longitude",
154 description="Longitude: (-) West of Greenwich (+) East of Greenwich",
155 soft_min=-180.0, soft_max=180.0,
156 step=5, precision=3,
157 default=0.0,
158 update=lat_long_update)
160 sunrise_time: FloatProperty(
161 name="Sunrise Time",
162 description="Time at which the Sun rises",
163 soft_min=0.0, soft_max=24.0,
164 default=0.0,
165 get=lambda _: sun.sunrise)
167 sunset_time: FloatProperty(
168 name="Sunset Time",
169 description="Time at which the Sun sets",
170 soft_min=0.0, soft_max=24.0,
171 default=0.0,
172 get=lambda _: sun.sunset)
174 sun_elevation: FloatProperty(
175 name="Sun Elevation",
176 description="Elevation angle of the Sun",
177 soft_min=-pi/2, soft_max=pi/2,
178 precision=3,
179 default=0.0,
180 unit="ROTATION",
181 get=lambda _: sun.elevation)
183 sun_azimuth: FloatProperty(
184 name="Sun Azimuth",
185 description="Rotation angle of the Sun from the direction of the north",
186 soft_min=-pi, soft_max=pi,
187 precision=3,
188 default=0.0,
189 unit="ROTATION",
190 get=lambda _: sun.azimuth - bpy.context.scene.sun_pos_properties.north_offset)
192 month: IntProperty(
193 name="Month",
194 min=1, max=12, default=TODAY.month,
195 update=sun_update)
197 day: IntProperty(
198 name="Day",
199 min=1, max=31, default=TODAY.day,
200 get=get_day,
201 set=set_day,
202 update=sun_update)
204 year: IntProperty(
205 name="Year",
206 min=1, max=4000, default=TODAY.year,
207 update=sun_update)
209 use_day_of_year: BoolProperty(
210 description="Use a single value for the day of year",
211 name="Use day of year",
212 default=False,
213 update=sun_update)
215 day_of_year: IntProperty(
216 name="Day of Year",
217 min=1, max=366, default=1,
218 update=sun_update)
220 UTC_zone: FloatProperty(
221 name="UTC Zone",
222 description="Difference from Greenwich, England, in hours",
223 precision=1,
224 min=-14.0, max=13, step=50, default=0.0,
225 update=sun_update)
227 time: FloatProperty(
228 name="Time",
229 description="Time of the day",
230 precision=4,
231 soft_min=0.0, soft_max=23.9999, step=1.0, default=12.0,
232 update=sun_update)
234 sun_distance: FloatProperty(
235 name="Distance",
236 description="Distance to the Sun from the origin",
237 unit="LENGTH",
238 min=0.0, soft_max=3000.0, step=10.0, default=50.0,
239 update=sun_update)
241 sun_object: PointerProperty(
242 name="Sun Object",
243 type=bpy.types.Object,
244 description="Sun object to use in the scene",
245 poll=lambda self, obj: obj.type == 'LIGHT',
246 update=sun_update)
248 object_collection: PointerProperty(
249 name="Collection",
250 type=bpy.types.Collection,
251 description="Collection of objects used to visualize the motion of the Sun",
252 update=sun_update)
254 object_collection_type: EnumProperty(
255 name="Display type",
256 description="Type of Sun motion to visualize",
257 items=(
258 ('ANALEMMA', "Analemma", "Trajectory of the Sun in the sky during the year, for a given time of the day"),
259 ('DIURNAL', "Diurnal", "Trajectory of the Sun in the sky during a single day"),
261 default='ANALEMMA',
262 update=sun_update)
264 sky_texture: StringProperty(
265 name="Sky Texture",
266 default="",
267 description="Name of the sky texture to use",
268 update=sun_update)
270 hdr_texture: StringProperty(
271 default="Environment Texture",
272 name="Environment Texture",
273 description="Name of the environment texture to use. World nodes must be enabled "
274 "and the color set to an environment Texture",
275 update=sun_update)
277 hdr_azimuth: FloatProperty(
278 name="Rotation",
279 description="Rotation angle of the Sun and environment texture",
280 unit="ROTATION",
281 step=10.0,
282 default=0.0, precision=3,
283 update=sun_update)
285 hdr_elevation: FloatProperty(
286 name="Elevation",
287 description="Elevation angle of the Sun",
288 unit="ROTATION",
289 step=10.0,
290 default=0.0, precision=3,
291 update=sun_update)
293 bind_to_sun: BoolProperty(
294 name="Bind Texture to Sun",
295 description="If enabled, the environment texture moves with the Sun",
296 default=False,
297 update=sun_update)
299 time_spread: FloatProperty(
300 name="Time Spread",
301 description="Time period around which to spread object collection",
302 precision=4,
303 soft_min=1.0, soft_max=24.0, step=1.0, default=23.0,
304 update=sun_update)
306 ############################################################################
307 # Preference panel properties
308 ############################################################################
311 class SunPosAddonPreferences(AddonPreferences):
312 bl_idname = __package__
314 show_overlays: BoolProperty(
315 name="Show Overlays",
316 description="Display overlays in the viewport: the direction of the north, analemmas and the Sun surface",
317 default=True,
318 update=sun_update)
320 show_refraction: BoolProperty(
321 name="Refraction",
322 description="Show Sun Refraction choice",
323 default=True)
325 show_az_el: BoolProperty(
326 name="Azimuth and Elevation Info",
327 description="Show azimuth and solar elevation info",
328 default=True)
330 show_rise_set: BoolProperty(
331 name="Sunrise and Sunset Info",
332 description="Show sunrise and sunset labels",
333 default=True)
335 def draw(self, context):
336 layout = self.layout
338 box = layout.box()
339 col = box.column()
341 col.label(text="Show options and info:")
342 flow = col.grid_flow(columns=0, even_columns=True, even_rows=False, align=False)
343 flow.prop(self, "show_refraction")
344 flow.prop(self, "show_overlays")
345 flow.prop(self, "show_az_el")
346 flow.prop(self, "show_rise_set")