3

Taking a cue from this question, I'm testing to make a one-color fill to a vertex_colors. Works fine, but is very slow on meshes with many polygons.

ob = context.object
color = (0,0,0,0)
vertex_color = ob.data.vertex_colors['My Vertex color']

i = 0 for p in ob.data.polygons: for idx in p.loop_indices: vertex_color.data[i].color = (color) i += 1

I also found an operator,bpy.ops.paint.vertex_color_set() (It seems to be twice as fast) but I don't really like the idea, plus it's an operator with no input apparently

Is there any blender Api that do this directly?

After a good answer:

The code above is code that doesn't make sense in this case. My tests moved to this method (Not very fast):

for d in vertex_color.data[:]: #Slow method
    d.color = (1,0,0,1)

In any case, given @batFINGER answer, and @lemon help, the code is now 4 times and more, faster than before.

Noob Cat
  • 1,222
  • 3
  • 20
  • 61
  • 1
    You can use "o.data.vertex_colors['Col'].data.foreach_set( "color", colors )" where "colors" is a flatten array of all the rgba colors. – lemon Aug 23 '20 at 15:11
  • Sorry @lemon RuntimeError: internal error setting the array – Noob Cat Aug 23 '20 at 15:18
  • 2
    the array needs to be flatten: [1, 0, 0, 1, 0, 1, 0, 1, ...] not [(1, 0, 0, 1), (0, 1, 0, 1), ...] and its length is so each vertex is considered for each face it belongs to. So for the cube, there are 24 vcols. – lemon Aug 23 '20 at 15:20

1 Answers1

6

Set all loop vertex colors to one

Use foreach set method. Example of use here creating bezier curves https://blender.stackexchange.com/a/180184/15543

import bpy
import numpy as np

context = bpy.context

name = "Xxxx" r, g, b, a = (1, 0, 0, 1) # red

ob = context.object me = ob.data

color_layer = (me.vertex_colors.get(name) or me.vertex_colors.new(name=name) ) ones = np.ones(len(color_layer.data))

color_layer.data.foreach_set( "color", np.array((r * ones, g * ones, b * ones, a * ones)).T.ravel(), )

me.update()

Could also simply

np.array((r, g, b, a) * len(color_layer.data)).T.ravel()
batFINGER
  • 84,216
  • 10
  • 108
  • 233