How to set vertex colors on import I have a python based importer and I can successfully import meshes with uv's and normals. Now I am trying the vertex colors, but I ran into an issue.
I use this to create the the vertex color:
vertex_color_layer0 = mesh.vertex_colors.new(name='layer0')

but then when I try to set a vertex color, I get this error
vertex_color_layer0.data[i].color = (c[0],c[1],c[2],c[3])
RuntimeError: bpy_prop_collection[index]: internal error, valid index
0 given in 172178 sized collection, but value not found

When I try the same thing in an interactive session, it works fine, it's just in the import code it fails.
I am able to make it work by looping for times:

    # not sure why we cant do this in one loop.... 
    vertex_color_layer0 = mesh.vertex_colors.new(name='layer0',do_init=False)
    for i, c in enumerate(data.colors):
        vertex_color_layer0.data[i].color = (c[3],c[3],c[3],c[3])
        
    vertex_color_layer1 = mesh.vertex_colors.new(name='layer1',do_init=False)
    for i, c in enumerate(data.colors):
        vertex_color_layer1.data[i].color = (c[0],c[0],c[0],c[0])
   
    vertex_color_layer2 = mesh.vertex_colors.new(name='layer2',do_init=False)
    for i, c in enumerate(data.colors):
        vertex_color_layer2.data[i].color = (c[1],c[1],c[1],c[1])
   
    vertex_color_layer3 = mesh.vertex_colors.new(name='layer3',do_init=False)
    for i, c in enumerate(data.colors):
        vertex_color_layer3.data[i].color = (c[2],c[2],c[2],c[2])

but not in a single loop, that gives me the error above:
    vertex_color_layer0 = mesh.vertex_colors.new(name='layer0',do_init=False)
    vertex_color_layer1 = mesh.vertex_colors.new(name='layer1',do_init=False)
    vertex_color_layer2 = mesh.vertex_colors.new(name='layer2',do_init=False)
    vertex_color_layer3 = mesh.vertex_colors.new(name='layer3',do_init=False)

    for i, c in enumerate(data.colors):
        vertex_color_layer0.data[i].color = (c[3],c[3],c[3],c[3])
        vertex_color_layer1.data[i].color = (c[0],c[0],c[0],c[0])
        vertex_color_layer2.data[i].color = (c[1],c[1],c[1],c[1])
        vertex_color_layer3.data[i].color = (c[2],c[2],c[2],c[2])

But ideally I would want to loop over the colors only once.
Any thoughts on how I could make this work?
Thanks,
Koen
I think it will work if you do it like...
mesh.vertex_colors.new(name='layer0',do_init=False)
mesh.vertex_colors.new(name='layer1',do_init=False)
mesh.vertex_colors.new(name='layer2',do_init=False)
mesh.vertex_colors.new(name='layer3',do_init=False)

vertex_color_layer0 = mesh.vertex_colors['layer0']
vertex_color_layer1 = mesh.vertex_colors['layer1']
vertex_color_layer2 = mesh.vertex_colors['layer2']
vertex_color_layer3 = mesh.vertex_colors['layer3']

for i, c in enumerate(data.colors):
    vertex_color_layer0.data[i].color = (c[3],c[3],c[3],c[3])
    vertex_color_layer1.data[i].color = (c[0],c[0],c[0],c[0])
    vertex_color_layer2.data[i].color = (c[1],c[1],c[1],c[1])
    vertex_color_layer3.data[i].color = (c[2],c[2],c[2],c[2])