Importing vertex color for bmesh

Hello,
I am trying to update a script to bmesh for importing vertex color.
But all my attempts have failed.
Here is my code:


import bpy,mathutils
 
# 5 vertex with x,y,z coordinates
vertex = [1.0, 0.0, -1.0, -1.0, 0.0, -1.0, 1.0, 0.0, 1.0, -1.0, 0.0, 1.0, -1.0, 0.78, -1.8]
# 2 faces (aquad and a tri
faces = [1, 0, 2, 3, 4, 0, 1, 0]
# the vertex color (by vertex index)
polypaint = [(255, 3, 0), (10, 0, 255), (81, 255, 0), (77, 255, 0), (19, 253, 0)]
 
me = bpy.data.meshes.new('Mesh')
 
me.vertices.add(len(vertex)/3)
me.vertices.foreach_set("co", vertex)
 
me.tessfaces.add(len(faces)/4)
me.tessfaces.foreach_set("vertices_raw",faces)
 
vertexColor = me.vertex_colors.new()
 
me.update()
for poly in me.polygons:
    print("face ",poly.index)
    for loop_index in poly.loop_indices:
        loop = me.loops[loop_index]
        v = loop.vertex_index
        print("vertex ",v)
        color = polypaint[v]
        print("color ",color,"
")
        vertexColor.data[poly.index].color = mathutils.Color([color[0]/255,color[1]/255,color[2]/255])
 
 
# another method, same result:
# me.update(calc_tessface=True)
# for i in range(len(me.tessfaces)):
    # for j in range(len(me.tessfaces[i].vertices)):
        # color = polypaint[me.tessfaces[i].vertices[j]]
        # vertexColor.data[i].color = mathutils.Color([color[0]/255,color[1]/255,color[2]/255])
        # print(mathutils.Color([color[0]/255,color[1]/255,color[2]/255]))
        # print(vertexColor.data[i].color,"
")
ob = bpy.data.objects.new("obj", me)
bpy.context.scene.objects.link(ob)

Blender crash with this two methods…
What is the right way to create a new mesh with vertex painting?

Well, you’re not alone. I’ve tried to update one of my script that paint vertexcolors, but it do not work with bmesh.
Perhaps something is broken, perhaps bmesh is not finished yet, i don’t know.
I’ve already posted some messages here about that.

good luck.

Hello raphael,
the Blender script ‘vertexpaint_dirt.py’ can help us, it works fine with bmesh.

I think that maybe there is need to update something in the mesh…

I have found the problem:
we need to add vertex_color after linking the mesh to an object:


import bpy,mathutils
print("
	------")
vertex = [1.0, 0.0, -1.0, -1.0, 0.0, -1.0, 1.0, 0.0, 1.0, -1.0, 0.0, 1.0, -1.0, 0.78, -1.8]
faces = [1, 0, 2, 3, 4, 0, 1, 0]
polypaint = [(255, 3, 0), (10, 0, 255), (81, 255, 0), (77, 255, 0), (19, 253, 0)]
#----
me = bpy.data.meshes.new('Mesh')
me.vertices.add(len(vertex)/3)
me.vertices.foreach_set("co", vertex)
me.tessfaces.add(len(faces)/4)
me.tessfaces.foreach_set("vertices_raw",faces)
me.update()
#----
ob = bpy.data.objects.new("obj", me)
bpy.context.scene.objects.link(ob)
# ----
vertexColor = me.vertex_colors.new()
i=0
for poly in me.polygons:
    print("face ",poly.index)
    for loop_index in poly.loop_indices:
        loop = me.loops[loop_index]
        v = loop.vertex_index
        print("vertex ",v)
        color = polypaint[v]
        print("color ",color,"
")
        vertexColor.data[i].color = mathutils.Color([color[0]/255,color[1]/255,color[2]/255])
        i+=1

Hi,

Congratulation, and thanks a lot for this solution !