1 # SPDX-License-Identifier: GPL-2.0-or-later
4 from bpy
.types
import AddonPreferences
, PropertyGroup
5 from bpy
.props
import (StringProperty
, EnumProperty
, IntProperty
,
6 FloatProperty
, BoolProperty
, PointerProperty
)
7 from bpy
.app
.translations
import pgettext_iface
as iface_
10 from .draw
import north_update
, surface_update
, analemmas_update
11 from .geo
import parse_position
12 from .sun_calc
import format_lat_long
, sun
, update_time
, move_sun
15 from datetime
import datetime
16 TODAY
= datetime
.today()
18 ############################################################################
19 # Sun panel properties
20 ############################################################################
25 def lat_long_update(self
, context
):
28 sun_update(self
, context
)
31 def get_coordinates(self
):
33 return format_lat_long(self
.latitude
, self
.longitude
)
34 return iface_("ERROR: Could not parse coordinates")
37 def set_coordinates(self
, value
):
38 parsed_co
= parse_position(value
)
41 if parsed_co
is not None and len(parsed_co
) == 2:
42 latitude
, longitude
= parsed_co
43 self
.latitude
, self
.longitude
= latitude
, longitude
47 sun_update(self
, bpy
.context
)
50 def sun_update(self
, context
):
51 sun_props
= context
.scene
.sun_pos_properties
56 if sun_props
.show_surface
:
57 surface_update(self
, context
)
58 if sun_props
.show_analemmas
:
59 analemmas_update(self
, context
)
60 if sun_props
.show_north
:
61 north_update(self
, context
)
64 class SunPosProperties(PropertyGroup
):
65 usage_mode
: EnumProperty(
67 description
="Operate in normal mode or environment texture mode",
69 ('NORMAL', "Normal", ""),
70 ('HDR', "Sun + HDR texture", ""),
75 use_daylight_savings
: BoolProperty(
76 name
="Daylight Savings",
77 description
="Daylight savings time adds 1 hour to standard time",
81 use_refraction
: BoolProperty(
82 name
="Use Refraction",
83 description
="Show the apparent Sun position due to atmospheric refraction",
87 show_north
: BoolProperty(
89 description
="Draw a line pointing to the north",
93 north_offset
: FloatProperty(
95 description
="Rotate the scene to choose the North direction",
97 soft_min
=-pi
, soft_max
=pi
, step
=10.0, default
=0.0,
100 show_surface
: BoolProperty(
102 description
="Draw the surface that the Sun occupies in the sky",
104 update
=surface_update
)
106 show_analemmas
: BoolProperty(
107 name
="Show Analemmas",
108 description
="Draw Sun analemmas. These help visualize the motion of the Sun in the sky during the year, for each hour of the day",
110 update
=analemmas_update
)
112 coordinates
: StringProperty(
114 description
="Enter coordinates from an online map",
117 options
={'SKIP_SAVE'})
119 latitude
: FloatProperty(
121 description
="Latitude: (+) Northern (-) Southern",
122 soft_min
=-90.0, soft_max
=90.0,
125 update
=lat_long_update
)
127 longitude
: FloatProperty(
129 description
="Longitude: (-) West of Greenwich (+) East of Greenwich",
130 soft_min
=-180.0, soft_max
=180.0,
133 update
=lat_long_update
)
135 sunrise_time
: FloatProperty(
137 description
="Time at which the Sun rises",
138 soft_min
=0.0, soft_max
=24.0,
140 get
=lambda _
: sun
.sunrise
)
142 sunset_time
: FloatProperty(
144 description
="Time at which the Sun sets",
145 soft_min
=0.0, soft_max
=24.0,
147 get
=lambda _
: sun
.sunset
)
149 sun_elevation
: FloatProperty(
150 name
="Sun Elevation",
151 description
="Elevation angle of the Sun",
152 soft_min
=-pi
/2, soft_max
=pi
/2,
156 get
=lambda _
: sun
.elevation
)
158 sun_azimuth
: FloatProperty(
160 description
="Rotation angle of the Sun from the direction of the north",
161 soft_min
=-pi
, soft_max
=pi
,
165 get
=lambda _
: sun
.azimuth
- bpy
.context
.scene
.sun_pos_properties
.north_offset
)
169 min=1, max=12, default
=TODAY
.month
,
174 min=1, max=31, default
=TODAY
.day
,
179 min=1, max=4000, default
=TODAY
.year
,
182 use_day_of_year
: BoolProperty(
183 description
="Use a single value for the day of year",
184 name
="Use day of year",
188 day_of_year
: IntProperty(
190 min=1, max=366, default
=1,
193 UTC_zone
: FloatProperty(
195 description
="Difference from Greenwich, England, in hours",
197 min=-14.0, max=13, step
=50, default
=0.0,
202 description
="Time of the day",
204 soft_min
=0.0, soft_max
=23.9999, step
=1.0, default
=12.0,
207 sun_distance
: FloatProperty(
209 description
="Distance to the Sun from the origin",
211 min=0.0, soft_max
=3000.0, step
=10.0, default
=50.0,
214 sun_object
: PointerProperty(
216 type=bpy
.types
.Object
,
217 description
="Sun object to use in the scene",
218 poll
=lambda self
, obj
: obj
.type == 'LIGHT',
221 object_collection
: PointerProperty(
223 type=bpy
.types
.Collection
,
224 description
="Collection of objects used to visualize the motion of the Sun",
227 object_collection_type
: EnumProperty(
229 description
="Type of Sun motion to visualize.",
231 ('ANALEMMA', "Analemma", "Trajectory of the Sun in the sky during the year, for a given time of the day"),
232 ('DIURNAL', "Diurnal", "Trajectory of the Sun in the sky during a single day"),
237 sky_texture
: StringProperty(
240 description
="Name of the sky texture to use",
243 hdr_texture
: StringProperty(
244 default
="Environment Texture",
245 name
="Environment Texture",
246 description
="Name of the environment texture to use. World nodes must be enabled "
247 "and the color set to an environment Texture",
250 hdr_azimuth
: FloatProperty(
252 description
="Rotation angle of the Sun and environment texture",
255 default
=0.0, precision
=3,
258 hdr_elevation
: FloatProperty(
260 description
="Elevation angle of the Sun",
263 default
=0.0, precision
=3,
266 bind_to_sun
: BoolProperty(
267 name
="Bind Texture to Sun",
268 description
="If enabled, the environment texture moves with the Sun",
272 time_spread
: FloatProperty(
274 description
="Time period around which to spread object collection",
276 soft_min
=1.0, soft_max
=24.0, step
=1.0, default
=23.0,
279 ############################################################################
280 # Preference panel properties
281 ############################################################################
284 class SunPosAddonPreferences(AddonPreferences
):
285 bl_idname
= __package__
287 show_overlays
: BoolProperty(
288 name
="Show Overlays",
289 description
="Display overlays in the viewport: the direction of the north, analemmas and the Sun surface",
293 show_refraction
: BoolProperty(
295 description
="Show Sun Refraction choice",
298 show_az_el
: BoolProperty(
299 name
="Azimuth and Elevation Info",
300 description
="Show azimuth and solar elevation info",
303 show_rise_set
: BoolProperty(
304 name
="Sunrise and Sunset Info",
305 description
="Show sunrise and sunset labels",
308 def draw(self
, context
):
314 col
.label(text
="Show options and info:")
315 flow
= col
.grid_flow(columns
=0, even_columns
=True, even_rows
=False, align
=False)
316 flow
.prop(self
, "show_refraction")
317 flow
.prop(self
, "show_overlays")
318 flow
.prop(self
, "show_az_el")
319 flow
.prop(self
, "show_rise_set")