0
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ Post your code. Possibly the reference is getting invalidated so you could try and see if mesh.vertex_colors['layer0'].data[i].color = ... works. $\endgroup$
    – scurest
    Commented Nov 15, 2020 at 23:11
  • $\begingroup$ thanks I'll try that, I added the code to the original post that caused the error. $\endgroup$
    – Koen
    Commented Nov 16, 2020 at 2:38
  • $\begingroup$ Would you want to use a smaller file instead of 172,178 vertices one? It might be some memory reallocate when you create multiple Blender array at once. The previous pointer to the array might have been changed due to the size reallocate for later entries $\endgroup$
    – HikariTW
    Commented Nov 16, 2020 at 4:49
  • $\begingroup$ Yeah, each new invalidates the old references. See developer.blender.org/T43332#294071 for an example of how to do it. $\endgroup$
    – scurest
    Commented Nov 16, 2020 at 11:04
  • $\begingroup$ thanks all, that was very usefull! $\endgroup$
    – Koen
    Commented Nov 16, 2020 at 18:58

1 Answer 1

0
$\begingroup$

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])
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .