Multi-Objects: MESH_OT_vert_connect_path by Falk David
authorDalai Felinto <dfelinto@gmail.com>
Mon, 14 May 2018 13:55:14 +0000 (15:55 +0200)
committerDalai Felinto <dfelinto@gmail.com>
Mon, 14 May 2018 13:56:26 +0000 (15:56 +0200)
With a few changes:
* Use space after if.
* Skip for loop when no selected vertices.

Maniphest Tasks: T54643
Differential Revision: https://developer.blender.org/D3349

source/blender/editors/mesh/editmesh_tools.c

index 5c6bc64..97debd6 100644 (file)
@@ -1106,91 +1106,97 @@ void MESH_OT_mark_sharp(wmOperatorType *ot)
        RNA_def_property_flag(prop, PROP_SKIP_SAVE);
 }
 
-static int edbm_vert_connect_exec(bContext *C, wmOperator *op)
+static bool bm_connect_vert_pair(BMEditMesh *em, wmOperator *op)
 {
-       ViewLayer *view_layer = CTX_data_view_layer(C);
-       uint objects_len = 0;
-       uint failed_objects_len = 0;
-       Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
-       for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
-               Object *obedit = objects[ob_index];
-               BMEditMesh *em = BKE_editmesh_from_object(obedit);
-               BMesh *bm = em->bm;
-               BMOperator bmop;
-               bool is_pair = (bm->totvertsel == 2);
-               int len = 0;
-               bool check_degenerate = true;
-               const int verts_len = bm->totvertsel;
-               BMVert **verts;
-               bool checks_succeded = true;
-
-               if (!is_pair) {
-                       continue;
+       BMesh *bm = em->bm;
+       BMOperator bmop;
+       const int verts_len = bm->totvertsel;
+       bool is_pair = (verts_len == 2);
+       int len = 0;
+       bool check_degenerate = true;
+
+       BMVert **verts;
+       bool checks_succeded = true;
+
+       /* sanity check */
+       if (!is_pair) {
+               return false;
+       }
+
+       verts = MEM_mallocN(sizeof(*verts) * verts_len, __func__);
+       {
+               BMIter iter;
+               BMVert *v;
+               int i = 0;
+
+               BM_ITER_MESH(v, &iter, bm, BM_VERTS_OF_MESH) {
+                       if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
+                               verts[i++] = v;
+                       }
                }
 
-               verts = MEM_mallocN(sizeof(*verts) * verts_len, __func__);
+               if (BM_vert_pair_share_face_check_cb(
+                       verts[0], verts[1],
+                       BM_elem_cb_check_hflag_disabled_simple(BMFace *, BM_ELEM_HIDDEN)))
                {
-                       BMIter iter;
-                       BMVert *v;
-                       int i = 0;
+                       check_degenerate = false;
+                       is_pair = false;
+               }
+       }
 
-                       BM_ITER_MESH(v, &iter, bm, BM_VERTS_OF_MESH) {
-                               if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
-                                       verts[i++] = v;
-                               }
-                       }
+       if (is_pair) {
+               if (!EDBM_op_init(
+                       em, &bmop, op,
+                       "connect_vert_pair verts=%eb verts_exclude=%hv faces_exclude=%hf",
+                       verts, verts_len, BM_ELEM_HIDDEN, BM_ELEM_HIDDEN))
+               {
+                       checks_succeded = false;
+               }
+       }
+       else {
+               if (!EDBM_op_init(
+                       em, &bmop, op,
+                       "connect_verts verts=%eb faces_exclude=%hf check_degenerate=%b",
+                       verts, verts_len, BM_ELEM_HIDDEN, check_degenerate))
+               {
+                       checks_succeded = false;
+               }
+       }
+       if (checks_succeded) {
+               BMO_op_exec(bm, &bmop);
+               len = BMO_slot_get(bmop.slots_out, "edges.out")->len;
 
-                       if (is_pair) {
-                               if (BM_vert_pair_share_face_check_cb(
-                                           verts[0], verts[1],
-                                           BM_elem_cb_check_hflag_disabled_simple(BMFace *, BM_ELEM_HIDDEN)))
-                               {
-                                       check_degenerate = false;
-                                       is_pair = false;
-                               }
-                       }
+               if (len && is_pair) {
+                       /* new verts have been added, we have to select the edges, not just flush */
+                       BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "edges.out", BM_EDGE, BM_ELEM_SELECT, true);
                }
 
-               if (is_pair) {
-                       if (!EDBM_op_init(
-                                   em, &bmop, op,
-                                   "connect_vert_pair verts=%eb verts_exclude=%hv faces_exclude=%hf",
-                                   verts, verts_len, BM_ELEM_HIDDEN, BM_ELEM_HIDDEN))
-                       {
-                               checks_succeded = false;
-                       }
+               if (!EDBM_op_finish(em, &bmop, op, true)) {
+                       len = 0;
                }
                else {
-                       if (!EDBM_op_init(
-                                   em, &bmop, op,
-                                   "connect_verts verts=%eb faces_exclude=%hf check_degenerate=%b",
-                                   verts, verts_len, BM_ELEM_HIDDEN, check_degenerate))
-                       {
-                               checks_succeded = false;
-                       }
+                       EDBM_selectmode_flush(em);  /* so newly created edges get the selection state from the vertex */
+
+                       EDBM_update_generic(em, true, true);
                }
-               if (checks_succeded) {
-                       BMO_op_exec(bm, &bmop);
-                       len = BMO_slot_get(bmop.slots_out, "edges.out")->len;
+       }
+       MEM_freeN(verts);
 
-                       if (len) {
-                               if (is_pair) {
-                                       /* new verts have been added, we have to select the edges, not just flush */
-                                       BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "edges.out", BM_EDGE, BM_ELEM_SELECT, true);
-                               }
-                       }
+       return len;
+}
 
-                       if (!EDBM_op_finish(em, &bmop, op, true)) {
-                               len = 0;
-                       }
-                       else {
-                               EDBM_selectmode_flush(em);  /* so newly created edges get the selection state from the vertex */
+static int edbm_vert_connect_exec(bContext *C, wmOperator *op)
+{
+       ViewLayer *view_layer = CTX_data_view_layer(C);
+       uint objects_len = 0;
+       uint failed_objects_len = 0;
+       Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
 
-                               EDBM_update_generic(em, true, true);
-                       }
-               }
-               MEM_freeN(verts);
-               if (len == 0) {
+       for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
+               Object *obedit = objects[ob_index];
+               BMEditMesh *em = BKE_editmesh_from_object(obedit);
+
+               if (!bm_connect_vert_pair(em, op)) {
                        failed_objects_len++;
                }
        }
@@ -1434,43 +1440,66 @@ static bool bm_vert_connect_select_history_edge_to_vert_path(BMesh *bm, ListBase
 
 static int edbm_vert_connect_path_exec(bContext *C, wmOperator *op)
 {
-       Object *obedit = CTX_data_edit_object(C);
-       BMEditMesh *em = BKE_editmesh_from_object(obedit);
-       BMesh *bm = em->bm;
-       bool is_pair = (em->bm->totvertsel == 2);
-       ListBase selected_orig = {NULL, NULL};
-       int retval;
+       ViewLayer *view_layer = CTX_data_view_layer(C);
+       uint objects_len = 0;
+       uint failed_selection_order_len = 0;
+       uint failed_connect_len = 0;
+       Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
 
-       /* when there is only 2 vertices, we can ignore selection order */
-       if (is_pair) {
-               return edbm_vert_connect_exec(C, op);
-       }
+       for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
+               Object *obedit = objects[ob_index];
+               BMEditMesh *em = BKE_editmesh_from_object(obedit);
+               BMesh *bm = em->bm;
+               const bool is_pair = (em->bm->totvertsel == 2);
+               ListBase selected_orig = {NULL, NULL};
 
-       if (bm->selected.first) {
-               BMEditSelection *ese = bm->selected.first;
-               if (ese->htype == BM_EDGE) {
-                       if (bm_vert_connect_select_history_edge_to_vert_path(bm, &selected_orig)) {
-                               SWAP(ListBase, bm->selected, selected_orig);
+               if (bm->totvertsel == 0) {
+                       continue;
+               }
+
+               /* when there is only 2 vertices, we can ignore selection order */
+               if (is_pair) {
+                       if(!bm_connect_vert_pair(em, op)) {
+                               failed_connect_len++;
                        }
+                       continue;
                }
-       }
 
-       if (bm_vert_connect_select_history(bm)) {
-               EDBM_selectmode_flush(em);
-               EDBM_update_generic(em, true, true);
-               retval = OPERATOR_FINISHED;
+               if (bm->selected.first) {
+                       BMEditSelection *ese = bm->selected.first;
+                       if (ese->htype == BM_EDGE) {
+                               if (bm_vert_connect_select_history_edge_to_vert_path(bm, &selected_orig)) {
+                                       SWAP(ListBase, bm->selected, selected_orig);
+                               }
+                       }
+               }
+
+               if (bm_vert_connect_select_history(bm)) {
+                       EDBM_selectmode_flush(em);
+                       EDBM_update_generic(em, true, true);
+               }
+               else {
+                       failed_selection_order_len++;
+               }
+
+               if (!BLI_listbase_is_empty(&selected_orig)) {
+                       BM_select_history_clear(bm);
+                       bm->selected = selected_orig;
+               }
        }
-       else {
+
+       MEM_freeN(objects);
+
+       if (failed_selection_order_len == objects_len) {
                BKE_report(op->reports, RPT_ERROR, "Invalid selection order");
-               retval = OPERATOR_CANCELLED;
+               return OPERATOR_CANCELLED;
        }
-
-       if (!BLI_listbase_is_empty(&selected_orig)) {
-               BM_select_history_clear(bm);
-               bm->selected = selected_orig;
+       else if (failed_connect_len == objects_len) {
+               BKE_report(op->reports, RPT_ERROR, "Could not connect vertices");
+               return OPERATOR_CANCELLED;
        }
 
-       return retval;
+       return OPERATOR_FINISHED;
 }
 
 void MESH_OT_vert_connect_path(wmOperatorType *ot)