Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ui_sun.py « sun_position - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c6eebc338e3aa4ae48a66f0916c5ff1e5229ecf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# SPDX-License-Identifier: GPL-2.0-or-later

import bpy
from bpy.types import Operator, Menu
from bl_operators.presets import AddPresetBase
import os

from .sun_calc import (format_lat_long, format_time, format_hms, sun)


# -------------------------------------------------------------------
# Choice list of places, month and day at 12:00 noon
# -------------------------------------------------------------------


class SUNPOS_MT_Presets(Menu):
    bl_label = "Sun Position Presets"
    preset_subdir = "operator/sun_position"
    preset_operator = "script.execute_preset"
    draw = Menu.draw_preset


class SUNPOS_OT_AddPreset(AddPresetBase, Operator):
    '''Add Sun Position preset'''
    bl_idname = "world.sunpos_add_preset"
    bl_label = "Add Sun Position preset"
    preset_menu = "SUNPOS_MT_Presets"

    # variable used for all preset values
    preset_defines = [
        "sun_props = bpy.context.scene.sun_pos_properties"
    ]

    # properties to store in the preset
    preset_values = [
        "sun_props.day",
        "sun_props.month",
        "sun_props.time",
        "sun_props.year",
        "sun_props.UTC_zone",
        "sun_props.use_daylight_savings",
        "sun_props.latitude",
        "sun_props.longitude",
    ]

    # where to store the preset
    preset_subdir = "operator/sun_position"


# -------------------------------------------------------------------
#
#   Draw the Sun Panel, sliders, et. al.
#
# -------------------------------------------------------------------

class SUNPOS_PT_Panel(bpy.types.Panel):
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_context = "world"
    bl_label = "Sun Position"
    bl_options = {'DEFAULT_CLOSED'}

    def draw(self, context):
        sp = context.scene.sun_pos_properties
        p = context.preferences.addons[__package__].preferences
        layout = self.layout
        self.draw_panel(context, sp, p, layout)

    def draw_panel(self, context, sp, p, layout):
        col = self.layout.column(align=True)
        col.label(text="Usage Mode")
        row = col.row()
        row.prop(sp, "usage_mode", expand=True)
        col.separator()
        if sp.usage_mode == "HDR":
            self.draw_environ_mode_panel(context, sp, p, layout)
        else:
            self.draw_normal_mode_panel(context, sp, p, layout)

    def draw_environ_mode_panel(self, context, sp, p, layout):
        flow = layout.grid_flow(row_major=True, columns=0, even_columns=True,
                             even_rows=False, align=False)

        col = flow.column(align=True)
        col.label(text="Environment Texture")

        if context.scene.world is not None:
            if context.scene.world.node_tree is not None:
                col.prop_search(sp, "hdr_texture",
                                context.scene.world.node_tree, "nodes", text="")
            else:
                col.label(text="Please activate Use Nodes in the World panel.",
                          icon="ERROR")
        else:
            col.label(text="Please select World in the World panel.",
                      icon="ERROR")

        col.separator()

        col = flow.column(align=True)
        col.label(text="Sun Object")
        col.prop_search(sp, "sun_object",
                        context.view_layer, "objects", text="")
        col.separator()

        col = flow.column(align=True)
        col.prop(sp, "sun_distance")
        if not sp.bind_to_sun:
            col.prop(sp, "hdr_elevation")
        col.prop(sp, "hdr_azimuth")
        col.separator()

        col = flow.column(align=True)
        if sp.bind_to_sun:
            col.prop(sp, "bind_to_sun", toggle=True, icon="CONSTRAINT",
                     text="Release binding")
        else:
            col.prop(sp, "bind_to_sun", toggle=True, icon="CONSTRAINT",
                     text="Bind Texture to Sun")

        row = col.row(align=True)
        row.enabled = not sp.bind_to_sun
        row.operator("world.sunpos_show_hdr", icon='LIGHT_SUN')

    def draw_normal_mode_panel(self, context, sp, p, layout):
        if p.show_time_place:
            row = layout.row(align=True)
            row.menu(SUNPOS_MT_Presets.__name__, text=SUNPOS_MT_Presets.bl_label)
            row.operator(SUNPOS_OT_AddPreset.bl_idname, text="", icon='ADD')
            row.operator(SUNPOS_OT_AddPreset.bl_idname, text="", icon='REMOVE').remove_active = True

        col = layout.column(align=True)
        col.use_property_split = True
        col.use_property_decorate = False
        col.prop(sp, "sun_object")
        col.separator()

        col.prop(sp, "object_collection")
        if sp.object_collection:
            col.prop(sp, "object_collection_type")
            if sp.object_collection_type == 'DIURNAL':
                col.prop(sp, "time_spread")
        col.separator()

        if context.scene.world is not None:
            if context.scene.world.node_tree is not None:
                col.prop_search(sp, "sky_texture",
                                context.scene.world.node_tree, "nodes")
            else:
                col.label(text="Please activate Use Nodes in the World panel.",
                          icon="ERROR")
        else:
            col.label(text="Please select World in the World panel.",
                      icon="ERROR")

class SUNPOS_PT_Location(bpy.types.Panel):
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_context = "world"
    bl_label = "Location"
    bl_parent_id = "SUNPOS_PT_Panel"

    @classmethod
    def poll(self, context):
        sp = context.scene.sun_pos_properties
        return sp.usage_mode != "HDR"

    def draw(self, context):
        layout = self.layout
        sp = context.scene.sun_pos_properties
        p = context.preferences.addons[__package__].preferences

        col = layout.column(align=True)
        col.label(text="Enter Coordinates")
        col.prop(sp, "co_parser", text='', icon='URL')

        layout.separator()

        flow = layout.grid_flow(row_major=True, columns=0, even_columns=True, even_rows=False, align=False)

        col = flow.column(align=True)
        col.prop(sp, "latitude")
        if p.show_dms:
            row = col.row()
            row.alignment = 'RIGHT'
            row.label(text=format_lat_long(sp.latitude, True))

        col = flow.column(align=True)
        col.prop(sp, "longitude")
        if p.show_dms:
            row = col.row()
            row.alignment = 'RIGHT'
            row.label(text=format_lat_long(sp.longitude, False))
        col.separator()

        if p.show_north:
            col = flow.column(align=True)
            col.prop(sp, "show_north", toggle=True)
            col.prop(sp, "north_offset")
            col.separator()

        if p.show_az_el:
            col = flow.column(align=True)
            split = col.split(factor=0.4, align=True)
            split.label(text="Azimuth:")
            split.label(text=str(round(sun.azimuth, 3)) + "°")
            split = col.split(factor=0.4, align=True)
            split.label(text="Elevation:")
            split.label(text=str(round(sun.elevation, 3)) + "°")
            col.separator()

        if p.show_refraction:
            col = flow.column()
            col.prop(sp, "use_refraction")
            col.separator()

        col = flow.column()
        col.prop(sp, "sun_distance")


class SUNPOS_PT_Time(bpy.types.Panel):
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_context = "world"
    bl_label = "Time"
    bl_parent_id = "SUNPOS_PT_Panel"

    @classmethod
    def poll(self, context):
        sp = context.scene.sun_pos_properties
        return sp.usage_mode != "HDR"

    def draw(self, context):
        layout = self.layout
        sp = context.scene.sun_pos_properties
        p = context.preferences.addons[__package__].preferences

        flow = layout.grid_flow(row_major=True, columns=0, even_columns=True, even_rows=False, align=False)

        col = flow.column(align=True)
        col.prop(sp, "use_day_of_year",
                 icon='SORTTIME')
        if sp.use_day_of_year:
            col.prop(sp, "day_of_year")
        else:
            col.prop(sp, "day")
            col.prop(sp, "month")
        col.prop(sp, "year")
        col.separator()

        col = flow.column(align=True)
        col.prop(sp, "time")
        col.prop(sp, "UTC_zone")
        if p.show_daylight_savings:
            col.prop(sp, "use_daylight_savings")
        col.separator()

        col = flow.column(align=True)
        lt = format_time(sp.time,
                         p.show_daylight_savings and sp.use_daylight_savings,
                         sp.longitude)
        ut = format_time(sp.time,
                         p.show_daylight_savings and sp.use_daylight_savings,
                         sp.longitude,
                         sp.UTC_zone)
        col.alignment = 'CENTER'

        split = col.split(factor=0.5, align=True)
        split.label(text="Local:", icon='TIME')
        split.label(text=lt)
        split = col.split(factor=0.5, align=True)
        split.label(text="UTC:", icon='PREVIEW_RANGE')
        split.label(text=ut)
        col.separator()


        col = flow.column(align=True)
        col.alignment = 'CENTER'
        if p.show_rise_set:
            sr = format_hms(sun.sunrise.time)
            ss = format_hms(sun.sunset.time)

            split = col.split(factor=0.5, align=True)
            split.label(text="Sunrise:", icon='LIGHT_SUN')
            split.label(text=sr)
            split = col.split(factor=0.5, align=True)
            split.label(text="Sunset:", icon='SOLO_ON')
            split.label(text=ss)

        col.separator()