forked from blender/blender-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
development_icon_get.py
494 lines (401 loc) · 14.6 KB
/
development_icon_get.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# SPDX-FileCopyrightText: 2010-2022 Blender Foundation
#
# SPDX-License-Identifier: GPL-2.0-or-later
bl_info = {
"name": "Icon Viewer",
"description": "Click an icon to copy its name to the clipboard",
"author": "roaoao",
"version": (1, 4, 1),
"blender": (3, 4, 0),
"location": "Text Editor > Dev Tab > Icon Viewer",
"doc_url": "{BLENDER_MANUAL_URL}/addons/development/icon_viewer.html",
"category": "Development",
}
import bpy
import math
from bpy.props import (
BoolProperty,
StringProperty,
)
DPI = 72
POPUP_PADDING = 10
PANEL_PADDING = 44
WIN_PADDING = 32
ICON_SIZE = 20
HISTORY_SIZE = 100
HISTORY = []
def ui_scale():
prefs = bpy.context.preferences.system
return prefs.dpi / DPI
def prefs():
return bpy.context.preferences.addons[__name__].preferences
class Icons:
def __init__(self, is_popup=False):
self._filtered_icons = None
self._filter = ""
self.filter = ""
self.selected_icon = ""
self.is_popup = is_popup
@property
def filter(self):
return self._filter
@filter.setter
def filter(self, value):
if self._filter == value:
return
self._filter = value
self.update()
@property
def filtered_icons(self):
if self._filtered_icons is None:
self._filtered_icons = []
icon_filter = self._filter.upper()
self.filtered_icons.clear()
pr = prefs()
icons = bpy.types.UILayout.bl_rna.functions[
"prop"].parameters["icon"].enum_items.keys()
for icon in icons:
if icon == 'NONE' or \
icon_filter and icon_filter not in icon or \
not pr.show_brush_icons and "BRUSH_" in icon and \
icon != 'BRUSH_DATA' or \
not pr.show_matcap_icons and "MATCAP_" in icon or \
not pr.show_event_icons and (
"EVENT_" in icon or "MOUSE_" in icon
) or \
not pr.show_colorset_icons and "COLORSET_" in icon:
continue
self._filtered_icons.append(icon)
return self._filtered_icons
@property
def num_icons(self):
return len(self.filtered_icons)
def update(self):
if self._filtered_icons is not None:
self._filtered_icons.clear()
self._filtered_icons = None
def draw(self, layout, num_cols=0, icons=None):
if icons:
filtered_icons = reversed(icons)
else:
filtered_icons = self.filtered_icons
column = layout.column(align=True)
row = column.row(align=True)
row.alignment = 'CENTER'
selected_icon = self.selected_icon if self.is_popup else \
bpy.context.window_manager.clipboard
col_idx = 0
for i, icon in enumerate(filtered_icons):
p = row.operator(
IV_OT_icon_select.bl_idname, text="",
icon=icon, emboss=icon == selected_icon)
p.icon = icon
p.force_copy_on_select = not self.is_popup
col_idx += 1
if col_idx > num_cols - 1:
if icons:
break
col_idx = 0
if i < len(filtered_icons) - 1:
row = column.row(align=True)
row.alignment = 'CENTER'
if col_idx != 0 and not icons and i >= num_cols:
for _ in range(num_cols - col_idx):
row.label(text="", icon='BLANK1')
if not filtered_icons:
row.label(text="No icons were found")
class IV_Preferences(bpy.types.AddonPreferences):
bl_idname = __name__
panel_icons = Icons()
popup_icons = Icons(is_popup=True)
def update_icons(self, context):
self.panel_icons.update()
self.popup_icons.update()
def set_panel_filter(self, value):
self.panel_icons.filter = value
panel_filter: StringProperty(
description="Filter",
default="",
get=lambda s: s.panel_icons.filter,
set=set_panel_filter,
options={'TEXTEDIT_UPDATE'})
show_panel_icons: BoolProperty(
name="Show Icons",
description="Show icons", default=True)
show_history: BoolProperty(
name="Show History",
description="Show history", default=True)
show_brush_icons: BoolProperty(
name="Show Brush Icons",
description="Show brush icons", default=True,
update=update_icons)
show_matcap_icons: BoolProperty(
name="Show Matcap Icons",
description="Show matcap icons", default=True,
update=update_icons)
show_event_icons: BoolProperty(
name="Show Event Icons",
description="Show event icons", default=True,
update=update_icons)
show_colorset_icons: BoolProperty(
name="Show Colorset Icons",
description="Show colorset icons", default=True,
update=update_icons)
copy_on_select: BoolProperty(
name="Copy Icon On Click",
description="Copy icon on click", default=True)
close_on_select: BoolProperty(
name="Close Popup On Click",
description=(
"Close the popup on click.\n"
"Not supported by some windows (User Preferences, Render)"
),
default=False)
auto_focus_filter: BoolProperty(
name="Auto Focus Input Field",
description="Auto focus input field", default=True)
show_panel: BoolProperty(
name="Show Panel",
description="Show the panel in the Text Editor", default=True)
show_header: BoolProperty(
name="Show Header",
description="Show the header in the Python Console",
default=True)
def draw(self, context):
layout = self.layout
row = layout.row()
row.scale_y = 1.5
row.operator(IV_OT_icons_show.bl_idname)
row = layout.row()
col = row.column(align=True)
col.label(text="Icons:")
col.prop(self, "show_matcap_icons")
col.prop(self, "show_brush_icons")
col.prop(self, "show_colorset_icons")
col.prop(self, "show_event_icons")
col.separator()
col.prop(self, "show_history")
col = row.column(align=True)
col.label(text="Popup:")
col.prop(self, "auto_focus_filter")
col.prop(self, "copy_on_select")
if self.copy_on_select:
col.prop(self, "close_on_select")
col = row.column(align=True)
col.label(text="Panel:")
col.prop(self, "show_panel")
if self.show_panel:
col.prop(self, "show_panel_icons")
col.separator()
col.label(text="Header:")
col.prop(self, "show_header")
class IV_PT_icons(bpy.types.Panel):
bl_space_type = "TEXT_EDITOR"
bl_region_type = "UI"
bl_label = "Icon Viewer"
bl_category = "Dev"
bl_options = {'DEFAULT_CLOSED'}
@staticmethod
def tag_redraw():
wm = bpy.context.window_manager
if not wm:
return
for w in wm.windows:
for a in w.screen.areas:
if a.type == 'TEXT_EDITOR':
for r in a.regions:
if r.type == 'UI':
r.tag_redraw()
def draw(self, context):
pr = prefs()
row = self.layout.row(align=True)
if pr.show_panel_icons:
row.prop(pr, "panel_filter", text="", icon='VIEWZOOM')
else:
row.operator(IV_OT_icons_show.bl_idname)
row.operator(
IV_OT_panel_menu_call.bl_idname, text="", icon='COLLAPSEMENU')
_, y0 = context.region.view2d.region_to_view(0, 0)
_, y1 = context.region.view2d.region_to_view(0, 10)
region_scale = 10 / abs(y0 - y1)
num_cols = max(
1,
(context.region.width - PANEL_PADDING) //
math.ceil(ui_scale() * region_scale * ICON_SIZE))
col = None
if HISTORY and pr.show_history:
col = self.layout.column(align=True)
pr.panel_icons.draw(col.box(), num_cols, HISTORY)
if pr.show_panel_icons:
col = col or self.layout.column(align=True)
pr.panel_icons.draw(col.box(), num_cols)
@classmethod
def poll(cls, context):
return prefs().show_panel
class IV_OT_panel_menu_call(bpy.types.Operator):
bl_idname = "iv.panel_menu_call"
bl_label = ""
bl_description = "Menu"
bl_options = {'INTERNAL'}
def menu(self, menu, context):
pr = prefs()
layout = menu.layout
layout.prop(pr, "show_panel_icons")
layout.prop(pr, "show_history")
if not pr.show_panel_icons:
return
layout.separator()
layout.prop(pr, "show_matcap_icons")
layout.prop(pr, "show_brush_icons")
layout.prop(pr, "show_colorset_icons")
layout.prop(pr, "show_event_icons")
def execute(self, context):
context.window_manager.popup_menu(self.menu, title="Icon Viewer")
return {'FINISHED'}
class IV_OT_icon_select(bpy.types.Operator):
bl_idname = "iv.icon_select"
bl_label = ""
bl_description = "Select the icon"
bl_options = {'INTERNAL'}
icon: StringProperty()
force_copy_on_select: BoolProperty()
def execute(self, context):
pr = prefs()
pr.popup_icons.selected_icon = self.icon
if pr.copy_on_select or self.force_copy_on_select:
context.window_manager.clipboard = self.icon
self.report({'INFO'}, self.icon)
if pr.close_on_select and IV_OT_icons_show.instance:
IV_OT_icons_show.instance.close()
if pr.show_history:
if self.icon in HISTORY:
HISTORY.remove(self.icon)
if len(HISTORY) >= HISTORY_SIZE:
HISTORY.pop(0)
HISTORY.append(self.icon)
return {'FINISHED'}
class IV_OT_icons_show(bpy.types.Operator):
bl_idname = "iv.icons_show"
bl_label = "Icon Viewer"
bl_description = "Icon viewer"
bl_property = "filter_auto_focus"
instance = None
def set_filter(self, value):
prefs().popup_icons.filter = value
def set_selected_icon(self, value):
if IV_OT_icons_show.instance:
IV_OT_icons_show.instance.auto_focusable = False
filter_auto_focus: StringProperty(
description="Filter",
get=lambda s: prefs().popup_icons.filter,
set=set_filter,
options={'TEXTEDIT_UPDATE', 'SKIP_SAVE'})
filter: StringProperty(
description="Filter",
get=lambda s: prefs().popup_icons.filter,
set=set_filter,
options={'TEXTEDIT_UPDATE'})
selected_icon: StringProperty(
description="Selected Icon",
get=lambda s: prefs().popup_icons.selected_icon,
set=set_selected_icon)
def get_num_cols(self, num_icons):
return round(1.3 * math.sqrt(num_icons))
def draw_header(self, layout):
pr = prefs()
header = layout.box()
header = header.split(factor=0.75) if self.selected_icon else \
header.row()
row = header.row(align=True)
row.prop(pr, "show_matcap_icons", text="", icon='SHADING_RENDERED')
row.prop(pr, "show_brush_icons", text="", icon='BRUSH_DATA')
row.prop(pr, "show_colorset_icons", text="", icon='COLOR')
row.prop(pr, "show_event_icons", text="", icon='HAND')
row.separator()
row.prop(
pr, "copy_on_select", text="",
icon='COPYDOWN', toggle=True)
if pr.copy_on_select:
sub = row.row(align=True)
if bpy.context.window.screen.name == "temp":
sub.alert = True
sub.prop(
pr, "close_on_select", text="",
icon='RESTRICT_SELECT_OFF', toggle=True)
row.prop(
pr, "auto_focus_filter", text="",
icon='OUTLINER_DATA_FONT', toggle=True)
row.separator()
if self.auto_focusable and pr.auto_focus_filter:
row.prop(self, "filter_auto_focus", text="", icon='VIEWZOOM')
else:
row.prop(self, "filter", text="", icon='VIEWZOOM')
if self.selected_icon:
row = header.row()
row.prop(self, "selected_icon", text="", icon=self.selected_icon)
def draw(self, context):
pr = prefs()
col = self.layout
self.draw_header(col)
history_num_cols = int(
(self.width - POPUP_PADDING) / (ui_scale() * ICON_SIZE))
num_cols = min(
self.get_num_cols(len(pr.popup_icons.filtered_icons)),
history_num_cols)
subcol = col.column(align=True)
if HISTORY and pr.show_history:
pr.popup_icons.draw(subcol.box(), history_num_cols, HISTORY)
pr.popup_icons.draw(subcol.box(), num_cols)
def close(self):
bpy.context.window.screen = bpy.context.window.screen
def check(self, context):
return True
def cancel(self, context):
IV_OT_icons_show.instance = None
IV_PT_icons.tag_redraw()
def execute(self, context):
if not IV_OT_icons_show.instance:
return {'CANCELLED'}
IV_OT_icons_show.instance = None
pr = prefs()
if self.selected_icon and not pr.copy_on_select:
context.window_manager.clipboard = self.selected_icon
self.report({'INFO'}, self.selected_icon)
pr.popup_icons.selected_icon = ""
IV_PT_icons.tag_redraw()
return {'FINISHED'}
def invoke(self, context, event):
pr = prefs()
pr.popup_icons.selected_icon = ""
pr.popup_icons.filter = ""
IV_OT_icons_show.instance = self
self.auto_focusable = True
num_cols = self.get_num_cols(len(pr.popup_icons.filtered_icons))
self.width = int(min(
ui_scale() * (num_cols * ICON_SIZE + POPUP_PADDING),
context.window.width - WIN_PADDING))
return context.window_manager.invoke_props_dialog(
self, width=self.width)
def draw_console_header(self, context):
if not prefs().show_header:
return
self.layout.operator(IV_OT_icons_show.bl_idname)
classes = (
IV_PT_icons,
IV_OT_panel_menu_call,
IV_OT_icon_select,
IV_OT_icons_show,
IV_Preferences,
)
def register():
if bpy.app.background:
return
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.CONSOLE_HT_header.append(draw_console_header)
def unregister():
if bpy.app.background:
return
bpy.types.CONSOLE_HT_header.remove(draw_console_header)
for cls in classes:
bpy.utils.unregister_class(cls)