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?