Skip to content

Commit 0725fe8

Browse files
new extension for vertex color threshold
1 parent 076796e commit 0725fe8

6 files changed

+102
-2
lines changed

__init__.py

+40-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
import importlib
1010
importlib.reload(apply_all)
1111
importlib.reload(apply_multi_user)
12+
importlib.reload(vertex_color_picker)
1213
else:
1314
from . import apply_all
1415
from . import apply_multi_user
16+
from . import vertex_color_picker
1517

1618
import bpy
1719

@@ -452,6 +454,23 @@ def execute(self, context):
452454
# raise the exception again
453455
raise e
454456

457+
class VertexColorPicker(bpy.types.Operator):
458+
bl_idname = "object.color_picker"
459+
bl_label = "Vertex color picker"
460+
461+
def execute(self, context):
462+
try:
463+
self.report(
464+
{"INFO"}, "----- ----- ----- Sougen Scripts ----- ----- -----")
465+
self.report({"INFO"}, "Vertex color processing processing")
466+
vertex_color_picker.vertex_color_picker(self, context)
467+
return {"FINISHED"}
468+
except Exception as e:
469+
print("Something went wrong")
470+
self.report({"ERROR"}, "Something went wrong")
471+
# raise the exception again
472+
raise e
473+
455474

456475
def curves_export(self, context):
457476
bpy.context.window.cursor_set("WAIT")
@@ -604,6 +623,16 @@ def draw(self, context):
604623
else:
605624
col2.prop(context.scene, "unlit")
606625

626+
box3 = layout.box()
627+
col3 = box3.column()
628+
row3 = col3.row()
629+
row4 = col3.row()
630+
631+
632+
row3.prop(context.scene, "color_treshold")
633+
row4.operator(
634+
"object.color_picker", icon="MOD_VERTEX_WEIGHT", depress=loading
635+
)
607636
layout.operator(
608637
"object.simple_operator", icon="VIEW_CAMERA", depress=loading
609638
)
@@ -620,8 +649,9 @@ def draw(self, context):
620649
icon="EDITMODE_HLT", depress=loading)
621650

622651

652+
623653
blender_classes = [BakeCamera, SimpleGLTF, GLTF_Collider,
624-
GLTF_PT_Panel, GLTF_Instance, Curves_Export, ApplyMultiUser]
654+
GLTF_PT_Panel, GLTF_Instance, Curves_Export, ApplyMultiUser, VertexColorPicker]
625655

626656

627657
def register():
@@ -658,6 +688,14 @@ def register():
658688
min=0,
659689
max=10,
660690
)
691+
bpy.types.Scene.color_treshold = bpy.props.FloatProperty(
692+
name="Color threshold:",
693+
description="Vertex color treshold",
694+
default=0.2,
695+
min=0.01,
696+
max=1,
697+
)
698+
661699
bpy.types.Scene.unlit = bpy.props.BoolProperty(
662700
name="Use KHR Unlit",
663701
description="Convert materials from metal/rough to unlit",
@@ -675,5 +713,6 @@ def unregister():
675713
del bpy.types.Scene.filename_path
676714
del bpy.types.Scene.draco
677715
del bpy.types.Scene.draco_level
716+
del bpy.types.Scene.color_treshold
678717
del bpy.types.Scene.unlit
679718
del bpy.types.Scene.gltf_sys

__pycache__/__init__.cpython-39.pyc

853 Bytes
Binary file not shown.
-33 Bytes
Binary file not shown.
1.78 KB
Binary file not shown.

apply_multi_user.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def applyModifierToMultiUser(self, context):
2323
linked.append(obj)
2424

2525

26-
bpy.ops.object.make_single_user(object=True, obdata=True, material=False, animation=False, obdata_animation=False)
26+
bpy.ops.object.make_single_user(object=True, obdata=True, material=False)
2727
bpy.ops.object.modifier_apply(modifier="Decimate")
2828

2929

vertex_color_picker.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import bpy
2+
import pprint
3+
import bmesh
4+
5+
from mathutils import Color
6+
7+
from collections import defaultdict
8+
9+
def avg_col(cols):
10+
avg_col = Color((0.0, 0.0, 0.0))
11+
12+
for col in cols:
13+
tempcol = Color((col[0], col[1], col[2]))
14+
15+
avg_col += tempcol/len(cols)
16+
return avg_col
17+
18+
def vertex_color_picker(self, context):
19+
tk = defaultdict(list)
20+
21+
mesh = bpy.context.active_object.data
22+
color_layer = mesh.vertex_colors['Col']
23+
24+
i = 0
25+
for poly in mesh.polygons:
26+
for idx in poly.loop_indices:
27+
loop = mesh.loops[idx]
28+
color = color_layer.data[i].color
29+
tk[loop.vertex_index].append(color)
30+
i += 1
31+
32+
vcol_averages = {k: avg_col(v) for k, v in tk.items()}
33+
34+
vertices_of_interest = [k for k, v in vcol_averages.items() if v.r < context.scene.color_treshold]
35+
36+
37+
bpy.ops.object.mode_set(mode='EDIT')
38+
39+
40+
obj = bpy.context.object
41+
me = obj.data
42+
bm = bmesh.from_edit_mesh(me)
43+
44+
vertices= [e for e in bm.verts]
45+
oa = bpy.context.active_object
46+
47+
for vert in vertices:
48+
if vert.index in vertices_of_interest:
49+
vert.select_set(True)
50+
else:
51+
vert.select_set(False)
52+
53+
54+
bmesh.update_edit_mesh(me)
55+
56+
pprint.pprint(vertices_of_interest)
57+
58+
# you must cast this as a dict manually like tk = dict(tk) ,
59+
# if you need it as a dict, but defaultdict will behave mostly the same
60+
61+
#pprint.pprint(tk)

0 commit comments

Comments
 (0)