Historical · current compatibility unverified Original context: 2020 forum discussion; PME and Blender versions were not stated.
Outcome
Use one trigger for two intentional paths: act immediately when edges are selected, otherwise open a selection tool that prepares the missing selection.
import bmesh
bm = bmesh.from_edit_mesh(C.object.data)
has_selected_edge = any(edge.select for edge in bm.edges)
open_menu("Select and Merge") if not has_selected_edge else bpy.ops.mesh.merge(type="COLLAPSE")Recipe
- Limit the caller to a Mesh/Edit Mesh context.
- Query only the state that actually decides the next action.
- Keep each branch independently usable: the fallback should prepare a valid selection, not merely show an error.
- Test no selection, one edge, multiple edges, and an invalid mode.
When two alternative controls are visible at once, the same predicate can be placed in the relevant menu items’ Poll methods: one item for not has_selected_edge, another for has_selected_edge.
Pitfalls
bmesh.from_edit_mesh()requires an edit mesh. It is not safe to evaluate in Object mode or on a non-mesh object.- Do not use selection count as a proxy for a more specific requirement. Count selected edges when the operator requires edges; query faces or vertices when it requires those.
- Keep destructive actions behind an explicit operation, not an implicit fallback.
Related answers
- Choose a conditional-execution pattern
- Diagnose Blender context failures
- Route to a context-specific menu