1
$\begingroup$

Okay, I need help again.

I've got a model with colored vertices. So far, there are only two colors applyed (red and white. It's a reference model which I use to define the script for automation). I need to select all white polygons in that model, so I can select them and hand the selection over to another function which generates another model based on the selection. Selecting the vertices is posing a problem, as vertices can have more than one color at once. So I tried to use the polygons.

Now, these polygons should have a color, right? How do I extract the color values so I can do something like this ((very) pseudo code):

for polygon in mesh.polygons:
    if polygon.color == red:
        plygon.selected = True

I read through other questions, but they all want to apply another color to a polygon. I don't need that. The color is already there. And it seems I have dive more into python to extract the snippets which would do the trick for me from the other answers.

$\endgroup$
2
  • 1
    $\begingroup$ faces have a material poly.material_index. The material has a color. For vertex color see blender.stackexchange.com/questions/121933/… $\endgroup$
    – batFINGER
    Jan 12, 2021 at 15:17
  • $\begingroup$ how do I access the material? The importet ply gives me an empty material collection $\endgroup$
    – Coder1234
    Jan 13, 2021 at 7:38

1 Answer 1

1
$\begingroup$

Welp, I don't really like to answer my own questions. But I found the solution and I wanted to share it with you :)

bm = bmesh.new()                          #Generate new bmesh object
bm.from_mesh(obj.data)                    #Assign the model mesh
    for face in bm.faces:                 #goes through the faces
        for loop in face.loops:           #goes through the loops of each face
            #your condition here, eg:
            if loop[bm.loops.layers.color[0]] == Vector((1,1,1,1)): #the left side is the vertex color
                loop.vert.select = True   #to select the vertex if correct
            else:
                loop.vert.select = False  #a good addition. sometimes all vertices are selected from the beginning

So the trick is convertig it to a bmesh and go through that bmesh. After everything is done, just throw it back to the bpy-mesh:

bm.to_mesh(obj.data)

And if you change the view to edit-mode you will notice that all the (in this case white) vertices are selected while the rest is deselected. Just make sure your object is in object-mode. At least, when giving returning the bmesh back to your mesh.

(And for all the newbies: "obj.data" stands for "bpy.data.objects['x'].data", which is of course the mesh of the object :) )

$\endgroup$
1
  • $\begingroup$ Default cube each vert belongs to 3 linked faces. The selection of verts using method above will depend on the face order. ie if the last face is not white it will deselect all 4 verts of face, but the 4 adjoining faces could all be white and hence 2 verts of each of those white faces is now deselected. Recommend instead selecting a face based on loop vert colours. Also see blender.stackexchange.com/a/73900/15543 re comparing values, checking for equality is dodgy. Can also be done with bmesh in edit mode. Can be done without bmesh look at mesh color layer. $\endgroup$
    – batFINGER
    Jan 23, 2021 at 16:01

You must log in to answer this question.

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