Python selection module using bmesh

Hi everyone.

Maybe you saw my Entoforms project a while ago:

I’m busy programming the next step in their evolution now… But… this also means I want it to work in the latest version of Blender. And that means re-writing some python scripts for use with B-mesh. Specifically the selection of faces/polygons based on a nr of difference scenarios.

What I require is a blender python module that has the following methods of selecting/deselecting polygons (and some extras):

  • by direction
  • checkered
  • in a specific vertex group
  • innermost (of selection)
  • outermost (of selection)
  • all
  • none
  • patches (will explain later)

You can find the original module (non bmesh) here for reference:

I’d love to know if there’s anyone out there who would like to help me in my project. I definitely don’t expect you to do it for free, but I don’t have a great deal of money. I expect it would take me a couple days to write it myself, and it’s definitely worth something to save me that time.

hey man. didn’t I port some of your stuff over a while back?

Hi Meta,

Yes you certainly did, but those don’t use bmesh, and because of the way bmesh was integrated, doing a simple = True doesn’t update the state of the verts in that face correctly any more… So say you have a specific selection of faces, and from that you let the script select only those facing a certain direction (in effect de-select everything else)… it does that beautifully, but the verts connected to faces in your original selection all remain selected (aren’t automatically updated).

One could rewrite the module to check the state of verts and edges related to every face after selection, but it’s very inefficient… so as far as I can tell, the most efficient thing is a complete redo using bmesh (though the concepts can be reused).

To clarify… here’s an example of how the old script screws up:

Selection flushing is indeed broken in standard API, bmesh module provides select_set(…) which flushes down.

I’d like to support your project, but need more details on how you wanna use a bmesh selection module - like, I would consider making it a class that holds a reference to an editmesh bm and runs stuff on that. Or should it be kept procedural?

Some quick lines:

# Select all polygons (or deselect)
def all(bm, action='SELECT'):
    #for p in
    # = True

    actions = ('SELECT', 'DESELECT', 'INVERT')
    if action not in actions:
        raise(TypeError("action not in " + repr(actions)))
    # Could use stock operator here
    if action == 'INVERT':
        for face in
            face.select_set(False) if else face.select_set(True)
        sel = action == 'SELECT'
        for face in

# Select all polygons (or deselect)
def none(bm):
    all(bm, 'DESELECT')

# Make sure only the vertices connected to selected polygons are selected
def vertexCheck(bm):
    faces = [f for f in bm.faces if]
    for face in faces:

Hi CoDEmanX, I think you are in #blenderpython sometimes right? I’ll join you there for a chat.

Mainly I think it would be nice to create something that has the potential to be useful for all of the community. So maybe the end product should be a series of “operators” that can be used in edit mode like all other selection tools. Currently my selection code is a module that I use from other scripts in object mode… but that doesn’t help anyone else (though it would be easy to tap into those modules.)

Also if each selection operator/script is completely standalone that makes it easier to update/maintain as well.

yes, i’m often around irc, so drop me a line.

Not sure about capsulating selection functions into operators, as they can add overhead. Maybe do it like in C-code: write a function that can be used in a low-level way, and provide an operator that basically acts as a wrapper for that function. So one could use it standalone as operator, as well as the actual function directly without op call. In this case, i wouldn’t recommend to implement the actual function somehow as class.