Proper way of selecting geometry in object mode?

If a have my operator called from object mode, how do I select face, edges, or vertices so that if the user switches to edit mode those elements will be selected ?
Should I use BM_elem_flag_enable(f, BM_ELEM_SELECT), BM_face_select_set(BMesh *bm, BMFace *f, const bool select) or
BM_select_history_store (this one doesn’t display) ?

If you are working with a BMesh, then BM_face_select_set and similar for vertices and edges are the ones to use. In edit mode there exists a persistent BMesh that you can edit, outside of edit mode there is only a Mesh.

So in object mode there are operators that work on a Mesh directly, and operators that temporarily create a BMesh and then convert it back to a Mesh. The right thing to do depends on how your operator works, and there’s not a lot of operators that affect selection outside of edit mode, so some more context about what you’re trying to do would help to answer the question.

For example painting supports face selection outside edit mode, but it’s not as straightforward as calling a single function because the selection needs to be flushed to vertices and edges, do some updates for drawing, etc.

I am working on a normal mapping algorithm that uses subdivisions of a mesh and gets called from object mode.

There are types of geometry that are not supported by the operator such as triangles or n-gons, I want to let the user know which parts of the mesh caused the operator to abort.

So I decided to select these parts, so that when the user switches to edit mode he can fix them.

What does the operator do though? Does it edit the mesh, or just read it? Does it work on Mesh or BMesh?

If you are working on a BMesh and converting that back to Mesh at the end, you would use BM_face_select_set. If you are working on a Mesh directly, you can look at paintface_mouse_select() and do something similar.

1 Like

It works with base_me->mpoly[idx].flag |= ME_FACE_SEL;.

It didn’t work with my bmesh because I generated it using BKE_mesh_to_bmesh(base_me, base_obj, false, &params); so its gets freed at the end of the operator.

I thought the bmesh would somehow write back to the mesh when its gets freed, either way I didn’t think Mesh could hold selection data.

Thanks for the reply, problem solved.