Best way to check if object is in Edit Mode


I want to check if the object the operator is acting on is in object mode.

I’m trying to port VIEW3D_OT_snap_selected_to_cursor to support multiple objects in edit mode. For now it uses if (obedit) { ... to check if the object is in edit mode, but when I use

objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);

then if (objects[0]) {... is always true.

So should I use something like if (OBEDIT_FROM_VIEW_LAYER(view_layer) == NULL) or if (mesh->edit_btmesh) {.. ? Or is there a better way to achieve this? Thanks.

Snap to grid seems to do this, so that should be ok:

Object *obedit = CTX_data_edit_object(C);
if (obedit) {

Otherwise I think you should test if (objects_len == 0) to verify that there are no objects in edit mode, it’s not safe to assume the array has a first element.