[Solved]How to get list of selected faces

I’m writing some selection editing operators (like Select More, but different), but I’ve got a serious problem. How do I find out which faces are selected currently?

I tried the following:


 for aface in mesh.faces:
     if aface.select:  #something goes wrong here; apparently for all faces aface.select is true
          initial_selected.append(aface) 

but all the faces are appended.

I’m using blender 2.55 beta

this works fine in the latest svn…

what you may not realise is that if you are running this script in edit mode it’ll give you a list of faces that were selected when you first entered edit mode, not the currently selected faces.

your script should check for edit mode, switch to object mode if necessary, do your thing then restore edit mode at the end of the script if you were in it originally.


import bpy

ob = bpy.context.active_object
me = ob.data

selfaces =[]

#check for edit mode
editmode = False
if ob.mode == 'EDIT':
    editmode =True
    #the following sets mode to object by default
    bpy.ops.object.mode_set()
        
        
        
for f in me.faces:
    if f.select:
        print(f.index)
        selfaces.append(f)
        
        
        
#done editing, restore edit mode if needed
if editmode:
    bpy.ops.object.mode_set(mode = 'EDIT')

I am totally agree with you your are right what you may not realise is that if you are running this script in edit mode it’ll give you a list of faces that were selected when you first entered edit mode, not the currently selected faces.
your script should check for edit mode, switch to object mode if necessary, do your thing then restore edit mode at the end of the script if you were in it originally.

Thanks, that is a clear explanation. Now it works.