3
$\begingroup$

How can I check if the mesh in edit mode has polygons (Faces) selected or not via Python? I use Blender v2.79.

I am working on a face selection check for a tool. If no polygons are selected in the mesh, the output console should print('No Faces Selected'), if polygons are selected in mesh, print('Faces are Selected') instead.

$\endgroup$

3 Answers 3

4
$\begingroup$

Polygons can be accessed in Python from the object data. So we start at bpy.context.object.data.polygons to retrieve the polygons of the active object. Note that I assume the object is in Edit Mode. This yields a list of polygons, which then can be checked for selection by accessing the select attribute.

To check the first polygon in the mesh for selection state, use:

bpy.context.object.data.polygons[0].select
# this returns True if the first polygon is selected,
# or False in case it is not

To get the selection state for all mesh polygons, you can use list accumulation:

print([x.select for x in bpy.context.object.data.polygons])
# for the Default Cube, this should yield:
# [True, True, True, True, True, True]
# IF all faces are selected. Play with selection to see the change,
# but remember to tab in and out of Edit Mode to make Blender aware of the
# selection change before running the code again

Now, if you just want to know if there is at least one polygon selected, you can query if True is in that list:

print(True in [x.select for x in bpy.context.object.data.polygons])
# this prints True if at least one polygon is selected

An IF-statement now could be:

if True in [x.select for x in bpy.context.object.data.polygons]:
    print('Faces are Selected')
else:
    print('No Faces Selected')
$\endgroup$
1
  • $\begingroup$ thank you very much for this detailed description :3 $\endgroup$
    – DevilSam
    Commented Jan 12, 2019 at 20:36
5
$\begingroup$

Use an edit bmesh

I would only use the Mesh.polygons collection for the simplest of tasks, otherwise for all else use bmesh.

  • Does not require an edit mode toggle
  • Is far simpler to work with IMO.

 

import bpy
import bmesh

context = bpy.context
ob = context.edit_object
me = ob.data

bm = bmesh.from_edit_mesh(me)
# list of selected faces
selfaces = [f for f in bm.faces if f.select]
if selfaces:
    print("%d faces selected" % len(selfaces))
else:
    print("No Faces Selected")

Changing selection in code

# invert selection
for f in bm.faces:
    f.select = not f.select

# to refresh and see changes
bmesh.update_edit_mesh(me)
$\endgroup$
0
$\begingroup$

For 2.79, look at the top bar to see the faces selected and for 2.8, look at the bottom bar

$\endgroup$
2
  • $\begingroup$ but show me the nummber of selection, but i need a function that checks if faces is selected in the mesh or not. $\endgroup$
    – DevilSam
    Commented Jan 12, 2019 at 17:57
  • $\begingroup$ Related Would need to "grep out" the values for selected faces from string. $\endgroup$
    – batFINGER
    Commented Jan 12, 2019 at 23:47

You must log in to answer this question.

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