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 options
={'SKIP_SAVE'})
121 latitude
: FloatProperty(
123 description
="Latitude: (+) Northern (-) Southern",
124 soft_min
=-90.0, soft_max
=90.0,
127 update
=lat_long_update
)
129 longitude
: FloatProperty(
131 description
="Longitude: (-) West of Greenwich (+) East of Greenwich",
132 soft_min
=-180.0, soft_max
=180.0,
135 update
=lat_long_update
)
137 sunrise_time
: FloatProperty(
139 description
="Time at which the Sun rises",
140 soft_min
=0.0, soft_max
=24.0,
142 get
=lambda _
: sun
.sunrise
)
144 sunset_time
: FloatProperty(
146 description
="Time at which the Sun sets",
147 soft_min
=0.0, soft_max
=24.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,
158 get
=lambda _
: sun
.elevation
)
160 sun_azimuth
: FloatProperty(
162 description
="Rotation angle of the Sun from the direction of the north",
163 soft_min
=-pi
, soft_max
=pi
,
167 get
=lambda _
: sun
.azimuth
- bpy
.context
.scene
.sun_pos_properties
.north_offset
)
171 min=1, max=12, default
=TODAY
.month
,
176 min=1, max=31, default
=TODAY
.day
,
181 min=1, max=4000, default
=TODAY
.year
,
184 use_day_of_year
: BoolProperty(
185 description
="Use a single value for the day of year",
186 name
="Use day of year",
190 day_of_year
: IntProperty(
192 min=1, max=366, default
=1,
195 UTC_zone
: FloatProperty(
197 description
="Difference from Greenwich, England, in hours",
199 min=-14.0, max=13, step
=50, default
=0.0,
204 description
="Time of the day",
206 soft_min
=0.0, soft_max
=23.9999, step
=1.0, default
=12.0,
209 sun_distance
: FloatProperty(
211 description
="Distance to the Sun from the origin",
213 min=0.0, soft_max
=3000.0, step
=10.0, default
=50.0,
216 sun_object
: PointerProperty(
218 type=bpy
.types
.Object
,
219 description
="Sun object to use in the scene",
220 poll
=lambda self
, obj
: obj
.type == 'LIGHT',
223 object_collection
: PointerProperty(
225 type=bpy
.types
.Collection
,
226 description
="Collection of objects used to visualize the motion of the Sun",
229 object_collection_type
: EnumProperty(
231 description
="Type of Sun motion to visualize.",
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"),
239 sky_texture
: StringProperty(
242 description
="Name of the sky texture to use",
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",
252 hdr_azimuth
: FloatProperty(
254 description
="Rotation angle of the Sun and environment texture",
257 default
=0.0, precision
=3,
260 hdr_elevation
: FloatProperty(
262 description
="Elevation angle of the Sun",
265 default
=0.0, precision
=3,
268 bind_to_sun
: BoolProperty(
269 name
="Bind Texture to Sun",
270 description
="If enabled, the environment texture moves with the Sun",
274 time_spread
: FloatProperty(
276 description
="Time period around which to spread object collection",
278 soft_min
=1.0, soft_max
=24.0, step
=1.0, default
=23.0,
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",
295 show_refraction
: BoolProperty(
297 description
="Show Sun Refraction choice",
300 show_az_el
: BoolProperty(
301 name
="Azimuth and Elevation Info",
302 description
="Show azimuth and solar elevation info",
305 show_rise_set
: BoolProperty(
306 name
="Sunrise and Sunset Info",
307 description
="Show sunrise and sunset labels",
310 def draw(self
, context
):
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")