You can try this code to apply color to vertices selected in edit mode:
import bpy
def color_to_vertices(color):
mesh = bpy.context.active_object.data
bpy.ops.object.mode_set(mode = 'VERTEX_PAINT')
selected_verts = []
for vert in mesh.vertices:
if vert.select == True:
selected_verts.append(vert)
for polygon in mesh.polygons:
for selected_vert in selected_verts:
for i, index in enumerate(polygon.vertices):
if selected_vert.index == index:
loop_index = polygon.loop_indices[i]
mesh.vertex_colors.active.data[loop_index].color = color
bpy.ops.object.mode_set(mode = 'EDIT')
import random
RGB = [random.uniform(0,1) for i in range(3)]
color_to_vertices(RGB)
The result will be like this:
Don't forget about proper material node setup if you want to see the result in edit mode (material viewport shading) and render:
For convenience sake you maybe want to create some panel and an operator.