Pick the state to use before the loop.
set_state = bm.faces[0].select
for f in bm.faces:
f.select = set_state
Note that within your loop f
is the same as bm.faces[f.index]
Or if you want to loop through until you find the first selected, use break
to stop the loop.
for f in bm.faces:
print(f.index)
if f.select:
break
In blender's UI, pressing a button executes an operator. You can give an operator properties and set different values to be used when clicking each button in the UI. This also means you can have one button and choose which value it uses.
An operator with a boolean property -
class MySelector(bpy.types.Operator):
bl_idname = 'mesh.my_select'
bl_label = 'sample selecting'
state = bpy.props.BoolProperty()
def execute(self, context):
obj = context.edit_object
bm = bmesh.from_edit_mesh(obj.data)
for f in bm.faces:
f.select = self.state
bmesh.update_edit_mesh(obj.data, True)
return {'FINISHED'}
A panel to show the buttons -
class MySelectPanel(bpy.types.Panel):
bl_idname = "OBJECT_PT_MySelector"
bl_label = "Selecting"
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
def draw(self, context):
row = self.layout.row()
row.operator(MySelector.bl_idname, text='Deselect').state = False
row.operator(MySelector.bl_idname, text='Select').state = True
It is also possible to set multiple properties for one button
b = row.operator(MySelector.bl_idname, text='Deselect')
b.state = False
b.value2 = 'Use this value'