Merge branch 'blender-v4.0-release'
[blender-addons.git] / sun_position / properties.py
blob2b262ddc1966bc1f8510da7e1bc97e8ada6527cc
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
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 set_coordinates(self, value):
40 parsed_co = parse_position(value)
42 global parse_success
43 if parsed_co is not None and len(parsed_co) == 2:
44 latitude, longitude = parsed_co
45 self.latitude, self.longitude = latitude, longitude
46 else:
47 parse_success = False
49 sun_update(self, bpy.context)
52 def sun_update(self, context):
53 sun_props = context.scene.sun_pos_properties
55 update_time(context)
56 move_sun(context)
58 if sun_props.show_surface:
59 surface_update(self, context)
60 if sun_props.show_analemmas:
61 analemmas_update(self, context)
62 if sun_props.show_north:
63 north_update(self, context)
66 class SunPosProperties(PropertyGroup):
67 usage_mode: EnumProperty(
68 name="Usage Mode",
69 description="Operate in normal mode or environment texture mode",
70 items=(
71 ('NORMAL', "Normal", ""),
72 ('HDR', "Sun + HDR texture", ""),
74 default='NORMAL',
75 update=sun_update)
77 use_daylight_savings: BoolProperty(
78 name="Daylight Savings",
79 description="Daylight savings time adds 1 hour to standard time",
80 default=False,
81 update=sun_update)
83 use_refraction: BoolProperty(
84 name="Use Refraction",
85 description="Show the apparent Sun position due to atmospheric refraction",
86 default=True,
87 update=sun_update)
89 show_north: BoolProperty(
90 name="Show North",
91 description="Draw a line pointing to the north",
92 default=False,
93 update=north_update)
95 north_offset: FloatProperty(
96 name="North Offset",
97 description="Rotate the scene to choose the North direction",
98 unit="ROTATION",
99 soft_min=-pi, soft_max=pi, step=10.0, default=0.0,
100 update=sun_update)
102 show_surface: BoolProperty(
103 name="Show Surface",
104 description="Draw the surface that the Sun occupies in the sky",
105 default=False,
106 update=surface_update)
108 show_analemmas: BoolProperty(
109 name="Show Analemmas",
110 description="Draw Sun analemmas. These help visualize the motion of the Sun in the sky during the year, for each hour of the day",
111 default=False,
112 update=analemmas_update)
114 coordinates: StringProperty(
115 name="Coordinates",
116 description="Enter coordinates from an online map",
117 get=get_coordinates,
118 set=set_coordinates,
119 options={'SKIP_SAVE'})
121 latitude: FloatProperty(
122 name="Latitude",
123 description="Latitude: (+) Northern (-) Southern",
124 soft_min=-90.0, soft_max=90.0,
125 step=5, precision=3,
126 default=0.0,
127 update=lat_long_update)
129 longitude: FloatProperty(
130 name="Longitude",
131 description="Longitude: (-) West of Greenwich (+) East of Greenwich",
132 soft_min=-180.0, soft_max=180.0,
133 step=5, precision=3,
134 default=0.0,
135 update=lat_long_update)
137 sunrise_time: FloatProperty(
138 name="Sunrise Time",
139 description="Time at which the Sun rises",
140 soft_min=0.0, soft_max=24.0,
141 default=0.0,
142 get=lambda _: sun.sunrise)
144 sunset_time: FloatProperty(
145 name="Sunset Time",
146 description="Time at which the Sun sets",
147 soft_min=0.0, soft_max=24.0,
148 default=0.0,
149 get=lambda _: sun.sunset)
151 sun_elevation: FloatProperty(
152 name="Sun Elevation",
153 description="Elevation angle of the Sun",
154 soft_min=-pi/2, soft_max=pi/2,
155 precision=3,
156 default=0.0,
157 unit="ROTATION",
158 get=lambda _: sun.elevation)
160 sun_azimuth: FloatProperty(
161 name="Sun Azimuth",
162 description="Rotation angle of the Sun from the direction of the north",
163 soft_min=-pi, soft_max=pi,
164 precision=3,
165 default=0.0,
166 unit="ROTATION",
167 get=lambda _: sun.azimuth - bpy.context.scene.sun_pos_properties.north_offset)
169 month: IntProperty(
170 name="Month",
171 min=1, max=12, default=TODAY.month,
172 update=sun_update)
174 day: IntProperty(
175 name="Day",
176 min=1, max=31, default=TODAY.day,
177 update=sun_update)
179 year: IntProperty(
180 name="Year",
181 min=1, max=4000, default=TODAY.year,
182 update=sun_update)
184 use_day_of_year: BoolProperty(
185 description="Use a single value for the day of year",
186 name="Use day of year",
187 default=False,
188 update=sun_update)
190 day_of_year: IntProperty(
191 name="Day of Year",
192 min=1, max=366, default=1,
193 update=sun_update)
195 UTC_zone: FloatProperty(
196 name="UTC Zone",
197 description="Difference from Greenwich, England, in hours",
198 precision=1,
199 min=-14.0, max=13, step=50, default=0.0,
200 update=sun_update)
202 time: FloatProperty(
203 name="Time",
204 description="Time of the day",
205 precision=4,
206 soft_min=0.0, soft_max=23.9999, step=1.0, default=12.0,
207 update=sun_update)
209 sun_distance: FloatProperty(
210 name="Distance",
211 description="Distance to the Sun from the origin",
212 unit="LENGTH",
213 min=0.0, soft_max=3000.0, step=10.0, default=50.0,
214 update=sun_update)
216 sun_object: PointerProperty(
217 name="Sun Object",
218 type=bpy.types.Object,
219 description="Sun object to use in the scene",
220 poll=lambda self, obj: obj.type == 'LIGHT',
221 update=sun_update)
223 object_collection: PointerProperty(
224 name="Collection",
225 type=bpy.types.Collection,
226 description="Collection of objects used to visualize the motion of the Sun",
227 update=sun_update)
229 object_collection_type: EnumProperty(
230 name="Display type",
231 description="Type of Sun motion to visualize.",
232 items=(
233 ('ANALEMMA', "Analemma", "Trajectory of the Sun in the sky during the year, for a given time of the day"),
234 ('DIURNAL', "Diurnal", "Trajectory of the Sun in the sky during a single day"),
236 default='ANALEMMA',
237 update=sun_update)
239 sky_texture: StringProperty(
240 name="Sky Texture",
241 default="",
242 description="Name of the sky texture to use",
243 update=sun_update)
245 hdr_texture: StringProperty(
246 default="Environment Texture",
247 name="Environment Texture",
248 description="Name of the environment texture to use. World nodes must be enabled "
249 "and the color set to an environment Texture",
250 update=sun_update)
252 hdr_azimuth: FloatProperty(
253 name="Rotation",
254 description="Rotation angle of the Sun and environment texture",
255 unit="ROTATION",
256 step=10.0,
257 default=0.0, precision=3,
258 update=sun_update)
260 hdr_elevation: FloatProperty(
261 name="Elevation",
262 description="Elevation angle of the Sun",
263 unit="ROTATION",
264 step=10.0,
265 default=0.0, precision=3,
266 update=sun_update)
268 bind_to_sun: BoolProperty(
269 name="Bind Texture to Sun",
270 description="If enabled, the environment texture moves with the Sun",
271 default=False,
272 update=sun_update)
274 time_spread: FloatProperty(
275 name="Time Spread",
276 description="Time period around which to spread object collection",
277 precision=4,
278 soft_min=1.0, soft_max=24.0, step=1.0, default=23.0,
279 update=sun_update)
281 ############################################################################
282 # Preference panel properties
283 ############################################################################
286 class SunPosAddonPreferences(AddonPreferences):
287 bl_idname = __package__
289 show_overlays: BoolProperty(
290 name="Show Overlays",
291 description="Display overlays in the viewport: the direction of the north, analemmas and the Sun surface",
292 default=True,
293 update=sun_update)
295 show_refraction: BoolProperty(
296 name="Refraction",
297 description="Show Sun Refraction choice",
298 default=True)
300 show_az_el: BoolProperty(
301 name="Azimuth and Elevation Info",
302 description="Show azimuth and solar elevation info",
303 default=True)
305 show_rise_set: BoolProperty(
306 name="Sunrise and Sunset Info",
307 description="Show sunrise and sunset labels",
308 default=True)
310 def draw(self, context):
311 layout = self.layout
313 box = layout.box()
314 col = box.column()
316 col.label(text="Show options and info:")
317 flow = col.grid_flow(columns=0, even_columns=True, even_rows=False, align=False)
318 flow.prop(self, "show_refraction")
319 flow.prop(self, "show_overlays")
320 flow.prop(self, "show_az_el")
321 flow.prop(self, "show_rise_set")