face selection

Is there a smarter way of selecting faces of a mesh?

Put the code in a text window (use Scripting) make a mesh active and select some (or all) faces and
run the script …


import bpy
import bmesh

def find_all_selected_faces_and_deselect():
    obj = bpy.context.scene.objects.active
    bpy.ops.object.mode_set(mode='EDIT')
    bm = bmesh.from_edit_mesh(obj.data)
    sel = [el.index for el in bm.faces if el.select]
    bpy.ops.mesh.select_all(action='TOGGLE')
    bpy.ops.object.mode_set(mode='OBJECT')
    return sel

all_selected = find_all_selected_faces_and_deselect()
print(all_selected)

def select_one_face(index):
    result = ("ERROR index not possible", index)
    obj = bpy.context.scene.objects.active
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.mesh.select_all(action = "DESELECT")
    bm = bmesh.from_edit_mesh(obj.data)
    tmp = len(bm.faces[:])
    if index in range(tmp):
        bm.faces[index].select = True
        result = "done"
    bpy.ops.object.mode_set(mode='OBJECT')
    return result

print(select_one_face(-4))

def select_several_face(index_list):
    result = ("wrong", index_list)
    if len(index_list) > 0: 
        ind_max = max(index_list)
        obj = bpy.context.scene.objects.active
        bpy.ops.object.mode_set(mode='EDIT')
        bpy.ops.mesh.select_all(action = "DESELECT")
        bm = bmesh.from_edit_mesh(obj.data)
        available_faces = len(bm.faces)
        if ind_max < available_faces:
            for index in index_list:
                bm.faces[index].select = True
            result = "done"
    bpy.ops.object.mode_set(mode='OBJECT')
    return result

res = select_several_face([0,2,4,6 ])

print(res)


You can use the standard API to select something if a mesh is in object mode to avoid mode switches. Deselecting is partly broken however. It seems that if you are in face select mode and deselect polygons, the faces will become unselected, but edges and verts stay selected. Deselecting vertices only doesn’t seem to work at all.

hmmm, you mean that there is somewhere in the REAL source something to do?

And what is the standard way of finding selected faces in OBJECT mode?
vertices and edges ok data.vertices[index] (.select = False) I know
but faces?
Please tell me :wink:

Faces are now calledpolygons.

Select a single face of the cube.


import bpy


ob = bpy.data.objects.get("Cube")
ob.data.polygons[0].select = True

Thanks, your are totally right.
Changing selection has to be done in OBJECT mode

Why I asked (though I knew about polygons) … I (earlier) tried on probably a too complicated object and did not see any …:o

BTW: What is the reason why you want to change selection state, but stay in object mode?

Oh, easy, I need such sort of functions in an addon and that needs OBJECT mode to be visible …

Hm ok… but what is a selection good for if you don’t show it anyway?
What does your addon do with it?