Skip to content

Instantly share code, notes, and snippets.

@galiminus
Created September 11, 2023 10:23
Show Gist options
  • Save galiminus/0af67779600fc679e3b085642f2a7032 to your computer and use it in GitHub Desktop.
Save galiminus/0af67779600fc679e3b085642f2a7032 to your computer and use it in GitHub Desktop.
blender vertex color to seams
import bpy
import bmesh
# Assume triangulated mesh
object = bpy.context.object
bm = bmesh.from_edit_mesh(object.data)
bm.calc_loop_triangles()
bm.edges.ensure_lookup_table()
bm.faces.ensure_lookup_table()
color_layer = bm.loops.layers.color.active
def get_edge_color(edge):
edge_color = None
for loop in edge.link_loops:
loop_color = loop[color_layer][:]
if edge_color == None:
edge_color = loop_color
if loop_color != edge_color:
return None
return edge_color
def get_full_face_color(face, exclude_verts):
face_color = None
colors = []
for loop in face.loops:
loop_color = loop[color_layer][:]
colors.append(loop_color)
if loop.vert in exclude_verts:
continue
if face_color != None and loop_color != face_color:
continue
face_color = loop_color
return face_color
for edge in bm.edges:
edge.seam = False
edge_color = get_edge_color(edge)
if edge_color == None:
continue
face_colors = []
for face in edge.link_faces:
face_colors.append(
get_full_face_color(face=face, exclude_verts=edge.verts)
)
# Exclude boundaries
if len(face_colors) != 2:
continue
# Face are the same colors, no seam needed
if face_colors[0] == face_colors[1]:
continue
if edge_color == face_colors[0]:
opposing_color = face_colors[1]
else:
opposing_color = face_colors[0]
if edge_color < opposing_color:
edge.seam = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment