2
$\begingroup$

I'm not sure that select_flush is supposed to do and what is the difference between select_flush and select_flush_mode. Could anybody provide any clarification?

The documentations says the following:

select_flush(select)

Flush selection, independent of the current selection mode.

select_flush_mode()

flush selection based on the current mode current BMesh.select_mode.

This answer as I read it seems to say that select_flush will enforce the correct state of selected elements in bmesh and edit mesh, but I'm not sure. And I'm not sure if "based on the current mode" means that in mode is 'VERT', will it take vertex selection as a ground truth and "fix" the edge and face selection?

EDIT:

If I run this on the standard cube I see no difference with/without either of select_flush commands. Exactly 2 vertices and one edge are selected in the bmesh, and the selection is not visible in the 3D View

import bpy
import bmesh
mesh = bpy.context.active_object.data
bm = bmesh.from_edit_mesh(mesh)

bm.verts[0].select_set(True)
bm.verts[1].select_set(True)

#bm.select_flush(True)
#bm.select_flush(False)
bm.select_flush_mode()

print(bm.verts[0].select)
print(bm.verts[1].select)
print(bm.verts[2].select)
print(bm.edges[2].select)
$\endgroup$
5
  • $\begingroup$ Seems like select_flush_mode() is what you would use if you were writing an addon and wanted it to execute the function dependent on what selection mode the user was currently in, while select_flush() works with regardless of the selection mode. Like if you wanted your addon to do one specific thing if you were in face mode, vs another in vertex, etc. $\endgroup$
    – Jakemoyo
    Jun 21, 2022 at 9:56
  • $\begingroup$ Does this answer your question? $\endgroup$ Jun 22, 2022 at 20:13
  • $\begingroup$ @Jakemoyo I see what you mean. Although I still don't understand what either of the commands is supposed to do. I'll add a code snippet to the question. $\endgroup$
    – elena
    Jun 23, 2022 at 15:40
  • $\begingroup$ @MartyFouts I reference that answer in my question and no, I still have no idea what it's supposed to do. $\endgroup$
    – elena
    Jun 23, 2022 at 15:40
  • $\begingroup$ @elena sorry, I missed that it was the same question. $\endgroup$ Jun 23, 2022 at 16:31

1 Answer 1

2
$\begingroup$

So I wrote a script to demonstrate the difference between the two and the purpose for this method.

Say we're doing some kind of programmatic python modeling:

import bpy 
import bmesh 



def demonstrate_select_flush(bm, demo_type=None):

    # loop through all verts to change the selection
    # right now this "selection" only exists in python
    for vert in bm.verts[:]:
        if vert.index % 2 != 0:
            vert.select_set(True)

    # right now verts [1, 3, 5, 7] are "selected"
    # in theory and according to common sense, this would also mean
    # that the edges between each vertex are selected too.
    # However, according to bmesh, this is not yet the case. 
    
    
    # But, if we wanted to tell bmesh: 
    # "I just ran a loop that selected 4 vertices... So..
    # You have 4 vertices selected now right?.
    # that means you also have at LEAST 2 edges and one  
    # face selected, if not more.
    # Tell me what faces and edges those are."
        
    if demo_type == "FLUSH_SELECT":
        # So in order to do that we would run this:
        bm.select_flush(True)
        
    # The bm.select_flush_mode() function will do this same thing, 
    # but only If the new bmesh/python selection you've made
    # and the viewport selection mode are the same.
    
    # So if the user is currently in edge or face selection mode, 
    # and we ran the loop to "select" some vertices like above ^ 
    # this would do nothing.
    
    elif demo_type == "FLUSH_SELECT_MODE":
        bm.select_flush_mode()
        
    # Then, depending on our methods used, 
    # we can get the theoretically "selected" edges via a for loop.
    edges = [e for e in bm.edges if e.select]
    
    # and run a bevel operation that affects those edges.
    bmesh.ops.bevel(bm, geom=edges, affect="EDGES",offset=.125)
    bmesh.update_edit_mesh(me)

Now if we run the function in a few separate scenarios to see the difference:

obj = bpy.context.object
me = obj.data
bm = bmesh.from_edit_mesh(me)
   
demonstrate_select_flush(bm)

You should see nothing happen.

Then we re-run the script with nothing selected in face mode again.

demonstrate_select_flush(bm, demo_type="FLUSH_SELECT")

You should see that the "edges" that we selected by selecting their vertices with python got caught in the bevel operation this time.

enter image description here

Now if you want you can see the difference between flush_mode and regular flush by running the demo again. Add a cube, enter edit mode in face select mode and run the script with nothing selected.

# Trying to use flush_select_mode but in the wrong mode.
    
demonstrate_select_flush(bm, demo_type="FLUSH_SELECT_MODE")

Nothing happens... But now switch to vertex selection mode, and run the script a final time

# with a cube in edit mode, vertex selection mode active.
demonstrate_select_flush(bm, demo_type="FLUSH_SELECT_MODE")

# It should bevel the edges as expected.

Hope this helps clear things up.

$\endgroup$

You must log in to answer this question.

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