forked from HummyR/LEOAlphaPaint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLEOAlphaPaint.py
1531 lines (1261 loc) · 57.3 KB
/
LEOAlphaPaint.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
# Foundational script by LeoMods (https://github.com/leotorrez) and Blue (https://github.com/SharpCyan)
# Edits by HummyR (https://github.com/HummyR) (tool developer at https://discord.gg/agmg)
# Credits to Gradient tool by andyp123 (https://github.com/andyp123/blender_vertex_color_master), Bartosz Styperek, and RylauChelmi, adapted and updated for alpha paint
# Primarily/intended to be used to help make Anime Game mods at https://discord.gg/agmg
# R = Ambient Occlusion (Higher = no occlude, Lower = Ambient Occlusion)
# G = Shadow Smoothing (Higher = Sharper, Lower = Smoother)
# B = Outline z/depth-index (Higher = behind, Lower = in front)
# A = Outline thickness (Higher = thicker, Lower = less width)
# Standard values:
# R = 1 , lower this to give usually shaded areas ambient occlusion
# G = 0.502, soft clothing tend to have smoother values between 0.251 - 0.2
# B = 0.502, (raise this value to push outlines back in the Z/depth-axis relative to the model, but usually no need to change)
# A = 0.502, generally: exposed hands and feet are 0.4, concave edges have low values, areas with a lot of small details have 0.302-0.106 (for example, a small spike whose outline gets thinner towards the tip), eyes are 0
import bpy
import bmesh
from mathutils import Color, Vector, Matrix
import gpu
from gpu_extras.batch import batch_for_shader
from bpy_extras import view3d_utils
from math import fmod
bl_info = {
"name": "LEOAlphaPaint",
"blender": (3, 5, 1),
"author": "LeoMods (https://github.com/leotorrez), Blue, and HummyR (https://github.com/HummyR)",
"location": "Vertex Paint",
"description": "Paint Vertex colors for Anime Game mods at https://discord.gg/agmg. Credits to Gradient tool by andyp123 (https://github.com/andyp123/blender_vertex_color_master), Bartosz Styperek, and RylauChelmi, adapted and updated for alpha paint. Tested to work on blender version 2.83 and above",
"category": "Paint",
"tracker_url": "https://github.com/HummyR",
}
FL = [0,1,2]
channel_list = ['R','G','B','A']
keyName = "_viewLayer_generated_"
blending_modes = [
('', 'Normal', ''), #---------------------------------------------
('MIX', "Mix", "S"),
('PAINTMIX', "Paint Mix", "sqrt(S^2+D^2)"),
('ALPHAOVER','Alpha Over', 'S*(sA): basically Mix with source alpha layer factor'),
('','Light',''), #------------------------------------------------
('ADD', "Add", "S+D"),
('LIGHTEN', "Lighten","max(S,D)"),
('COLORDODGE','Color Dodge','D/(1-S)'),
('SCREEN','Screen','1-(1-S)*(1-D)'),
('','Dark',''), #-------------------------------------------------
('DARKEN', "Darken", "min(S,D)"),
('MUL', "Multiply", "S*D"),
('LINEARBURN', 'Linear Burn','S+D-1'),
('COLORBURN', 'Color Burn','1-(1-D)/S'),
('','Cancel',''), #-----------------------------------------------
('SUB', "Subtract", "D-S"),
('DIV', "Divide", "D/S"),
('','Contrast',''), #---------------------------------------------
('OVERLAY','Overlay','Multiply(dark) + Screen(light), depending on Source Layer value'),
('HARDLIGHT','Hard Light','Multiply(dark) + Screen(light), depending on Active Layer value'),
('SOFTLIGHT','Soft Light','(1-2D)S^2+2D*S'),
('','Component',''), #-------------------------------------------
('HUE', "Hue", "S.h"),
('SATURATION', "Saturation", "S.s"),
('COLOR', "Color", "S.sh"),
('VALUE', "Value", "S.v"),
]
class PaintAlphaPropertyGroup(bpy.types.PropertyGroup):
one_layer_isolate : bpy.props.BoolProperty(
name='Isolate one layer',
description='Only isolate one layer at a time. \nRight click to assign shortcut',
default=True
)
enable_transfer_tools : bpy.props.BoolProperty(
name="Enable Transfer Tools",
default=False,
description="Show/hide tools for transfering/blending data between vertex color layers/channels. \nRight click to assign shortcut"
)
enable_indiscriminate_fill : bpy.props.BoolProperty(
name="allow use ALL vertices",
description="WARNING: this will allow you to paint all verticies at the click of one button. \nRight click to assign shortcut. Can be undone with Control + Z",
default=True
)
isolated_Channel : bpy.props.StringProperty(
name="isolated_Channel",
default=""
)
blend_mode : bpy.props.EnumProperty(
name='Blend type',
items=blending_modes,
default="MIX",
)
def vcol_layer_items(self, context):
obj = context.active_object
mesh = obj.data
try: vcollayers = mesh.vertex_colors
except: vcollayers = mesh.attributes.color
items = [] if vcollayers is None else [(vcol.name, vcol.name, "") for vcol in vcollayers]
return items
def vcol_layer_items_factor(self, context):
obj = context.active_object
mesh = obj.data
try: vcollayers = mesh.vertex_colors
except: vcollayers = mesh.attributes.color
items = [('NONE','None','')]
if vcollayers is not None:
items.extend([(vcol.name, vcol.name, "") for vcol in vcollayers])
return items
src_vcol: bpy.props.EnumProperty(
name="Source",
items=vcol_layer_items,
description="Source vertex color layer",
)
factor_vcol: bpy.props.EnumProperty(
name="Factor",
description="Factor color channel. 0-1 RGBA channels corresponds to how much the blend mode will be applied",
items=vcol_layer_items_factor,
)
factor_slider : bpy.props.FloatProperty(
name="Mix",
description="Factor amount",
default=1,
max=1,
min=0,
)
src_ch: bpy.props.BoolVectorProperty(
name="Source Channel",
description="Source color channel. Click and drag to enable/disable multiple simultaneously",
size=4,
default=(False,False,False,False),
)
past_shading : bpy.props.StringProperty(default = 'UnInitialized')
space_shader_storage : bpy.props.StringProperty(default = 'VERTEX')
select_color_mode : bpy.props.EnumProperty(
name='Select mode',
items=[
('BRUSH', 'Brush', 'Primary brush color'),
('PALETTE', 'Palette', 'All colors in active palette')
],
default='BRUSH'
)
def refreshMesh(bm, obj):
bm.to_mesh(obj)
bm.free()
obj.update()
def findActiveColorLayer(color_data, obj):
try: color_layer = color_data[obj.vertex_colors.active.name]
except:
try: color_layer = color_data[obj.attributes.active_color.name]
except: color_layer = color_data.active
return color_layer
def trySetActiveVC(obj, basename):
try: obj.vertex_colors.active = obj.vertex_colors[basename]
except:
try: obj.attributes.active_color = obj.attributes[basename]
except: pass
def clamp01(inp):
return max(0, min(inp, 1))
def blendChannels(self,context, settings, obj, bm, color_data):
blend_mode = self.blend_mode
factor = self.factor_vcol
factor_slider = self.factor_slider
dstname = self.dst_vcol
dst = color_data[dstname]
src = color_data[self.src_vcol]
src_ch = [i for i, x in enumerate(self.src_ch) if x]
if not src_ch: src_ch = [0,1,2]
settings.isolated_Channel = dstname.split(keyName)[1] if keyName in dstname else ""
isolated_channels = [int(x) for x in settings.isolated_Channel if x]
alpha_mode = bool(3 in isolated_channels)
if not isolated_channels or alpha_mode: isolated_channels = [0,1,2]
if blend_mode == 'ALPHAOVER':
src_ch = [x for x in src_ch if x!=3]
isolated_channels = [x for x in isolated_channels if x!=3]
src_count = len(src_ch)
iso_count = len(isolated_channels)
referenceColor = color_data.new()
referenceColor.copy_from(src)
# MIX [1,2] COLORD COLORS COLORF 1.0
if src_count == 1 or iso_count==1 or alpha_mode:
for vertex in bm.verts:
for loop in vertex.link_loops:
for x in isolated_channels:
loop[referenceColor][x] = 0
for c in src_ch:
loop[referenceColor][x] += loop[src][c]/src_count
elif set(src_ch) == set(isolated_channels):
for vertex in bm.verts:
for loop in vertex.link_loops:
for c in src_ch:
loop[referenceColor][c] = loop[src][c]
else:
self.report({'ERROR'},'Plugin does not support multi-to-multi-different-channel transfer')
return {'FINISHED'}
if blend_mode == 'MIX':
pass
elif blend_mode == 'ALPHAOVER':
for vertex in bm.verts:
for loop in vertex.link_loops:
for c in isolated_channels:
a = loop[referenceColor][3]
loop[referenceColor][c] = loop[referenceColor][c]*a + (1-a)*loop[dst][c]
elif blend_mode == 'PAINTMIX':
for vertex in bm.verts:
for loop in vertex.link_loops:
dstCol = Color(loop[dst][:3])
refCol = Color(loop[referenceColor][:3])
referenceColorConv = Color([
clamp01(clamp01(dstCol[0]*(1-.25*refCol[2])*(1-.25*refCol[1]))+\
clamp01(refCol[0]*(1-.25*dstCol[2])*(1-.25*dstCol[1]))),\
clamp01(clamp01(dstCol[1]*(1-.7*refCol[0]))+\
clamp01(refCol[1]*(1-.3*dstCol[0]))),\
clamp01(clamp01(dstCol[2]*(1-.3*refCol[1]))+\
clamp01(refCol[2]*(1-.7*dstCol[1])))
])
loop[referenceColor][:3] = referenceColorConv[:3]
elif blend_mode == 'ADD':
for vertex in bm.verts:
for loop in vertex.link_loops:
for c in isolated_channels:
loop[referenceColor][c] = clamp01(loop[dst][c]+loop[referenceColor][c])
elif blend_mode == 'LIGHTEN':
for vertex in bm.verts:
for loop in vertex.link_loops:
for c in isolated_channels:
loop[referenceColor][c] = max(loop[dst][c],loop[referenceColor][c])
elif blend_mode == 'COLORDODGE':
for vertex in bm.verts:
for loop in vertex.link_loops:
for c in isolated_channels:
loop[referenceColor][c] = 1 if 1-loop[referenceColor][c]==0 else clamp01(loop[dst][c]/(1-loop[referenceColor][c]))
elif blend_mode == 'SCREEN':
for vertex in bm.verts:
for loop in vertex.link_loops:
for c in isolated_channels:
loop[referenceColor][c] = clamp01(1-(1-loop[referenceColor][c])*(1-loop[dst][c]))
elif blend_mode == 'DARKEN':
for vertex in bm.verts:
for loop in vertex.link_loops:
for c in isolated_channels:
loop[referenceColor][c] = min(loop[referenceColor][c],loop[dst][c])
elif blend_mode == 'MUL':
for vertex in bm.verts:
for loop in vertex.link_loops:
for c in isolated_channels:
loop[referenceColor][c] *= loop[dst][c]
elif blend_mode == 'LINEARBURN':
for vertex in bm.verts:
for loop in vertex.link_loops:
for c in isolated_channels:
loop[referenceColor][c] = clamp01(loop[referenceColor][c]+loop[dst][c]-1)
elif blend_mode == 'COLORBURN':
for vertex in bm.verts:
for loop in vertex.link_loops:
for c in isolated_channels:
loop[referenceColor][c] = 0 if loop[referenceColor][c]==0 else clamp01(1-(1-loop[dst][c])/loop[referenceColor][c])
elif blend_mode == 'SUB':
for vertex in bm.verts:
for loop in vertex.link_loops:
for c in isolated_channels:
loop[referenceColor][c] = clamp01(loop[dst][c]-loop[referenceColor][c])
elif blend_mode == 'DIV':
for vertex in bm.verts:
for loop in vertex.link_loops:
for c in isolated_channels:
loop[referenceColor][c] = 1 if loop[referenceColor][c]==0 else clamp01(loop[dst][c]/loop[referenceColor][c])
elif blend_mode == 'OVERLAY':
for vertex in bm.verts:
for loop in vertex.link_loops:
for c in isolated_channels:
if loop[referenceColor][c] < 0.5:
loop[referenceColor][c] = clamp01(loop[dst][c]*2*loop[referenceColor][c])
else:
loop[referenceColor][c] = clamp01(1-2*(1-loop[referenceColor][c])*(1-loop[dst][c]))
elif blend_mode == 'HARDLIGHT':
for vertex in bm.verts:
for loop in vertex.link_loops:
for c in isolated_channels:
if loop[dst][c] < 0.5:
loop[referenceColor][c] = clamp01(loop[dst][c]*2*loop[referenceColor][c])
else:
loop[referenceColor][c] = clamp01(1-2*(1-loop[referenceColor][c])*(1-loop[dst][c]))
elif blend_mode == 'SOFTLIGHT':
for vertex in bm.verts:
for loop in vertex.link_loops:
for c in isolated_channels:
loop[referenceColor][c] = clamp01((1-2*loop[dst][c])*loop[referenceColor][c]**2 + 2*loop[dst][c]*loop[referenceColor][c])
elif blend_mode == 'HUE':
for vertex in bm.verts:
for loop in vertex.link_loops:
dstCol = Color(loop[dst][:3])
refCol = Color(loop[referenceColor][:3])
dstCol.h = refCol.h
loop[referenceColor][:3] = dstCol[:]
elif blend_mode == 'SATURATION':
for vertex in bm.verts:
for loop in vertex.link_loops:
dstCol = Color(loop[dst][:3])
refCol = Color(loop[referenceColor][:3])
dstCol.s = refCol.s
loop[referenceColor][:3] = dstCol[:]
elif blend_mode == 'COLOR':
for vertex in bm.verts:
for loop in vertex.link_loops:
dstCol = Color(loop[dst][:3])
refCol = Color(loop[referenceColor][:3])
dstCol.h = refCol.h
dstCol.s = refCol.s
loop[referenceColor][:3] = dstCol[:]
elif blend_mode == 'VALUE':
for vertex in bm.verts:
for loop in vertex.link_loops:
dstCol = Color(loop[dst][:3])
refCol = Color(loop[referenceColor][:3])
dstCol.v = refCol.v
loop[referenceColor][:3] = dstCol[:]
if self.factor_vcol != 'NONE':
factorvc = color_data[self.factor_vcol]
for vertex in bm.verts:
for loop in vertex.link_loops:
for c in isolated_channels:
f = loop[factorvc][c]*factor_slider
loop[dst][c] = loop[referenceColor][c]*f + (1-f)*loop[dst][c]
else:
for vertex in bm.verts:
for loop in vertex.link_loops:
for c in isolated_channels:
loop[dst][c] = loop[referenceColor][c]*factor_slider + (1-factor_slider)*loop[dst][c]
color_data.remove(referenceColor)
refreshMesh(bm, obj)
trySetActiveVC(obj, dstname)
class BlendChannels(bpy.types.Operator):
bl_idname = "paint.blendchannels"
bl_label = "Blend Transfer Channels"
bl_description = "Transfer vertex colors from another layer to the active isolated layer with specified blend mode. \nRight click to assign shortcut"
bl_context = 'vertexpaint'
bl_options = {'REGISTER', 'UNDO'}
blend_mode : bpy.props.EnumProperty(
name='Blend type',
items=blending_modes,
)
def vcol_layer_items(self, context):
obj = context.active_object
mesh = obj.data
try: vcollayers = mesh.vertex_colors
except: vcollayers = mesh.attributes.color
items = [] if vcollayers is None else [(vcol.name, vcol.name, "") for vcol in vcollayers]
return items
def vcol_layer_items_factor(self, context):
obj = context.active_object
mesh = obj.data
try: vcollayers = mesh.vertex_colors
except: vcollayers = mesh.attributes.color
items = [('NONE','None','')]
if vcollayers is not None:
items.extend([(vcol.name, vcol.name, "") for vcol in vcollayers])
return items
src_vcol: bpy.props.EnumProperty(
name="Source",
items=vcol_layer_items,
description="Source vertex color layer",
)
dst_vcol: bpy.props.EnumProperty(
name="Destination",
items=vcol_layer_items,
description="Destination vertex color layer",
)
factor_vcol: bpy.props.EnumProperty(
name="Factor",
description="Factor color channel. 0-1 RGBA channels corresponds to how much the blend mode will be applied",
items=vcol_layer_items_factor,
)
factor_slider : bpy.props.FloatProperty(
name="Mix",
description="Factor amount",
max=1,
min=0,
)
src_ch: bpy.props.BoolVectorProperty(
name="Source Channel",
description="Source color channel. Click and drag to enable/disable multiple simultaneously",
size=4,
subtype='XYZ',
)
def invoke(self, context, event):
sett = context.scene.paint_alpha_settings
obj = context.active_object.data
self.blend_mode = sett.blend_mode
self.src_ch = sett.src_ch
self.src_vcol = sett.src_vcol
self.factor_slider = sett.factor_slider
self.factor_vcol = sett.factor_vcol
bm = bmesh.new()
bm.from_mesh(obj)
bm.verts.ensure_lookup_table()
color_data = bm.loops.layers.color
self.dst_vcol = findActiveColorLayer(color_data, obj).name
blendChannels(self, context, sett, obj, bm, color_data)
return {'FINISHED'}
def execute(self,context):
sett = context.scene.paint_alpha_settings
obj = context.active_object.data
bm = bmesh.new()
bm.from_mesh(obj)
bm.verts.ensure_lookup_table()
color_data = bm.loops.layers.color
blendChannels(self, context, sett, obj, bm, color_data)
return {'FINISHED'}
class SampleAverageVertex(bpy.types.Operator):
bl_idname = "paint.sample_vertex"
bl_label = "Sample average vertex"
bl_description = "Sample a selected vertex or group of vertices for their average color. \nRight click to assign shortcut"
bl_context = 'vertexpaint'
bl_options = {'UNDO'}
def execute(self, context):
settings = settings = context.scene.paint_alpha_settings
obj = context.active_object.data
bm = bmesh.new()
bm.from_mesh(obj)
bm.verts.ensure_lookup_table()
color_data = bm.loops.layers.color
color_layer = findActiveColorLayer(color_data, obj)
if obj.use_paint_mask_vertex:
vertex_data = [v for v in bm.verts if v.select]
elif settings.enable_indiscriminate_fill:
vertex_data = bm.verts
face_loops = [0,0,0]
i=0
for vertex in vertex_data:
if obj.use_paint_mask:
for loop in vertex.link_loops:
if loop.face.select:
face_loops = [face_loops[i]+loop[color_layer][i] for i in FL]
i+=1
else:
for loop in vertex.link_loops:
face_loops = [face_loops[i]+loop[color_layer][i] for i in FL]
i+=1
flg = (face_loops[0]/i, face_loops[1]/i, face_loops[2]/i)
flgc = Color(flg)
context.tool_settings.vertex_paint.brush.color = flgc
srgb502 = 128/255
srgb251 = 64/255
pal = bpy.data.palettes.get("LEO_Alpha_Palette")
if not pal:
pal = bpy.data.palettes.new("LEO_Alpha_Palette")
# add a color to that palette
default = pal.colors.new()
default.color = (1, srgb502, srgb502)
pal.colors.new().color = (0, srgb502, srgb502)
pal.colors.new().color = (1, srgb251, srgb502)
pal.colors.new().color = (srgb502, srgb502, srgb502)
# make default active
pal.colors.active = default
bpy.context.tool_settings.vertex_paint.palette = pal
setColors = set()
for col in pal.colors:
setColors.add(tuple(col.color))
if flg not in setColors:
flc = pal.colors.new()
flc.color = flgc
pal.colors.active = flc
return {'FINISHED'}
class FlatShading(bpy.types.Operator):
bl_idname = "paint.flat_shading"
bl_label = "ToggleVertexShading"
bl_description = "Set 3d view to flat shading to accurately view vertex colors. \nRight click to assign shortcut"
bl_context = 'vertexpaint'
bl_options = {'REGISTER','UNDO'}
def execute(self, context):
settings = context.scene.paint_alpha_settings
current_shading = context.space_data.shading.light
space_shader_current = context.space_data.shading.color_type
if current_shading != 'FLAT' or space_shader_current != 'VERTEX':
settings.past_shading = context.space_data.shading.light
context.space_data.shading.light = 'FLAT'
settings.space_shader_storage = context.space_data.shading.color_type
context.space_data.shading.color_type = 'VERTEX'
elif settings.past_shading != 'UnInitialized':
context.space_data.shading.light = settings.past_shading
context.space_data.shading.color_type = settings.space_shader_storage
settings.past_shading = 'FLAT'
return {'FINISHED'}
def paintChannel(self, context):
old_color_layer = None
settings = context.scene.paint_alpha_settings
# Get the active object
brushcolor1 = context.tool_settings.vertex_paint.brush.color
obj = context.active_object.data
bm = bmesh.new()
bm.from_mesh(obj)
bm.verts.ensure_lookup_table()
color_data = bm.loops.layers.color
color_layer = findActiveColorLayer(color_data, obj)
basename = color_layer.name
isolated_channels = [int(x) for x in settings.isolated_Channel]
if not isolated_channels or any((x==3 for x in isolated_channels)): isolated_channels = FL
if obj.use_paint_mask_vertex:
vertex_data = [v for v in bm.verts if v.select]
elif settings.enable_indiscriminate_fill:
vertex_data = bm.verts
else:
self.report({'ERROR'}, "Cannot paint when no vertices are selected and ALL option is off.")
return {'FINISHED'}
for vertex in vertex_data:
if obj.use_paint_mask:
face_loops = [loop for loop in vertex.link_loops if loop.face.select]
else:
face_loops = [loop for loop in vertex.link_loops]
for loop in face_loops:
for x in isolated_channels:
loop[color_layer][x] = brushcolor1[x]
refreshMesh(bm, obj)
trySetActiveVC(obj, basename)
return {'FINISHED'}
class PaintAlphaOperator(bpy.types.Operator):
bl_idname = "paint.paint_alpha_operator"
bl_label = "Paint Color"
bl_description = "Paint color for the selected vertices in vertex paint mode. \nRight click to assign shortcut"
bl_context = 'vertexpaint'
bl_options = {'REGISTER','UNDO'}
def execute(self, context):
paintChannel(self, context)
return {'FINISHED'}
def isolateChannel(self, context, ch):
subtract_channels = False
color_layer = None
old_layer_name = None
addOld = False
channel = str(ch)
former_ch = []
settings = context.scene.paint_alpha_settings
obj = context.active_object.data
Mono = settings.one_layer_isolate
settings.isolated_Channel = ""
bm = bmesh.new()
bm.from_mesh(obj)
bm.verts.ensure_lookup_table()
color_data = bm.loops.layers.color
active_color_layer = findActiveColorLayer(color_data, obj)
basename = active_color_layer.name
if keyName in basename:
old_ch = basename.split(keyName)[1]
former_ch = [int(x) for x in old_ch if x]
basename = basename.split(keyName)[0]
settings.isolated_Channel = old_ch
try: color_layer = color_data[basename]
except:
color_layer = color_data.new(basename)
color_layer.copy_from(active_color_layer)
if ch in former_ch:
subtract_channels = True
settings.isolated_Channel = old_ch
else:
old_layer = active_color_layer
if not Mono: active_color_layer = color_data.new(basename+keyName+old_ch+channel)
else: active_color_layer = color_data.new(basename+keyName+channel)
addOld = True
else:
old_layer = active_color_layer
concisoname = basename+keyName
old_layer_name = next((x for x in color_data.keys() if concisoname in x), False)
if old_layer_name:
old_ch = old_layer_name.split(keyName)[1]
former_ch = [int(x) for x in old_ch if x]
old_layer = color_data[old_layer_name]
settings.isolated_Channel = old_ch
try: color_layer = color_data[basename]
except:
color_layer = color_data.new(basename)
color_layer.copy_from(active_color_layer)
if ch in former_ch:
subtract_channels = True
settings.isolated_Channel = old_ch
else:
if not Mono: active_color_layer = color_data.new(concisoname+old_ch+channel)
else: active_color_layer = color_data.new(concisoname+channel)
addOld = True
else:
color_layer = color_data[basename]
active_color_layer = color_data.new(concisoname+channel)
if subtract_channels:
settings.isolated_Channel = settings.isolated_Channel.replace(channel,"")
isolated_channels = [int(x) for x in settings.isolated_Channel]
if 3 in isolated_channels and not ch==3:
if not old_layer_name: prev_layer = active_color_layer
else: prev_layer = old_layer
new_active_name = basename+keyName+settings.isolated_Channel
active_color_layer = color_data.new(new_active_name)
for vertex in bm.verts:
for loop in vertex.link_loops:
a = Color(loop[prev_layer][:3]).v
loop[active_color_layer][:3] = [a,a,a]
if not old_layer_name: color_data.remove(prev_layer)
else: color_data.remove(old_layer)
elif ch==3 and not isolated_channels:
settings.isolated_Channel = ""
if not old_layer_name:
for vertex in bm.verts:
for loop in vertex.link_loops:
loop[color_layer][3] = Color(loop[active_color_layer][:3]).v
color_data.remove(active_color_layer)
else: color_data.remove(old_layer)
new_active_name = basename
elif ch==3:
prev_layer = active_color_layer
new_active_name = basename+keyName+settings.isolated_Channel
active_color_layer = color_data.new(new_active_name)
for vertex in bm.verts:
for loop in vertex.link_loops:
loop[color_layer][3] = Color(loop[prev_layer][:3]).v
rgb_c = [0,0,0]
for x in isolated_channels:
rgb_c[x] = loop[color_layer][x]
loop[active_color_layer][:3] = rgb_c
if not old_layer_name: color_data.remove(prev_layer)
else: color_data.remove(old_layer)
elif isolated_channels:
prev_layer = active_color_layer
new_active_name = basename+keyName+settings.isolated_Channel
active_color_layer = color_data.new(new_active_name)
for vertex in bm.verts:
for loop in vertex.link_loops:
rgb_c = [0,0,0]
for x in former_ch:
loop[color_layer][x]=loop[prev_layer][x]
for x in isolated_channels:
rgb_c[x] = loop[color_layer][x]
loop[active_color_layer][:3] = rgb_c
if not old_layer_name: color_data.remove(prev_layer)
else: color_data.remove(old_layer)
else:
settings.isolated_Channel = ""
for vertex in bm.verts:
for loop in vertex.link_loops:
loop[color_layer][ch] = loop[active_color_layer][ch]
if not old_layer_name: color_data.remove(active_color_layer)
else: color_data.remove(old_layer)
new_active_name = basename
refreshMesh(bm, obj)
trySetActiveVC(obj, new_active_name)
return {'FINISHED'}
if Mono: settings.isolated_Channel = channel
old_channels = [int(x) for x in settings.isolated_Channel]
if channel not in settings.isolated_Channel:
settings.isolated_Channel += channel
isolated_channels = [int(x) for x in settings.isolated_Channel]
if ch==3:
for vertex in bm.verts:
for loop in vertex.link_loops:
a = loop[color_layer][3]
loop[active_color_layer][:3] = (a,a,a)
if addOld:
for vertex in bm.verts:
for loop in vertex.link_loops:
for x in former_ch:
loop[old_layer][x] = loop[color_layer][x]
elif 3 in isolated_channels:
for vertex in bm.verts:
for loop in vertex.link_loops:
a = Color(loop[old_layer][:3]).v
loop[active_color_layer][:3] = (a,a,a)
else:
old_channels = [x for x in old_channels if x!=ch]
if 3 in former_ch and not old_layer_name:
former_ch = [x for x in former_ch if x!=3]
for vertex in bm.verts:
for loop in vertex.link_loops:
for x in former_ch:
loop[color_layer][x]=loop[old_layer][x]
loop[color_layer][3]=Color(loop[old_layer][:3]).v
for vertex in bm.verts:
for loop in vertex.link_loops:
rgb_c = [0,0,0]
for x in old_channels:
rgb_c[x]=loop[old_layer][x]
rgb_c[ch]=loop[color_layer][ch]
loop[active_color_layer][:3] = rgb_c[:3]
if addOld: color_data.remove(old_layer)
refreshMesh(bm, obj)
trySetActiveVC(obj, basename+keyName+settings.isolated_Channel)
return bm, obj
class IsolateVertexAlpha(bpy.types.Operator):
bl_idname = "paint.isolate_vertex_alpha"
bl_label = "A"
bl_description = "Isolate the vertex color alpha channel. \nOutline thickness (Higher = thicker, Lower = less width). \nRight click to assign shortcut"
bl_context = 'vertexpaint'
bl_options = {'UNDO'}
def execute(self, context):
isolateChannel(self, context, 3)
return {'FINISHED'}
class IsolateVertexRed(bpy.types.Operator):
bl_idname = "paint.isolate_vertex_red"
bl_label = "R"
bl_description = "Isolate the vertex color red channel. \nAmbient Occlusion (Higher = no occlude, Lower = Ambient Occlusion). \nRight click to assign shortcut"
bl_context = 'vertexpaint'
bl_options = {'UNDO'}
def execute(self, context):
isolateChannel(self, context, 0)
return {'FINISHED'}
class IsolateVertexGreen(bpy.types.Operator):
bl_idname = "paint.isolate_vertex_green"
bl_label = "G"
bl_description = "Isolate the vertex color green channel. \nShadow Smoothing (Higher = Sharper, Lower = Smoother). \nRight click to assign shortcut"
bl_context = 'vertexpaint'
bl_options = {'UNDO'}
def execute(self, context):
isolateChannel(self, context, 1)
return {'FINISHED'}
class IsolateVertexBlue(bpy.types.Operator):
bl_idname = "paint.isolate_vertex_blue"
bl_label = "B"
bl_description = "Isolate the vertex color blue channel. \nOutline z/depth-index (Higher = behind, Lower = in front). \nRight click to assign shortcut"
bl_context = 'vertexpaint'
bl_options = {'UNDO'}
def execute(self, context):
isolateChannel(self, context, 2)
return {'FINISHED'}
# Gradient tool by andyp123 adapted for leo alpha paint; github at: https://github.com/andyp123/blender_vertex_color_master
def draw_gradient_callback(self, context, line_params, line_shader, circle_shader):
line_batch = batch_for_shader(line_shader, 'LINES', {
"pos": line_params["coords"],
"color": line_params["colors"]})
line_shader.bind()
line_batch.draw(line_shader)
if circle_shader is not None:
a = line_params["coords"][0]
b = line_params["coords"][1]
radius = (b - a).length
steps = 50
circle_points = []
for i in range(steps+1):
angle = (2.0 * math.pi * i) / steps
point = Vector((a.x + radius * math.cos(angle), a.y + radius * math.sin(angle)))
circle_points.append(point)
circle_batch = batch_for_shader(circle_shader, 'LINE_LOOP', {
"pos": circle_points})
circle_shader.bind()
circle_shader.uniform_float("color", line_params["colors"][1])
circle_batch.draw(circle_shader)
def mapsum(self, x):
e = self.error_margin
errorlist = [x[0]+e, x[1]+e, x[2]+e]
errorlist_ = [x[0]-e, x[1]-e, x[2]-e]
return (errorlist_,errorlist)
class SelectByIsolatedVertexColor(bpy.types.Operator):
bl_idname = "paint.selectbyisolatedvertexcolor"
bl_label = "Select Color"
bl_description = "Select by isolated vertex color. \nRight click to assign shortcut"
bl_options = {'REGISTER','UNDO'}
error_margin : bpy.props.FloatProperty(
name="Error margin",
description="precision of color select value",
default=0.001,
min=0
)
restrict_loops : bpy.props.BoolProperty(
name="Exclude Loops",
description="Exclude vertices that have a loop which does not match the primary color",
default=False
)
def execute(self, context):
obj = context.active_object.data
brush = bpy.context.tool_settings.vertex_paint.brush
obj.use_paint_mask_vertex = True
bm = bmesh.new()
bm.from_mesh(obj)
bm.verts.ensure_lookup_table()
color_data = bm.loops.layers.color
color_layer = findActiveColorLayer(color_data, obj)
brushError_, brushError = mapsum(self, brush.color)
for vertex in bm.verts:
for loop in vertex.link_loops:
x = loop[color_layer]
if all((brushError_[i] <= x[i] <= brushError[i] for i in FL)):
try: vertex.select_set(True)
except: vertex.select = True
elif self.restrict_loops:
try: vertex.select_set(False)
except: vertex.select = False
break
refreshMesh(bm, obj)
return {'FINISHED'}
class SelectByPaletteColor(bpy.types.Operator):
bl_idname = "paint.selectbypalettecolor"
bl_label = "Select Palette"
bl_description = "Select by colors in the active palette. WARNING: Slow. Complexity of selection is # of non-hidden vertices * # of palette colors. \nRight click to assign shortcut"
bl_options = {'REGISTER','UNDO'}
error_margin : bpy.props.FloatProperty(
name="Error margin",
description="precision of color select value",
default=0.001,
min=0
)
restrict_loops : bpy.props.BoolProperty(
name="Exclude Loops",
description="Exclude vertices that have a loop which does not match the primary color",
default=False
)
def execute(self, context):
obj = context.active_object.data
palette = context.tool_settings.vertex_paint.palette.colors
obj.use_paint_mask_vertex = True
bm = bmesh.new()
bm.from_mesh(obj)
bm.verts.ensure_lookup_table()
color_data = bm.loops.layers.color
color_layer = findActiveColorLayer(color_data, obj)
paletteError = [mapsum(self, x.color) for x in palette]
for vertex in bm.verts:
for loop in vertex.link_loops:
x = loop[color_layer]
for a, b in paletteError:
if all((a[i] <= x[i] <= b[i] for i in FL)):
try: vertex.select_set(True)
except: vertex.select = True
break
if self.restrict_loops and vertex.select == False:
try: vertex.select_set(False)
except: vertex.select = False
break
refreshMesh(bm, obj)
return {'FINISHED'}
class PaintGradient(bpy.types.Operator):
"""Draw a line with the mouse to paint a vertex color gradient"""
bl_idname = "paint.gradienttool"
bl_label = "Gradient Tool"
bl_description = "Paint vertex color gradient. \nRight click to assign shortcut"
bl_options = {"REGISTER", "UNDO"}
_handle = None
# https://developer.blender.org/docs/release_notes/4.0/python_api/#gpu-module-gpu
# "Blender 4.0: Remove deprecated 2D_ / 3D_ prefix for built-in shader names"
try:
line_shader = gpu.shader.from_builtin('SMOOTH_COLOR')
circle_shader = gpu.shader.from_builtin('UNIFORM_COLOR')
# https://projects.blender.org/blender/blender/commit/8cfca8e1bd85a300732d92b30741d5c243797a31
# Maintain backwards compatibility with Blender versions < 3.4.0