1

For a plugin I am making, I need to highlight the vertices that the user hovers over. I could not work out this color assignment part yet. I have tried methods then found out they were obsolete. I have tried color attributes but it didnt seem to do anything.. Heres the code anyways:

import bpy
import random

obj = bpy.context.active_object if obj.type == 'MESH': mesh = obj.data # Get the active color layer color_layer = mesh.color_attributes.active

# Create a new layer if none exists
if not color_layer:
    color_layer = mesh.color_attributes.new(name="FalloffGradient", type="FLOAT_COLOR", domain="POINT")

colors = []
# Example: Set random colors for each vertex
for v in mesh.vertices:
    colors.append({'index':v.index,'color':(random.random(), random.random(), random.random()),'alpha':1.0})

color_layer.data.foreach_set("colors",colors) # I think this is wrong since i didnt set the size first

# Ensure changes are reflected in the viewport
mesh.update()

# If vertex colors aren't visible by default:
obj.data.use_paint_mask_vertex = True

I feel this is the wrong way to do things. Would really appreciate this or any other solutions that make highlighting possible, including opacity. Thanks!

SKB_BGPL
  • 11
  • 2

0 Answers0