-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
199 lines (140 loc) · 5.57 KB
/
__init__.py
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
import bpy
import os
from bpy.types import Menu, Operator, AddonPreferences
from bpy.props import StringProperty
def get_current_mode(context):
preferences = context.preferences
addon_prefs = preferences.addons[__package__].preferences
import darkdetect
addon_prefs.current_mode = darkdetect.theme()
return addon_prefs.current_mode
def set_theme_from_system(context):
preferences = context.preferences
addon_prefs = preferences.addons[__package__].preferences
light_theme_path = addon_prefs.light_theme_path
dark_theme_path = addon_prefs.dark_theme_path
current_mode = get_current_mode(context)
# Default to dark.
switch_to_theme = dark_theme_path
if current_mode == 'Light':
switch_to_theme = light_theme_path
# Set the theme.
bpy.ops.script.execute_preset(
filepath=switch_to_theme,
menu_idname="USERPREF_MT_interface_theme_presets",
)
# Set the theme name as label for dropdowns.
WM_MT_autoswitch_theme_light.bl_label = bpy.path.display_name(light_theme_path)
WM_MT_autoswitch_theme_dark.bl_label = bpy.path.display_name(dark_theme_path)
def load_post_handler(context):
set_theme_from_system(bpy.context)
class WM_AutoThemePreferences(AddonPreferences):
bl_idname = __package__
scripts_path = bpy.utils.script_paths()[0]
themes_path = os.path.join(scripts_path, "presets/interface_theme")
light_theme_path: StringProperty(
name='Light Theme Filepath',
description='Filepath to use as light theme',
default=os.path.join(themes_path, "Blender_Light.xml"),
)
dark_theme_path: StringProperty(
name='Dark Theme Filepath',
description='Filepath to use as dark theme',
default=os.path.join(themes_path, "Blender_Dark.xml"),
)
current_mode: StringProperty(
name='Light or Dark',
default="Not detected yet",
)
def draw(self, _context):
layout = self.layout
layout.use_property_split = True
col = layout.column()
split = col.split(factor=0.4)
split.alignment = 'RIGHT'
split.label(text="System Appearance Mode")
split = split.column()
row = split.row()
row.label(text=self.current_mode)
row.operator("wm.autoswitch_set_theme_auto", text="", icon='FILE_REFRESH')
col.separator(type='LINE')
sub = col.column(heading="Light Theme")
sub.menu("WM_MT_autoswitch_theme_light", text=WM_MT_autoswitch_theme_light.bl_label)
sub = col.column(heading="Dark Theme")
sub.menu("WM_MT_autoswitch_theme_dark", text=WM_MT_autoswitch_theme_dark.bl_label)
class WM_OT_autoswitch_set_theme_auto(Operator):
"""Switch the theme to follow system light/dark mode"""
bl_idname = "wm.autoswitch_set_theme_auto"
bl_label = "Manually Set Themes"
def execute(self, context):
set_theme_from_system(context)
return {'FINISHED'}
class WM_OT_autoswitch_set_theme_light(Operator):
"""Set theme path to use when light"""
bl_idname = "wm.autoswitch_set_theme_light"
bl_label = "Set Light Theme to AutoSwitch to"
menu_idname: StringProperty(
name='WM_MT_autoswitch_theme_light',
)
filepath: StringProperty(
name='Theme Filepath',
)
def execute(self, context):
preferences = context.preferences
addon_prefs = preferences.addons[__package__].preferences
addon_prefs.light_theme_path = self.filepath
WM_MT_autoswitch_theme_light.bl_label = bpy.path.display_name(self.filepath)
return {'FINISHED'}
class WM_OT_autoswitch_set_theme_dark(Operator):
"""Set theme path to use when dark"""
bl_idname = "wm.autoswitch_set_theme_dark"
bl_label = "Set Dark Theme to AutoSwitch to"
menu_idname: StringProperty(
name='WM_MT_autoswitch_theme_dark',
)
filepath: StringProperty(
name='Theme Filepath',
)
def execute(self, context):
preferences = context.preferences
addon_prefs = preferences.addons[__package__].preferences
addon_prefs.dark_theme_path = self.filepath
WM_MT_autoswitch_theme_dark.bl_label = bpy.path.display_name(self.filepath)
return {'FINISHED'}
class WM_MT_autoswitch_theme_light(Menu):
bl_label = "Blender Light"
bl_description = "Set light theme to auto-switch to"
preset_subdir = "interface_theme"
preset_operator = "wm.autoswitch_set_theme_light"
draw = Menu.draw_preset
class WM_MT_autoswitch_theme_dark(Menu):
bl_label = "Blender Dark"
bl_description = "Set dark theme to auto-switch to"
preset_subdir = "interface_theme"
preset_operator = "wm.autoswitch_set_theme_dark"
draw = Menu.draw_preset
classes = (
WM_AutoThemePreferences,
WM_MT_autoswitch_theme_light,
WM_MT_autoswitch_theme_dark,
WM_OT_autoswitch_set_theme_auto,
WM_OT_autoswitch_set_theme_light,
WM_OT_autoswitch_set_theme_dark,
)
def register():
for c in classes:
bpy.utils.register_class(c)
from bl_pkg import theme_preset_draw
WM_MT_autoswitch_theme_light.append(theme_preset_draw)
WM_MT_autoswitch_theme_dark.append(theme_preset_draw)
bpy.app.handlers.load_post.append(load_post_handler)
def unregister():
for c in classes:
bpy.utils.unregister_class(c)
from bl_pkg import theme_preset_draw
WM_MT_autoswitch_theme_light.remove(theme_preset_draw)
WM_MT_autoswitch_theme_dark.remove(theme_preset_draw)
if load_post_handler in bpy.app.handlers.load_post:
bpy.app.handlers.load_post.remove(load_post_handler)
if __name__ == "__main__":
register()