Merge branch 'blender-v4.0-release'
[blender-addons.git] / sun_position / properties.py
blob846c1416fc4e5ade20b767e5d01a91d7e4a65b04
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 default="00°00′00.00″ 00°00′00.00″",
120 options={'SKIP_SAVE'})
122 latitude: FloatProperty(
123 name="Latitude",
124 description="Latitude: (+) Northern (-) Southern",
125 soft_min=-90.0, soft_max=90.0,
126 step=5, precision=3,
127 default=0.0,
128 update=lat_long_update)
130 longitude: FloatProperty(
131 name="Longitude",
132 description="Longitude: (-) West of Greenwich (+) East of Greenwich",
133 soft_min=-180.0, soft_max=180.0,
134 step=5, precision=3,
135 default=0.0,
136 update=lat_long_update)
138 sunrise_time: FloatProperty(
139 name="Sunrise Time",
140 description="Time at which the Sun rises",
141 soft_min=0.0, soft_max=24.0,
142 default=0.0,
143 get=lambda _: sun.sunrise)
145 sunset_time: FloatProperty(
146 name="Sunset Time",
147 description="Time at which the Sun sets",
148 soft_min=0.0, soft_max=24.0,
149 default=0.0,
150 get=lambda _: sun.sunset)
152 sun_elevation: FloatProperty(
153 name="Sun Elevation",
154 description="Elevation angle of the Sun",
155 soft_min=-pi/2, soft_max=pi/2,
156 precision=3,
157 default=0.0,
158 unit="ROTATION",
159 get=lambda _: sun.elevation)
161 sun_azimuth: FloatProperty(
162 name="Sun Azimuth",
163 description="Rotation angle of the Sun from the direction of the north",
164 soft_min=-pi, soft_max=pi,
165 precision=3,
166 default=0.0,
167 unit="ROTATION",
168 get=lambda _: sun.azimuth - bpy.context.scene.sun_pos_properties.north_offset)
170 month: IntProperty(
171 name="Month",
172 min=1, max=12, default=TODAY.month,
173 update=sun_update)
175 day: IntProperty(
176 name="Day",
177 min=1, max=31, default=TODAY.day,
178 update=sun_update)
180 year: IntProperty(
181 name="Year",
182 min=1, max=4000, default=TODAY.year,
183 update=sun_update)
185 use_day_of_year: BoolProperty(
186 description="Use a single value for the day of year",
187 name="Use day of year",
188 default=False,
189 update=sun_update)
191 day_of_year: IntProperty(
192 name="Day of Year",
193 min=1, max=366, default=1,
194 update=sun_update)
196 UTC_zone: FloatProperty(
197 name="UTC Zone",
198 description="Difference from Greenwich, England, in hours",
199 precision=1,
200 min=-14.0, max=13, step=50, default=0.0,
201 update=sun_update)
203 time: FloatProperty(
204 name="Time",
205 description="Time of the day",
206 precision=4,
207 soft_min=0.0, soft_max=23.9999, step=1.0, default=12.0,
208 update=sun_update)
210 sun_distance: FloatProperty(
211 name="Distance",
212 description="Distance to the Sun from the origin",
213 unit="LENGTH",
214 min=0.0, soft_max=3000.0, step=10.0, default=50.0,
215 update=sun_update)
217 sun_object: PointerProperty(
218 name="Sun Object",
219 type=bpy.types.Object,
220 description="Sun object to use in the scene",
221 poll=lambda self, obj: obj.type == 'LIGHT',
222 update=sun_update)
224 object_collection: PointerProperty(
225 name="Collection",
226 type=bpy.types.Collection,
227 description="Collection of objects used to visualize the motion of the Sun",
228 update=sun_update)
230 object_collection_type: EnumProperty(
231 name="Display type",
232 description="Type of Sun motion to visualize.",
233 items=(
234 ('ANALEMMA', "Analemma", "Trajectory of the Sun in the sky during the year, for a given time of the day"),
235 ('DIURNAL', "Diurnal", "Trajectory of the Sun in the sky during a single day"),
237 default='ANALEMMA',
238 update=sun_update)
240 sky_texture: StringProperty(
241 name="Sky Texture",
242 default="",
243 description="Name of the sky texture to use",
244 update=sun_update)
246 hdr_texture: StringProperty(
247 default="Environment Texture",
248 name="Environment Texture",
249 description="Name of the environment texture to use. World nodes must be enabled "
250 "and the color set to an environment Texture",
251 update=sun_update)
253 hdr_azimuth: FloatProperty(
254 name="Rotation",
255 description="Rotation angle of the Sun and environment texture",
256 unit="ROTATION",
257 step=10.0,
258 default=0.0, precision=3,
259 update=sun_update)
261 hdr_elevation: FloatProperty(
262 name="Elevation",
263 description="Elevation angle of the Sun",
264 unit="ROTATION",
265 step=10.0,
266 default=0.0, precision=3,
267 update=sun_update)
269 bind_to_sun: BoolProperty(
270 name="Bind Texture to Sun",
271 description="If enabled, the environment texture moves with the Sun",
272 default=False,
273 update=sun_update)
275 time_spread: FloatProperty(
276 name="Time Spread",
277 description="Time period around which to spread object collection",
278 precision=4,
279 soft_min=1.0, soft_max=24.0, step=1.0, default=23.0,
280 update=sun_update)
282 ############################################################################
283 # Preference panel properties
284 ############################################################################
287 class SunPosAddonPreferences(AddonPreferences):
288 bl_idname = __package__
290 show_overlays: BoolProperty(
291 name="Show Overlays",
292 description="Display overlays in the viewport: the direction of the north, analemmas and the Sun surface",
293 default=True,
294 update=sun_update)
296 show_refraction: BoolProperty(
297 name="Refraction",
298 description="Show Sun Refraction choice",
299 default=True)
301 show_az_el: BoolProperty(
302 name="Azimuth and Elevation Info",
303 description="Show azimuth and solar elevation info",
304 default=True)
306 show_rise_set: BoolProperty(
307 name="Sunrise and Sunset Info",
308 description="Show sunrise and sunset labels",
309 default=True)
311 def draw(self, context):
312 layout = self.layout
314 box = layout.box()
315 col = box.column()
317 col.label(text="Show options and info:")
318 flow = col.grid_flow(columns=0, even_columns=True, even_rows=False, align=False)
319 flow.prop(self, "show_refraction")
320 flow.prop(self, "show_overlays")
321 flow.prop(self, "show_az_el")
322 flow.prop(self, "show_rise_set")