-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBrush_Manager.py
4758 lines (4092 loc) · 172 KB
/
Brush_Manager.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENCE BLOCK #####
import bpy
import os
import sys
import subprocess
import json
import bpy.utils.previews
from bpy.app.handlers import persistent
from bpy.types import Operator, Menu, Panel, PropertyGroup, AddonPreferences, Scene, WindowManager, BlendData
from bpy.props import *
import rna_keymap_ui
from bl_ui import space_toolsystem_common, space_toolsystem_toolbar
from .t3dn_bip import previews
from .t3dn_bip.ops import InstallPillow
from .t3dn_bip.utils import support_pillow
Addon_Name = __package__
def prefs():
return bpy.context.preferences.addons[Addon_Name].preferences
MODE = None
UI_MODE = False
START_FAV_LOADED = {}
FAV_SETTINGS_LOADED = {}
IS_INIT_SMEAR = {}
SET_DEFAULT_ICONS = {}
CURRENT_MODE_CATEGORY = {}
BRUSHES_SCULPT_NAMES = [
'Blob', 'Clay', 'Clay Strips', 'Clay Thumb', 'Cloth',
'Crease', 'Draw Face Sets', 'Draw Sharp', 'Elastic Deform',
'Fill/Deepen', 'Flatten/Contrast', 'Grab', 'Inflate/Deflate',
'Layer', 'Mask', 'Multi-plane Scrape', 'Nudge', 'Pinch/Magnify',
'Pose', 'Rotate', 'Scrape/Peaks', 'SculptDraw', 'Simplify',
'Slide Relax', 'Smooth', 'Snake Hook', 'Thumb'
]
if bpy.app.version >= (3, 2, 0):
BRUSHES_SCULPT_NAMES += ['Paint', 'Smear Sculpt']
BRUSHES_IPAINT_NAMES = ['Clone', 'Fill', 'Mask', 'Smear', 'Soften', 'TexDraw']
BRUSHES_GPAINT_NAMES = [
'Airbrush', 'Eraser Hard', 'Eraser Point', 'Eraser Soft',
'Eraser Stroke', 'Fill Area', 'Ink Pen', 'Ink Pen Rough', 'Marker Bold',
'Marker Chisel', 'Pen', 'Pencil', 'Pencil Soft', 'Tint'
]
# print([b.name for b in bpy.data.brushes if b.use_paint_weight])
BRUSHES_WPAINT_NAMES = [
'Add', 'Average', 'Blur', 'Darken', 'Draw', 'Lighten', 'Mix', 'Multiply', 'Subtract',
'Smear Weight'
]
BRUSHES_VPAINT_NAMES = [
'Add', 'Average', 'Blur', 'Darken', 'Draw', 'Lighten', 'Mix', 'Multiply', 'Subtract',
'Smear Vertex',
]
# use_vertex_grease_pencil
BRUSHES_GVERTEX_NAMES = [
'Vertex Average', 'Vertex Blur', 'Vertex Draw', 'Vertex Replace', 'Vertex Smear'
]
def collect_tools(mode):
b_tools = []
o_tools = []
tools = space_toolsystem_toolbar.VIEW3D_PT_tools_active.tools_from_context(None, mode)
for item in tools:
if item is None:
continue
if type(item) is tuple:
for subitem in item:
o_tools.append(subitem.label)
continue
if item.idname.startswith("builtin_brush"):
b_tools.append(item.label)
else:
o_tools.append(item.label)
b_tools.sort()
return b_tools, o_tools
BRUSHES_SCULPT, TOOLS_SCULPT = collect_tools('SCULPT')
BRUSHES_IPAINT, TOOLS_IPAINT = collect_tools('PAINT_TEXTURE')
BRUSHES_GPAINT, TOOLS_GPAINT = collect_tools('PAINT_GPENCIL')
BRUSHES_WPAINT, TOOLS_WPAINT = collect_tools('PAINT_WEIGHT')
BRUSHES_VPAINT, TOOLS_VPAINT = collect_tools('PAINT_VERTEX')
BRUSHES_GVERTEX, TOOLS_GVERTEX = collect_tools('VERTEX_GPENCIL')
def evaluate_brush_tools(brushes, mode=''):
if not mode:
mode = MODE
brush_tool_names = []
for t_label in brushes:
found = False
if mode == 'SCULPT':
if t_label == 'Draw':
brush_tool_names.append((t_label, 'SculptDraw'))
continue
if t_label == 'Smear':
brush_tool_names.append((t_label, 'Smear Sculpt'))
continue
for brush in BRUSHES_SCULPT_NAMES:
if brush.split('/')[0] == t_label:
brush_tool_names.append((t_label, brush))
found = True
break
if not found:
brush_tool_names.append((t_label, t_label))
elif mode == 'PAINT_TEXTURE':
if t_label == 'Draw':
brush_tool_names.append((t_label, 'TexDraw'))
continue
brush_tool_names.append((t_label, t_label))
elif mode == 'PAINT_WEIGHT':
if t_label == 'Smear':
brush_tool_names.append((t_label, 'Smear Weight'))
continue
brush_tool_names.append((t_label, t_label))
elif mode == 'PAINT_VERTEX':
if t_label == 'Smear':
brush_tool_names.append((t_label, 'Smear Vertex'))
continue
brush_tool_names.append((t_label, t_label))
elif mode == 'VERTEX_GPENCIL':
brush_tool_names.append((t_label, 'Vertex ' + t_label))
else:
brush_tool_names.append((t_label, t_label))
return brush_tool_names
def update_pref_def_brush(self, context, mode=''):
if context.mode not in self.modes.in_modes:
return None
if not mode:
mode = self.pref_tabs
# props = context.window_manager.brush_manager_props
default_brushes = get_default_brushes_list(mode=mode)
pref_def_brushes = get_pref_default_brush_props(mode=mode)
icons_path = get_icons_path(mode)
if not self.modes.Modes[mode].get('has_themes'):
icons_path = os.path.join(icons_path, 'custom_icons')
for brush in default_brushes:
if pref_def_brushes.get(brush):
if not SET_DEFAULT_ICONS.get(mode):
continue
set_custom_icon(context, icons_path, brush)
continue
try:
bpy.data.brushes[brush].use_custom_icon = False
# bpy.data.brushes[brush].icon_filepath = ''
except KeyError:
pass
update_brush_list(self, context)
def update_pref_def_s_brush(self, context):
update_pref_def_brush(self, context, mode='SCULPT')
def update_pref_def_wp_brush(self, context):
update_pref_def_brush(self, context, mode='PAINT_WEIGHT')
def update_pref_def_vp_brush(self, context):
update_pref_def_brush(self, context, mode='PAINT_VERTEX')
def update_pref_def_gv_brush(self, context):
update_pref_def_brush(self, context, mode='VERTEX_GPENCIL')
class BM_Modes:
in_modes = [
'SCULPT',
'PAINT_TEXTURE',
'PAINT_WEIGHT',
'PAINT_VERTEX',
'PAINT_GPENCIL',
'VERTEX_GPENCIL',
]
def __init__(self, context_mode=''):
if MODE:
self.mode = MODE
else:
self.mode = 'SCULPT'
if UI_MODE:
self.mode = 'PAINT_TEXTURE'
if context_mode != '':
self.mode = context_mode
self.mode_prefixes = {
'SCULPT': 's',
'PAINT_TEXTURE': 'ip',
'PAINT_WEIGHT': 'wp',
'PAINT_VERTEX': 'vp',
'PAINT_GPENCIL': 'gp',
'VERTEX_GPENCIL': 'gv'
}
self.Modes = dict(
SCULPT={
'tool_settings': 'sculpt', # context.tool_settings
'brush_tool': 'sculpt_tool',
'brush_use_mode': 'use_paint_sculpt',
'fav_settings': 'bm_favorite_list_settings',
'fav_store': 'bm_sculpt_fav_list_store',
'icons_folder': 'icon_themes',
'has_themes': True,
'def_brushes_tool_list': BRUSHES_SCULPT,
'other_tools_list': TOOLS_SCULPT,
'def_brush_names': BRUSHES_SCULPT_NAMES,
'is_split_tools': False,
'use_custom_icons': True,
'default_custom_icons': 'default_brushes_custom_icon',
},
PAINT_TEXTURE={
'tool_settings': 'image_paint',
'brush_tool': 'image_tool',
'brush_use_mode': 'use_paint_image',
'fav_settings': 'bm_paint_favorite_settings',
'fav_store': 'bm_paint_fav_list_store',
'icons_folder': 'paint_icons',
'has_themes': False,
'def_brushes_tool_list': BRUSHES_IPAINT,
'other_tools_list': TOOLS_IPAINT,
'def_brush_names': BRUSHES_IPAINT_NAMES,
'is_split_tools': False,
'use_custom_icons': False,
'default_custom_icons': False,
},
PAINT_GPENCIL={
'tool_settings': 'gpencil_paint',
'brush_tool': 'gpencil_tool',
'brush_use_mode': 'use_paint_grease_pencil',
'fav_settings': 'bm_gpaint_favorite_settings',
'fav_store': 'bm_gpaint_fav_list_store',
'icons_folder': 'gpaint_icons',
'has_themes': False,
'def_brushes_tool_list': BRUSHES_GPAINT,
'other_tools_list': TOOLS_GPAINT,
'def_brush_names': BRUSHES_GPAINT_NAMES,
'is_split_tools': True, # if brush tool has more default brushes than one
'use_custom_icons': False,
'default_custom_icons': False,
},
PAINT_WEIGHT={
'tool_settings': 'weight_paint',
'brush_tool': 'weight_tool',
'brush_use_mode': 'use_paint_weight',
'fav_settings': 'bm_wpaint_favorite_settings',
'fav_store': 'bm_wpaint_fav_list_store',
'icons_folder': 'wpaint_icons',
'has_themes': False,
'def_brushes_tool_list': BRUSHES_WPAINT,
'other_tools_list': TOOLS_WPAINT,
'def_brush_names': BRUSHES_WPAINT_NAMES,
'is_split_tools': True,
'use_custom_icons': True,
'default_custom_icons': 'default_wp_brushes_custom_icon',
},
PAINT_VERTEX={
'tool_settings': 'vertex_paint',
'brush_tool': 'vertex_tool',
'brush_use_mode': 'use_paint_vertex',
'fav_settings': 'bm_vpaint_favorite_settings',
'fav_store': 'bm_vpaint_fav_list_store',
'icons_folder': 'vpaint_icons',
'has_themes': False,
'def_brushes_tool_list': BRUSHES_VPAINT,
'other_tools_list': TOOLS_VPAINT,
'def_brush_names': BRUSHES_VPAINT_NAMES,
'is_split_tools': True,
'use_custom_icons': True,
'default_custom_icons': 'default_vp_brushes_custom_icon',
},
VERTEX_GPENCIL={
'tool_settings': 'gpencil_vertex_paint',
'brush_tool': 'gpencil_vertex_tool',
'brush_use_mode': 'use_vertex_grease_pencil',
'fav_settings': 'bm_gvertex_favorite_settings',
'fav_store': 'bm_gvertex_fav_list_store',
'icons_folder': 'vpaint_icons',
'has_themes': False,
'def_brushes_tool_list': BRUSHES_GVERTEX,
'other_tools_list': TOOLS_GVERTEX,
'def_brush_names': BRUSHES_GVERTEX_NAMES,
'is_split_tools': False,
'use_custom_icons': True,
'default_custom_icons': 'default_gv_brushes_custom_icon',
},
)
for im in self.in_modes:
m = self.mode_prefixes.get(im)
similar_props = {
'pref_brush': 'default_' + m + '_brush_',
'pref_tool': m + '_tool_brush_',
'pref_other_tool': m + '_tool_',
'use_startup_favorites': 'use_' + m + '_startup_favorites',
'path_to_startup_favorites': 'path_to_' + m + '_startup_favorites',
'brush_library': m + '_brush_library',
'wide_popup_layout': 'wide_' + m + '_popup_layout',
'wide_popup_layout_size': 'wide_' + m + '_popup_layout_size',
'popup_max_tool_columns': 'popup_' + m + '_max_tool_columns',
'popup_width': 'popup_' + m + '_width',
'preview_frame_scale': 'preview_' + m + '_frame_scale',
'popup_items_scale': 'popup_' + m + '_items_scale',
'show_def_brushes_in_categories': 'show_' + m + '_def_brushes_in_categories',
}
self.Modes[im].update(similar_props)
def show_def_brushes_in_categories(self):
prefs = bpy.context.preferences.addons[Addon_Name].preferences
return eval('prefs.' + self.Modes[self.mode].get('show_def_brushes_in_categories'))
def popup_items_scale(self):
prefs = bpy.context.preferences.addons[Addon_Name].preferences
return eval('prefs.' + self.Modes[self.mode].get('popup_items_scale'))
def preview_frame_scale(self):
prefs = bpy.context.preferences.addons[Addon_Name].preferences
return eval('prefs.' + self.Modes[self.mode].get('preview_frame_scale'))
def popup_width(self):
prefs = bpy.context.preferences.addons[Addon_Name].preferences
return eval('prefs.' + self.Modes[self.mode].get('popup_width'))
def popup_max_tool_columns(self):
prefs = bpy.context.preferences.addons[Addon_Name].preferences
return eval('prefs.' + self.Modes[self.mode].get('popup_max_tool_columns'))
def wide_popup_layout_size(self):
prefs = bpy.context.preferences.addons[Addon_Name].preferences
return eval('prefs.' + self.Modes[self.mode].get('wide_popup_layout_size'))
def wide_popup_layout(self):
prefs = bpy.context.preferences.addons[Addon_Name].preferences
return eval('prefs.' + self.Modes[self.mode].get('wide_popup_layout'))
def tool_settings(self, context):
ts = context.tool_settings
return eval('ts.' + self.Modes[self.mode].get('tool_settings'))
def brush_tool(self, brush):
return eval('brush.' + self.Modes[self.mode].get('brush_tool'))
def brush_use_mode(self, brush):
return eval('brush.' + self.Modes[self.mode].get('brush_use_mode'))
def def_brushes_tool_list(self):
return self.Modes[self.mode].get('def_brushes_tool_list')
def def_brushes_list(self):
if not self.mode:
return None
if self.Modes[self.mode].get('is_split_tools'):
return self.Modes[self.mode].get('def_brush_names')
return [b for t, b in evaluate_brush_tools(self.def_brushes_tool_list(), self.mode)]
def pref_brush(self):
return self.Modes[self.mode].get('pref_brush')
def pref_tool(self, t_type='brush'):
if t_type == 'brush':
return self.Modes[self.mode].get('pref_tool')
elif t_type == 'other':
return self.Modes[self.mode].get('pref_other_tool')
def brush_tool_enum_items(self):
enum_items = []
for brush in bpy.data.brushes:
if self.brush_use_mode(brush):
enum_items = brush.bl_rna.properties[
self.Modes[self.mode].get('brush_tool')].enum_items
break
return enum_items
def use_startup_favorites(self):
prefs = bpy.context.preferences.addons[Addon_Name].preferences
return eval('prefs.' + self.Modes[self.mode].get('use_startup_favorites'))
def path_to_startup_favorites(self):
prefs = bpy.context.preferences.addons[Addon_Name].preferences
return eval('prefs.' + self.Modes[self.mode].get('path_to_startup_favorites'))
def icons_path(self):
prefs = bpy.context.preferences.addons[Addon_Name].preferences
folder = self.Modes[self.mode].get('icons_folder')
icons_path = get_icon_themes_path(folder)
if self.Modes[self.mode].get('has_themes'):
icons_path = os.path.join(icons_path, prefs.brush_icon_theme)
return icons_path
def fav_settings(self):
if not self.mode:
return None
scene = bpy.context.scene
return eval('scene.' + self.Modes[self.mode].get('fav_settings'))
def fav_store(self):
if not self.mode:
return None
wm = bpy.context.window_manager
return eval('wm.' + self.Modes[self.mode].get('fav_store'))
def library_path(self):
prefs = bpy.context.preferences.addons[Addon_Name].preferences
return eval('prefs.' + self.Modes[self.mode].get('brush_library'))
def text_lookup(find_string, source_text):
if source_text.find(find_string) != -1:
return True
else:
return False
def get_b_files(directory):
if directory and os.path.exists(directory):
b_files = []
for fn in os.listdir(directory):
if fn.lower().endswith(".blend"):
b_files.append(fn)
return b_files
def get_brushes_in_files(directory, b_files):
brushes = []
for name in b_files:
filepath = os.path.join(directory, name)
with bpy.data.libraries.load(filepath) as (data_from, data_to):
for brush in data_from.brushes:
brushes.append(brush)
return brushes
def get_library_directory(context):
props = context.window_manager.brush_manager_props
prefs = bpy.context.preferences.addons[Addon_Name].preferences
modes = BM_Modes()
lib_path = modes.library_path()
directory = os.path.join(lib_path, props.lib_categories)
return directory
def get_active_brush(context):
modes = BM_Modes()
ts = modes.tool_settings(context)
return ts.brush
def check_brush_type(brush, mode=''):
if mode == '':
mode = bpy.context.mode
modes = BM_Modes(mode)
return modes.brush_use_mode(brush)
def get_append_brushes(directory, b_files, default_brushes=False):
brushes_append = []
brushes_in_files = get_brushes_in_files(directory, b_files)
def_brushes = get_default_brushes_list()
for brush in brushes_in_files:
if brush in def_brushes and not default_brushes:
continue
try:
if not check_brush_type(bpy.data.brushes[brush], MODE):
continue
except KeyError:
continue
brushes_append.append(brush)
brushes_append = list(set(brushes_append))
brushes_append.sort()
return brushes_append
def get_copy_number(name):
name_digits = []
brushes = get_current_file_brushes(MODE)
check_name = '.'.join(name.split('.')[0:-1])
if check_name == '':
check_name = name
for b in brushes:
if b.startswith(check_name) and\
len(check_name) == len('.'.join(b.split('.')[0:-1])):
if b.split('.')[-1].isdigit():
name_digits.append(b)
name_digits.sort()
if name_digits:
return name_digits[-1]
return False
def auto_rename(name):
digits = '001'
copy_number = get_copy_number(name)
if copy_number:
name = copy_number
if name.split(".")[-1].isdigit():
zeroes = ''
for i in range(len(name.split('.')[-1])):
if int(name.split('.')[-1][i]) > 0:
digits = zeroes + str(int(name.split('.')[-1]) + 1)
break
else:
zeroes += '0'
return '.'.join(name.split('.')[0:-1]) + '.' + digits
else:
return name + '.' + digits
def append_brushes_from_a_file(filepath, default_brushes=False, duplicates='SKIP'):
brushes = []
brushes_to_rename = []
duplicates_list = []
def_brushes = get_default_brushes_list(mode=MODE)
with bpy.data.libraries.load(filepath) as (data_from, data_to):
for brush in data_from.brushes:
if brush in def_brushes and not default_brushes:
continue
if brush not in bpy.data.brushes:
data_to.brushes.append(brush)
brushes.append(brush)
continue
elif duplicates == 'OVERWRITE':
bpy.data.brushes.remove(bpy.data.brushes[brush], do_unlink=True)
data_to.brushes.append(brush)
elif duplicates == 'RENAME':
b = bpy.data.brushes[brush]
b.name = b.name + " {ORIGINAL}"
data_to.brushes.append(brush)
brushes_to_rename.append(brush)
continue
# !! Append even if the same brush is already exists
brushes.append(brush)
for br in brushes_to_rename:
name = auto_rename(br)
bpy.data.brushes[br].name = name
brushes.append(name)
bpy.data.brushes[br + " {ORIGINAL}"].name = br
return brushes
def append_brushes_to_current_file(directory):
brushes_in_files = []
b_files = get_b_files(directory)
modes = BM_Modes()
for name in b_files:
filepath = os.path.join(directory, name)
if modes.show_def_brushes_in_categories():
brushes_in_files += append_brushes_from_a_file(filepath, default_brushes=True)
else:
brushes_in_files += append_brushes_from_a_file(filepath)
return brushes_in_files
def set_first_preview_item(context, brushes_list, wm_enum_prop='main'):
wm = context.window_manager
props = wm.brush_manager_props
props.skip_brush_set = True
try:
if wm_enum_prop == 'main':
# wm.brushes_in_files = brushes_list[0]
wm['brushes_in_files'] = 0
if wm_enum_prop == 'fav':
wm.brushes_in_favorites = brushes_list[0]
except (TypeError, IndexError) as e:
pass
props.skip_brush_set = False
UPDATE_ICONS = False
def update_category(self, context):
wm = bpy.context.window_manager
props = wm.brush_manager_props
if props.lib_categories == 'Default' and\
context.mode == 'SCULPT' and not UI_MODE:
create_default_sculpt_tools()
set_ui_mode(context)
update_brush_list(self, context)
update_fav_list(self, context)
if context.mode == 'SCULPT' and not UI_MODE:
set_toggle_default_icons(context)
def update_category_first_preview_item(self, context):
set_first_preview_item(context, [])
def update_brush_list(self, context):
if context.mode not in BM_Modes.in_modes:
return None
global _directory
prefs = context.preferences.addons[Addon_Name].preferences
create_default_smear_tools()
set_first_preview_item(context, [])
if prefs.use_3dn_bip_previews:
_directory = None
else:
b_preview_coll = preview_brushes_coll["main"]
b_preview_coll.my_previews_dir = ""
def update_fav_list(self, context):
if context.mode not in BM_Modes.in_modes:
return None
global _fav_list
prefs = context.preferences.addons[Addon_Name].preferences
fav_brushes = get_favorite_brushes()
set_first_preview_item(context, fav_brushes, wm_enum_prop='fav')
if prefs.use_3dn_bip_previews:
_fav_list = None
else:
b_preview_coll = preview_brushes_coll["favorites"]
b_preview_coll.my_previews_dir = ""
def lib_category_folders(self, context):
prefs = context.preferences.addons[Addon_Name].preferences
modes = BM_Modes()
lib_path = modes.library_path()
default_list = ['Default', 'Current File']
folders = get_folders_contains_files(lib_path, ".blend")
folders_list = default_list + folders
return [(name, name, "") for name in folders_list]
class WM_OT_Set_Category(Operator):
bl_label = 'BM Set Category'
bl_idname = 'bm.set_brushes_category'
bl_description = "Select the category for the brushes preview"
bl_options = {'UNDO'}
lib_category: EnumProperty(
name='Category',
items=lib_category_folders,
description='The library category that contain the list of brushes existing in the blender file data'
)
def execute(self, context):
wm = bpy.context.window_manager
props = wm.brush_manager_props
props.lib_categories = self.lib_category
return {'FINISHED'}
def filter_brushes_type(brushes_list, mode=''):
filter_brushes = []
for b in brushes_list:
try:
if not check_brush_type(bpy.data.brushes[b], mode):
continue
except KeyError:
continue
filter_brushes.append(b)
filter_brushes = list(set(filter_brushes))
filter_brushes.sort()
return filter_brushes
def get_appended_to_current_brushes(category, directory):
brushes_added = append_brushes_to_current_file(directory)
brushes = filter_brushes_type(brushes_added, MODE)
if len(brushes) == 0:
b_files = get_b_files(directory)
brushes = get_append_brushes(directory, b_files)
return brushes
def get_default_brushes_list(list_type='brushes', mode=''):
if mode != '':
modes = BM_Modes(mode)
else:
modes = BM_Modes()
if list_type == 'brushes':
brushes = modes.def_brushes_list()
return brushes
if list_type == 'init_tools' or list_type == 'tools':
enum_items = modes.brush_tool_enum_items()
if list_type == 'sculpt_tools':
modes.mode = 'SCULPT'
enum_items = modes.brush_tool_enum_items()
if list_type == 'init_tools':
init_tools = dict([(b.identifier, b.name) for b in enum_items])
return init_tools
if list_type == 'tools' or list_type == 'sculpt_tools':
tools = [t.identifier for t in enum_items]
tools.sort()
return tools
def check_vertex_paint_brushes():
try:
check = bpy.context.preferences.experimental.use_sculpt_vertex_colors
except AttributeError:
check = False
if get_app_version() >= 2.90 and check:
return True
return False
def get_current_file_brushes(mode=''):
brushes = []
try:
for brush in bpy.data.brushes:
try:
if not check_brush_type(brush, mode):
continue
if brush.name == 'Paint':
if bpy.app.version < (3, 2, 0) and\
not check_vertex_paint_brushes():
continue
brushes.append(brush.name)
except AttributeError:
continue
except AttributeError:
pass
brushes.sort()
return brushes
def get_brushes_from_preview_enums(enum_items, list_type='brushes'):
brushes_list = []
icons = []
for name1, name2, blank, iconid, index in enum_items:
brushes_list.append(name1)
icons.append(iconid)
if list_type == 'icons':
return icons
return brushes_list
def get_main_list_brushes(context, list_type='brushes'):
prefs = bpy.context.preferences.addons[Addon_Name].preferences
b_preview_coll = get_preview_brushes_collection()
if not prefs.use_3dn_bip_previews:
directory = get_library_directory(context)
b_preview_coll.my_previews_dir = directory
main_preview_enums = b_preview_coll.my_previews
if list_type == 'full':
return main_preview_enums
return get_brushes_from_preview_enums(main_preview_enums, list_type)
global _enum_items
if list_type == 'icons':
return [icon_id for name, n, e, icon_id, i in _enum_items]
return [name for name, n, e, icon_id, i in _enum_items]
def get_favorite_brushes(list_type='brushes'):
prefs = bpy.context.preferences.addons[Addon_Name].preferences
b_preview_coll = get_preview_brushes_collection(coll_type='favorites')
if not prefs.use_3dn_bip_previews:
preview_enums = b_preview_coll.my_previews
return get_brushes_from_preview_enums(preview_enums, list_type)
global _enum_items_fav
if list_type == 'icons':
return [icon_id for name, n, e, icon_id, i in _enum_items_fav]
return [name for name, n, e, icon_id, i in _enum_items_fav]
def add_to_fav_active_current_brush(context, brushes_list):
active_brush = get_active_brush(context)
if not active_brush or active_brush.name in brushes_list:
return brushes_list
brushes_list.append(active_brush.name)
brushes_list.sort()
return brushes_list
def clear_favorites_list():
b_preview_coll = get_preview_brushes_collection(coll_type='favorites')
clear_preview_collection_list(b_preview_coll)
_enum_items_fav.clear()
def clear_Default_list():
props = bpy.context.window_manager.brush_manager_props
b_preview_coll = get_preview_brushes_collection()
if props.lib_categories != "Default":
return None
clear_preview_collection_list(b_preview_coll, coll_list='default')
_enum_items.clear()
def get_app_version():
version = str(bpy.app.version[0]) + '.' + str(bpy.app.version[1])
return float(version)
def save_brushes_to_file(brushes_data, filepath, relative_path_remap=False):
data_blocks = {
*brushes_data,
# *bpy.data.textures,
*bpy.data.node_groups
}
if get_app_version() < 2.90:
bpy.data.libraries.write(
filepath, data_blocks, fake_user=True, relative_remap=relative_path_remap
)
return None
path_remap = 'NONE'
if relative_path_remap:
path_remap = 'RELATIVE_ALL'
bpy.data.libraries.write(
filepath, data_blocks, fake_user=True, path_remap=path_remap)
return None
def get_folders_contains_files(root_folder_path, file_extension=".blend"):
folders_list = []
if not root_folder_path or not os.path.isdir(root_folder_path):
return folders_list
for folder in os.listdir(root_folder_path):
if not os.path.isdir(os.path.join(root_folder_path, folder)):
continue
for fn in os.listdir(os.path.join(root_folder_path, folder)):
if fn.lower().endswith(file_extension):
# folder = folder.encode("utf-8").decode("utf-8")
folders_list.append(folder)
break
return folders_list
def filter_brushes_by_name(brushes_list, name):
props = bpy.context.window_manager.brush_manager_props
filtered_brushes_list = []
for brush in brushes_list:
if not props.search_case_sensitive:
if not text_lookup(name.lower(), brush.lower()):
continue
else:
if not text_lookup(name, brush):
continue
filtered_brushes_list.append(brush)
filtered_brushes_list.sort()
return filtered_brushes_list
def get_icon_themes_path(folder_name='icon_themes'):
current_file_dir = os.path.dirname(__file__)
icon_themes_path = os.path.join(current_file_dir, folder_name)
return icon_themes_path
def get_icons_path(mode=''):
if mode == '':
mode = MODE
modes = BM_Modes(mode)
return modes.icons_path()
def set_brush_icon_themes(self, context):
current_file_dir = os.path.dirname(__file__)
icon_themes_path = get_icon_themes_path()
default_list = []
folders = get_folders_contains_files(icon_themes_path, ".png")
folders_list = folders + default_list
folders_list.sort()
return [(name, name, "") for name in folders_list]
def set_active_tool(tool_name):
if UI_MODE:
area_type = 'IMAGE_EDITOR'
else:
area_type = 'VIEW_3D'
for area in bpy.context.screen.areas:
if area.type == area_type:
override = bpy.context.copy()
override["space_data"] = area.spaces[0]
override["area"] = area
if bpy.app.version < (4, 0, 0):
bpy.ops.wm.tool_set_by_id(override, name=tool_name)
continue
region = area.regions[-1]
with bpy.context.temp_override(area=area, region=region):
bpy.ops.wm.tool_set_by_id('INVOKE_DEFAULT', name=tool_name)
def get_icon_name(context, brush_name):
brush = bpy.data.brushes[brush_name]
modes = BM_Modes()
mode = MODE
if not MODE:
mode = context.mode
if modes.Modes[mode].get('is_split_tools'):
return brush.name.lower() + '.png'
else:
return modes.brush_tool(brush).lower() + '.png'
def create_thumbnail_icon(context, brush_name, b_preview_coll):
icons_path = get_icons_path()
icon_name = get_icon_name(context, brush_name)
filepath = os.path.join(icons_path, icon_name)
if not os.path.isfile(filepath):
modes = BM_Modes()
if modes.Modes[MODE].get('is_split_tools'):
for b in modes.Modes[MODE].get('def_brush_names'):
if text_lookup(b.split(' ')[0], bpy.data.brushes[brush_name].name):
icon_name = bpy.data.brushes[b].name.lower() + '.png'
break
if not os.path.isfile(os.path.join(icons_path, icon_name)):
icon_name = modes.brush_tool(bpy.data.brushes[brush_name]).lower() + '.png'
if not os.path.isfile(os.path.join(icons_path, icon_name)):
icon_name = 'NA_brush.png'
filepath = os.path.join(icons_path, icon_name)
icon = load_preview_icon(context, brush_name, filepath, b_preview_coll)
return icon.icon_id
def load_preview_icon(context, brush_name, filepath, b_preview_coll):
prefs = context.preferences.addons[Addon_Name].preferences
if prefs.use_3dn_bip_previews:
return b_preview_coll.load_safe(MODE + '_' + brush_name, filepath, 'IMAGE')
else:
return b_preview_coll.load(MODE + '_' + brush_name, filepath, 'IMAGE')
def create_enum_list(context, brushes, b_preview_coll, update_icon=False):
props = bpy.context.window_manager.brush_manager_props
global UPDATE_ICONS
if UPDATE_ICONS or update_icon:
update_icon = True
b_preview_coll.clear()
icons_path = get_icons_path()
enum_items = []
default_brushes = get_sorted_default_brushes(MODE)
for index, brush in enumerate(brushes):
try:
check = bpy.data.brushes[brush]
except KeyError:
continue
if update_icon:
icon = False
else:
if UI_MODE:
icon = b_preview_coll.get('PAINT_TEXTURE' + '_' + brush)
else:
icon = b_preview_coll.get(context.mode + '_' + brush)
if not icon:
is_default = brush in default_brushes and props.set_default_brushes_custom_icon
if bpy.data.brushes[brush].use_custom_icon and not is_default:
filepath = bpy.path.abspath(bpy.data.brushes[brush].icon_filepath)
if os.path.isfile(bpy.path.abspath(filepath)):
# thumb = bpy.data.brushes[brush].preview.icon_id
icon = load_preview_icon(context, brush, filepath, b_preview_coll)
thumb = icon.icon_id
else:
thumb = create_thumbnail_icon(context, brush, b_preview_coll)
else:
thumb = create_thumbnail_icon(context, brush, b_preview_coll)
else:
thumb = icon.icon_id
enum_items.append((brush, brush, "", thumb, index))
return enum_items
def reset_all_default_brushes(context):
if context.mode != 'SCULPT':
return None
props = context.window_manager.brush_manager_props
def_brushes = get_sorted_default_brushes()
active_brush = get_active_brush(context)
for brush in def_brushes:
try:
set_brush_tool(None, context, bpy.data.brushes[brush])
except KeyError:
pass
bpy.ops.brush.reset()
if props.set_default_brushes_custom_icon:
bpy.data.brushes[brush].use_custom_icon = True
set_brush_tool(None, context, active_brush)