1 # SPDX-FileCopyrightText: 2019-2023 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
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
17 from datetime
import datetime
18 TODAY
= datetime
.today()
20 ############################################################################
21 # Sun panel properties
22 ############################################################################
27 def lat_long_update(self
, context
):
30 sun_update(self
, context
)
33 def get_coordinates(self
):
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
)
43 if parsed_co
is not None and len(parsed_co
) == 2:
44 latitude
, longitude
= parsed_co
45 self
.latitude
, self
.longitude
= latitude
, longitude
49 sun_update(self
, bpy
.context
)
52 def sun_update(self
, context
):
53 sun_props
= context
.scene
.sun_pos_properties
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(
69 description
="Operate in normal mode or environment texture mode",
71 ('NORMAL', "Normal", ""),
72 ('HDR', "Sun + HDR texture", ""),
77 use_daylight_savings
: BoolProperty(
78 name
="Daylight Savings",
79 description
="Daylight savings time adds 1 hour to standard time",
83 use_refraction
: BoolProperty(
84 name
="Use Refraction",
85 description
="Show the apparent Sun position due to atmospheric refraction",
89 show_north
: BoolProperty(
91 description
="Draw a line pointing to the north",
95 north_offset
: FloatProperty(
97 description
="Rotate the scene to choose the North direction",
99 soft_min
=-pi
, soft_max
=pi
, step
=10.0, default
=0.0,
102 show_surface
: BoolProperty(
104 description
="Draw the surface that the Sun occupies in the sky",
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",
112 update
=analemmas_update
)
114 coordinates
: StringProperty(
116 description
="Enter coordinates from an online map",
119 default
="00°00′00.00″ 00°00′00.00″",
120 options
={'SKIP_SAVE'})
122 latitude
: FloatProperty(
124 description
="Latitude: (+) Northern (-) Southern",
125 soft_min
=-90.0, soft_max
=90.0,
128 update
=lat_long_update
)
130 longitude
: FloatProperty(
132 description
="Longitude: (-) West of Greenwich (+) East of Greenwich",
133 soft_min
=-180.0, soft_max
=180.0,
136 update
=lat_long_update
)
138 sunrise_time
: FloatProperty(
140 description
="Time at which the Sun rises",
141 soft_min
=0.0, soft_max
=24.0,
143 get
=lambda _
: sun
.sunrise
)
145 sunset_time
: FloatProperty(
147 description
="Time at which the Sun sets",
148 soft_min
=0.0, soft_max
=24.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,
159 get
=lambda _
: sun
.elevation
)
161 sun_azimuth
: FloatProperty(
163 description
="Rotation angle of the Sun from the direction of the north",
164 soft_min
=-pi
, soft_max
=pi
,
168 get
=lambda _
: sun
.azimuth
- bpy
.context
.scene
.sun_pos_properties
.north_offset
)
172 min=1, max=12, default
=TODAY
.month
,
177 min=1, max=31, default
=TODAY
.day
,
182 min=1, max=4000, default
=TODAY
.year
,
185 use_day_of_year
: BoolProperty(
186 description
="Use a single value for the day of year",
187 name
="Use day of year",
191 day_of_year
: IntProperty(
193 min=1, max=366, default
=1,
196 UTC_zone
: FloatProperty(
198 description
="Difference from Greenwich, England, in hours",
200 min=-14.0, max=13, step
=50, default
=0.0,
205 description
="Time of the day",
207 soft_min
=0.0, soft_max
=23.9999, step
=1.0, default
=12.0,
210 sun_distance
: FloatProperty(
212 description
="Distance to the Sun from the origin",
214 min=0.0, soft_max
=3000.0, step
=10.0, default
=50.0,
217 sun_object
: PointerProperty(
219 type=bpy
.types
.Object
,
220 description
="Sun object to use in the scene",
221 poll
=lambda self
, obj
: obj
.type == 'LIGHT',
224 object_collection
: PointerProperty(
226 type=bpy
.types
.Collection
,
227 description
="Collection of objects used to visualize the motion of the Sun",
230 object_collection_type
: EnumProperty(
232 description
="Type of Sun motion to visualize.",
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"),
240 sky_texture
: StringProperty(
243 description
="Name of the sky texture to use",
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",
253 hdr_azimuth
: FloatProperty(
255 description
="Rotation angle of the Sun and environment texture",
258 default
=0.0, precision
=3,
261 hdr_elevation
: FloatProperty(
263 description
="Elevation angle of the Sun",
266 default
=0.0, precision
=3,
269 bind_to_sun
: BoolProperty(
270 name
="Bind Texture to Sun",
271 description
="If enabled, the environment texture moves with the Sun",
275 time_spread
: FloatProperty(
277 description
="Time period around which to spread object collection",
279 soft_min
=1.0, soft_max
=24.0, step
=1.0, default
=23.0,
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",
296 show_refraction
: BoolProperty(
298 description
="Show Sun Refraction choice",
301 show_az_el
: BoolProperty(
302 name
="Azimuth and Elevation Info",
303 description
="Show azimuth and solar elevation info",
306 show_rise_set
: BoolProperty(
307 name
="Sunrise and Sunset Info",
308 description
="Show sunrise and sunset labels",
311 def draw(self
, context
):
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")