diff options
author | Hans Goudey <h.goudey@me.com> | 2022-09-23 17:38:37 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2022-09-23 18:45:07 +0300 |
commit | 12becbf0dffe06b6f28c4cc444fe0312cf9249b9 (patch) | |
tree | fb7b54e30e9144021ba0714e0c2dd78eb8f5e928 /source/blender/editors/mesh | |
parent | e345686cb7f4a8ccdc10e85d4f05cded45850cf7 (diff) |
Mesh: Move selection flags to generic attributes
Using the attribute name semantics from T97452, this patch moves the
selection status of mesh elements from the `SELECT` of vertices, and
edges, and the `ME_FACE_SEL` of faces to generic boolean attribute
Storing this data as generic attributes can significantly simplify and
improve code, as described in T95965.
The attributes are called `.select_vert`, `.select_edge`, and
`.select_poly`. The `.` prefix means they are "UI attributes",so they
still contain original data edited by users, but they aren't meant to
be accessed procedurally by the user in arbitrary situations. They are
also be hidden in the spreadsheet and the attribute list.
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when selection is used. When the flags are removed
completely, requirements will decrease.
Further notes:
* The `MVert` flag is empty at runtime now, so it can be ignored.
* `BMesh` is unchanged, otherwise the change would be much larger.
* Many tests have slightly different results, since the selection
attribute uses more generic propagation. Previously you couldn't
really rely on edit mode selections being propagated procedurally.
Now it mostly works as expected.
Similar to 2480b55f216c
Ref T95965
Differential Revision: https://developer.blender.org/D15795
Diffstat (limited to 'source/blender/editors/mesh')
-rw-r--r-- | source/blender/editors/mesh/editface.cc | 318 | ||||
-rw-r--r-- | source/blender/editors/mesh/mesh_data.cc | 34 |
2 files changed, 192 insertions, 160 deletions
diff --git a/source/blender/editors/mesh/editface.cc b/source/blender/editors/mesh/editface.cc index f729db29b8c..ac8dd87bed6 100644 --- a/source/blender/editors/mesh/editface.cc +++ b/source/blender/editors/mesh/editface.cc @@ -73,14 +73,9 @@ void paintface_flush_flags(bContext *C, Mesh *me_eval = (Mesh *)ob_eval->runtime.data_eval; bke::MutableAttributeAccessor attributes_eval = me_eval->attributes_for_write(); bool updated = false; - const Span<MPoly> me_polys = me->polys(); if (me_orig != nullptr && me_eval != nullptr && me_orig->totpoly == me->totpoly) { /* Update the COW copy of the mesh. */ - MutableSpan<MPoly> orig_polys = me_orig->polys_for_write(); - for (int i = 0; i < me->totpoly; i++) { - orig_polys[i].flag = me_polys[i].flag; - } if (flush_hidden) { const VArray<bool> hide_poly_me = attributes_me.lookup_or_default<bool>( ".hide_poly", ATTR_DOMAIN_FACE, false); @@ -89,28 +84,46 @@ void paintface_flush_flags(bContext *C, hide_poly_me.materialize(hide_poly_orig.span); hide_poly_orig.finish(); } + if (flush_selection) { + const VArray<bool> select_poly_me = attributes_me.lookup_or_default<bool>( + ".select_poly", ATTR_DOMAIN_FACE, false); + bke::SpanAttributeWriter<bool> select_poly_orig = + attributes_orig.lookup_or_add_for_write_only_span<bool>(".select_poly", + ATTR_DOMAIN_FACE); + select_poly_me.materialize(select_poly_orig.span); + select_poly_orig.finish(); + } /* Mesh polys => Final derived polys */ if ((index_array = (const int *)CustomData_get_layer(&me_eval->pdata, CD_ORIGINDEX))) { - MutableSpan<MPoly> eval_polys = me_orig->polys_for_write(); - /* loop over final derived polys */ - for (const int i : eval_polys.index_range()) { - if (index_array[i] != ORIGINDEX_NONE) { - /* Copy flags onto the final derived poly from the original mesh poly */ - eval_polys[i].flag = me_polys[index_array[i]].flag; + if (flush_hidden) { + const VArray<bool> hide_poly_orig = attributes_orig.lookup_or_default<bool>( + ".hide_poly", ATTR_DOMAIN_FACE, false); + bke::SpanAttributeWriter<bool> hide_poly_eval = + attributes_eval.lookup_or_add_for_write_only_span<bool>(".hide_poly", + ATTR_DOMAIN_FACE); + for (const int i : IndexRange(me_eval->totpoly)) { + const int orig_poly_index = index_array[i]; + if (orig_poly_index != ORIGINDEX_NONE) { + hide_poly_eval.span[i] = hide_poly_orig[orig_poly_index]; + } } + hide_poly_eval.finish(); } - const VArray<bool> hide_poly_orig = attributes_orig.lookup_or_default<bool>( - ".hide_poly", ATTR_DOMAIN_FACE, false); - bke::SpanAttributeWriter<bool> hide_poly_eval = - attributes_eval.lookup_or_add_for_write_only_span<bool>(".hide_poly", ATTR_DOMAIN_FACE); - for (const int i : IndexRange(me_eval->totpoly)) { - const int orig_poly_index = index_array[i]; - if (orig_poly_index != ORIGINDEX_NONE) { - hide_poly_eval.span[i] = hide_poly_orig[orig_poly_index]; + if (flush_selection) { + const VArray<bool> select_poly_orig = attributes_orig.lookup_or_default<bool>( + ".select_poly", ATTR_DOMAIN_FACE, false); + bke::SpanAttributeWriter<bool> select_poly_eval = + attributes_eval.lookup_or_add_for_write_only_span<bool>(".select_poly", + ATTR_DOMAIN_FACE); + for (const int i : IndexRange(me_eval->totpoly)) { + const int orig_poly_index = index_array[i]; + if (orig_poly_index != ORIGINDEX_NONE) { + select_poly_eval.span[i] = select_poly_orig[orig_poly_index]; + } } + select_poly_eval.finish(); } - hide_poly_eval.finish(); updated = true; } @@ -141,25 +154,26 @@ void paintface_hide(bContext *C, Object *ob, const bool unselected) return; } - MutableSpan<MPoly> polys = me->polys_for_write(); bke::MutableAttributeAccessor attributes = me->attributes_for_write(); bke::SpanAttributeWriter<bool> hide_poly = attributes.lookup_or_add_for_write_span<bool>( ".hide_poly", ATTR_DOMAIN_FACE); + bke::SpanAttributeWriter<bool> select_poly = attributes.lookup_or_add_for_write_span<bool>( + ".select_poly", ATTR_DOMAIN_FACE); for (int i = 0; i < me->totpoly; i++) { - MPoly *mpoly = &polys[i]; if (!hide_poly.span[i]) { - if (((mpoly->flag & ME_FACE_SEL) == 0) == unselected) { + if (!select_poly.span[i] == unselected) { hide_poly.span[i] = true; } } if (hide_poly.span[i]) { - mpoly->flag &= ~ME_FACE_SEL; + select_poly.span[i] = false; } } hide_poly.finish(); + select_poly.finish(); BKE_mesh_flush_hidden_from_polys(me); @@ -174,18 +188,19 @@ void paintface_reveal(bContext *C, Object *ob, const bool select) return; } - MutableSpan<MPoly> polys = me->polys_for_write(); bke::MutableAttributeAccessor attributes = me->attributes_for_write(); if (select) { const VArray<bool> hide_poly = attributes.lookup_or_default<bool>( ".hide_poly", ATTR_DOMAIN_FACE, false); - for (int i = 0; i < me->totpoly; i++) { - MPoly *mpoly = &polys[i]; + bke::SpanAttributeWriter<bool> select_poly = attributes.lookup_or_add_for_write_span<bool>( + ".select_poly", ATTR_DOMAIN_FACE); + for (const int i : hide_poly.index_range()) { if (hide_poly[i]) { - mpoly->flag |= ME_FACE_SEL; + select_poly.span[i] = true; } } + select_poly.finish(); } attributes.remove(".hide_poly"); @@ -207,27 +222,29 @@ static void select_linked_tfaces_with_seams(Mesh *me, const uint index, const bo BLI_bitmap *poly_tag = BLI_BITMAP_NEW(me->totpoly, __func__); const Span<MEdge> edges = me->edges(); - MutableSpan<MPoly> polys = me->polys_for_write(); + const Span<MPoly> polys = me->polys(); const Span<MLoop> loops = me->loops(); - bke::AttributeAccessor attributes = me->attributes(); + bke::MutableAttributeAccessor attributes = me->attributes_for_write(); const VArray<bool> hide_poly = attributes.lookup_or_default<bool>( ".hide_poly", ATTR_DOMAIN_FACE, false); + bke::SpanAttributeWriter<bool> select_poly = attributes.lookup_or_add_for_write_span<bool>( + ".select_poly", ATTR_DOMAIN_FACE); if (index != (uint)-1) { /* only put face under cursor in array */ - const MPoly *mp = &polys[index]; - BKE_mesh_poly_edgebitmap_insert(edge_tag, mp, &loops[mp->loopstart]); + const MPoly &poly = polys[index]; + BKE_mesh_poly_edgebitmap_insert(edge_tag, &poly, &loops[poly.loopstart]); BLI_BITMAP_ENABLE(poly_tag, index); } else { /* fill array by selection */ for (int i = 0; i < me->totpoly; i++) { - MPoly *mp = &polys[i]; if (hide_poly[i]) { /* pass */ } - else if (mp->flag & ME_FACE_SEL) { - BKE_mesh_poly_edgebitmap_insert(edge_tag, mp, &loops[mp->loopstart]); + else if (select_poly.span[i]) { + const MPoly &poly = polys[i]; + BKE_mesh_poly_edgebitmap_insert(edge_tag, &poly, &loops[poly.loopstart]); BLI_BITMAP_ENABLE(poly_tag, i); } } @@ -238,7 +255,6 @@ static void select_linked_tfaces_with_seams(Mesh *me, const uint index, const bo /* expand selection */ for (int i = 0; i < me->totpoly; i++) { - MPoly *mp = &polys[i]; if (hide_poly[i]) { continue; } @@ -246,8 +262,9 @@ static void select_linked_tfaces_with_seams(Mesh *me, const uint index, const bo if (!BLI_BITMAP_TEST(poly_tag, i)) { mark = false; - const MLoop *ml = &loops[mp->loopstart]; - for (int b = 0; b < mp->totloop; b++, ml++) { + const MPoly &poly = polys[i]; + const MLoop *ml = &loops[poly.loopstart]; + for (int b = 0; b < poly.totloop; b++, ml++) { if ((edges[ml->e].flag & ME_SEAM) == 0) { if (BLI_BITMAP_TEST(edge_tag, ml->e)) { mark = true; @@ -258,7 +275,7 @@ static void select_linked_tfaces_with_seams(Mesh *me, const uint index, const bo if (mark) { BLI_BITMAP_ENABLE(poly_tag, i); - BKE_mesh_poly_edgebitmap_insert(edge_tag, mp, &loops[mp->loopstart]); + BKE_mesh_poly_edgebitmap_insert(edge_tag, &poly, &loops[poly.loopstart]); do_it = true; } } @@ -268,9 +285,8 @@ static void select_linked_tfaces_with_seams(Mesh *me, const uint index, const bo MEM_freeN(edge_tag); for (int i = 0; i < me->totpoly; i++) { - MPoly *mp = &polys[index]; if (BLI_BITMAP_TEST(poly_tag, i)) { - SET_FLAG_FROM_TEST(mp->flag, select, ME_FACE_SEL); + select_poly.span[i] = select; } } @@ -305,17 +321,17 @@ bool paintface_deselect_all_visible(bContext *C, Object *ob, int action, bool fl return false; } - MutableSpan<MPoly> polys = me->polys_for_write(); - bke::AttributeAccessor attributes = me->attributes(); + bke::MutableAttributeAccessor attributes = me->attributes_for_write(); const VArray<bool> hide_poly = attributes.lookup_or_default<bool>( ".hide_poly", ATTR_DOMAIN_FACE, false); + bke::SpanAttributeWriter<bool> select_poly = attributes.lookup_or_add_for_write_span<bool>( + ".select_poly", ATTR_DOMAIN_FACE); if (action == SEL_TOGGLE) { action = SEL_SELECT; for (int i = 0; i < me->totpoly; i++) { - MPoly *mpoly = &polys[i]; - if (!hide_poly[i] && mpoly->flag & ME_FACE_SEL) { + if (!hide_poly[i] && select_poly.span[i]) { action = SEL_DESELECT; break; } @@ -325,29 +341,29 @@ bool paintface_deselect_all_visible(bContext *C, Object *ob, int action, bool fl bool changed = false; for (int i = 0; i < me->totpoly; i++) { - MPoly *mpoly = &polys[i]; - if (!hide_poly[i]) { - switch (action) { - case SEL_SELECT: - if ((mpoly->flag & ME_FACE_SEL) == 0) { - mpoly->flag |= ME_FACE_SEL; - changed = true; - } - break; - case SEL_DESELECT: - if ((mpoly->flag & ME_FACE_SEL) != 0) { - mpoly->flag &= ~ME_FACE_SEL; - changed = true; - } - break; - case SEL_INVERT: - mpoly->flag ^= ME_FACE_SEL; - changed = true; - break; - } + if (hide_poly[i]) { + continue; + } + const bool old_selection = select_poly.span[i]; + switch (action) { + case SEL_SELECT: + select_poly.span[i] = true; + break; + case SEL_DESELECT: + select_poly.span[i] = false; + break; + case SEL_INVERT: + select_poly.span[i] = !select_poly.span[i]; + changed = true; + break; + } + if (old_selection != select_poly.span[i]) { + changed = true; } } + select_poly.finish(); + if (changed) { if (flush_flags) { paintface_flush_flags(C, ob, true, false); @@ -375,15 +391,17 @@ bool paintface_minmax(Object *ob, float r_min[3], float r_max[3]) bke::AttributeAccessor attributes = me->attributes(); const VArray<bool> hide_poly = attributes.lookup_or_default<bool>( ".hide_poly", ATTR_DOMAIN_FACE, false); + const VArray<bool> select_poly = attributes.lookup_or_default<bool>( + ".select_poly", ATTR_DOMAIN_FACE, false); for (int i = 0; i < me->totpoly; i++) { - const MPoly *mp = &polys[i]; - if (hide_poly[i] || !(mp->flag & ME_FACE_SEL)) { + if (hide_poly[i] || !select_poly[i]) { continue; } - const MLoop *ml = &loops[mp->loopstart]; - for (int b = 0; b < mp->totloop; b++, ml++) { + const MPoly &poly = polys[i]; + const MLoop *ml = &loops[poly.loopstart]; + for (int b = 0; b < poly.totloop; b++, ml++) { mul_v3_m3v3(vec, bmat, verts[ml->v].co); add_v3_v3v3(vec, vec, ob->obmat[3]); minmax_v3v3_v3(r_min, r_max, vec); @@ -401,7 +419,6 @@ bool paintface_mouse_select(bContext *C, Object *ob) { using namespace blender; - MPoly *mpoly_sel = nullptr; uint index; bool changed = false; bool found = false; @@ -409,14 +426,14 @@ bool paintface_mouse_select(bContext *C, /* Get the face under the cursor */ Mesh *me = BKE_mesh_from_object(ob); - MutableSpan<MPoly> polys = me->polys_for_write(); - bke::AttributeAccessor attributes = me->attributes(); + bke::MutableAttributeAccessor attributes = me->attributes_for_write(); const VArray<bool> hide_poly = attributes.lookup_or_default<bool>( ".hide_poly", ATTR_DOMAIN_FACE, false); + bke::AttributeWriter<bool> select_poly = attributes.lookup_or_add_for_write<bool>( + ".select_poly", ATTR_DOMAIN_FACE); if (ED_mesh_pick_face(C, ob, mval, ED_MESH_PICK_DEFAULT_FACE_DIST, &index)) { if (index < me->totpoly) { - mpoly_sel = polys.data() + index; if (!hide_poly[index]) { found = true; } @@ -424,7 +441,7 @@ bool paintface_mouse_select(bContext *C, } if (params->sel_op == SEL_OP_SET) { - if ((found && params->select_passthrough) && (mpoly_sel->flag & ME_FACE_SEL)) { + if ((found && params->select_passthrough) && select_poly.varray[index]) { found = false; } else if (found || params->deselect_all) { @@ -437,31 +454,19 @@ bool paintface_mouse_select(bContext *C, me->act_face = (int)index; switch (params->sel_op) { - case SEL_OP_ADD: { - mpoly_sel->flag |= ME_FACE_SEL; + case SEL_OP_SET: + case SEL_OP_ADD: + select_poly.varray.set(index, true); break; - } - case SEL_OP_SUB: { - mpoly_sel->flag &= ~ME_FACE_SEL; + case SEL_OP_SUB: + select_poly.varray.set(index, false); break; - } - case SEL_OP_XOR: { - if (mpoly_sel->flag & ME_FACE_SEL) { - mpoly_sel->flag &= ~ME_FACE_SEL; - } - else { - mpoly_sel->flag |= ME_FACE_SEL; - } - break; - } - case SEL_OP_SET: { - mpoly_sel->flag |= ME_FACE_SEL; + case SEL_OP_XOR: + select_poly.varray.set(index, !select_poly.varray[index]); break; - } - case SEL_OP_AND: { + case SEL_OP_AND: BLI_assert_unreachable(); /* Doesn't make sense for picking. */ break; - } } /* image window redraw */ @@ -476,10 +481,9 @@ bool paintface_mouse_select(bContext *C, void paintvert_flush_flags(Object *ob) { using namespace blender; + using namespace blender; Mesh *me = BKE_mesh_from_object(ob); Mesh *me_eval = BKE_object_get_evaluated_mesh(ob); - const int *index_array = nullptr; - if (me == nullptr) { return; } @@ -492,25 +496,42 @@ void paintvert_flush_flags(Object *ob) return; } - index_array = (const int *)CustomData_get_layer(&me_eval->vdata, CD_ORIGINDEX); + const bke::AttributeAccessor attributes_orig = me->attributes(); + bke::MutableAttributeAccessor attributes_eval = me_eval->attributes_for_write(); - const Span<MVert> verts = me->verts_for_write(); - MutableSpan<MVert> verts_eval = me_eval->verts_for_write(); + const int *orig_indices = (const int *)CustomData_get_layer(&me_eval->vdata, CD_ORIGINDEX); - if (index_array) { - int orig_index; - for (const int i : verts_eval.index_range()) { - orig_index = index_array[i]; - if (orig_index != ORIGINDEX_NONE) { - verts_eval[i].flag = verts[index_array[i]].flag; + const VArray<bool> hide_vert_orig = attributes_orig.lookup_or_default<bool>( + ".hide_vert", ATTR_DOMAIN_POINT, false); + bke::SpanAttributeWriter<bool> hide_vert_eval = + attributes_eval.lookup_or_add_for_write_only_span<bool>(".hide_vert", ATTR_DOMAIN_POINT); + if (orig_indices) { + for (const int i : hide_vert_eval.span.index_range()) { + if (orig_indices[i] != ORIGINDEX_NONE) { + hide_vert_eval.span[i] = hide_vert_orig[orig_indices[i]]; } } } else { - for (const int i : verts_eval.index_range()) { - verts_eval[i].flag = verts[i].flag; + hide_vert_orig.materialize(hide_vert_eval.span); + } + hide_vert_eval.finish(); + + const VArray<bool> select_vert_orig = attributes_orig.lookup_or_default<bool>( + ".select_vert", ATTR_DOMAIN_POINT, false); + bke::SpanAttributeWriter<bool> select_vert_eval = + attributes_eval.lookup_or_add_for_write_only_span<bool>(".select_vert", ATTR_DOMAIN_POINT); + if (orig_indices) { + for (const int i : select_vert_eval.span.index_range()) { + if (orig_indices[i] != ORIGINDEX_NONE) { + select_vert_eval.span[i] = select_vert_orig[orig_indices[i]]; + } } } + else { + select_vert_orig.materialize(select_vert_eval.span); + } + select_vert_eval.finish(); BKE_mesh_batch_cache_dirty_tag(me, BKE_MESH_BATCH_DIRTY_ALL); } @@ -529,17 +550,17 @@ bool paintvert_deselect_all_visible(Object *ob, int action, bool flush_flags) return false; } - MutableSpan<MVert> verts = me->verts_for_write(); - bke::AttributeAccessor attributes = me->attributes(); + bke::MutableAttributeAccessor attributes = me->attributes_for_write(); const VArray<bool> hide_vert = attributes.lookup_or_default<bool>( ".hide_vert", ATTR_DOMAIN_POINT, false); + bke::SpanAttributeWriter<bool> select_vert = attributes.lookup_or_add_for_write_span<bool>( + ".select_vert", ATTR_DOMAIN_POINT); if (action == SEL_TOGGLE) { action = SEL_SELECT; for (int i = 0; i < me->totvert; i++) { - MVert *mvert = &verts[i]; - if (!hide_vert[i] && mvert->flag & SELECT) { + if (!hide_vert[i] && select_vert.span[i]) { action = SEL_DESELECT; break; } @@ -548,29 +569,28 @@ bool paintvert_deselect_all_visible(Object *ob, int action, bool flush_flags) bool changed = false; for (int i = 0; i < me->totvert; i++) { - MVert *mvert = &verts[i]; - if (!hide_vert[i]) { - switch (action) { - case SEL_SELECT: - if ((mvert->flag & SELECT) == 0) { - mvert->flag |= SELECT; - changed = true; - } - break; - case SEL_DESELECT: - if ((mvert->flag & SELECT) != 0) { - mvert->flag &= ~SELECT; - changed = true; - } - break; - case SEL_INVERT: - mvert->flag ^= SELECT; - changed = true; - break; - } + if (hide_vert[i]) { + continue; + } + const bool old_selection = select_vert.span[i]; + switch (action) { + case SEL_SELECT: + select_vert.span[i] = true; + break; + case SEL_DESELECT: + select_vert.span[i] = false; + break; + case SEL_INVERT: + select_vert.span[i] = !select_vert.span[i]; + break; + } + if (old_selection != select_vert.span[i]) { + changed = true; } } + select_vert.finish(); + if (changed) { /* handle mselect */ if (action == SEL_SELECT) { @@ -606,22 +626,23 @@ void paintvert_select_ungrouped(Object *ob, bool extend, bool flush_flags) paintvert_deselect_all_visible(ob, SEL_DESELECT, false); } - MutableSpan<MVert> verts = me->verts_for_write(); - bke::AttributeAccessor attributes = me->attributes(); + bke::MutableAttributeAccessor attributes = me->attributes_for_write(); const VArray<bool> hide_vert = attributes.lookup_or_default<bool>( ".hide_vert", ATTR_DOMAIN_POINT, false); + bke::SpanAttributeWriter<bool> select_vert = attributes.lookup_or_add_for_write_span<bool>( + ".select_vert", ATTR_DOMAIN_POINT); - for (int i = 0; i < me->totvert; i++) { - MVert *mv = &verts[i]; - const MDeformVert *dv = &dverts[i]; + for (const int i : select_vert.span.index_range()) { if (!hide_vert[i]) { - if (dv->dw == nullptr) { + if (dverts[i].dw == nullptr) { /* if null weight then not grouped */ - mv->flag |= SELECT; + select_vert.span[i] = true; } } } + select_vert.finish(); + if (flush_flags) { paintvert_flush_flags(ob); } @@ -635,24 +656,25 @@ void paintvert_hide(bContext *C, Object *ob, const bool unselected) return; } - MutableSpan<MVert> verts = me->verts_for_write(); bke::MutableAttributeAccessor attributes = me->attributes_for_write(); bke::SpanAttributeWriter<bool> hide_vert = attributes.lookup_or_add_for_write_span<bool>( ".hide_vert", ATTR_DOMAIN_POINT); + bke::SpanAttributeWriter<bool> select_vert = attributes.lookup_or_add_for_write_span<bool>( + ".select_vert", ATTR_DOMAIN_POINT); - for (const int i : verts.index_range()) { - MVert &vert = verts[i]; + for (const int i : hide_vert.span.index_range()) { if (!hide_vert.span[i]) { - if (((vert.flag & SELECT) == 0) == unselected) { + if (!select_vert.span[i] == unselected) { hide_vert.span[i] = true; } } if (hide_vert.span[i]) { - vert.flag &= ~SELECT; + select_vert.span[i] = false; } } hide_vert.finish(); + select_vert.finish(); BKE_mesh_flush_hidden_from_verts(me); @@ -668,18 +690,20 @@ void paintvert_reveal(bContext *C, Object *ob, const bool select) return; } - MutableSpan<MVert> verts = me->verts_for_write(); bke::MutableAttributeAccessor attributes = me->attributes_for_write(); const VArray<bool> hide_vert = attributes.lookup_or_default<bool>( ".hide_vert", ATTR_DOMAIN_POINT, false); + bke::SpanAttributeWriter<bool> select_vert = attributes.lookup_or_add_for_write_span<bool>( + ".select_vert", ATTR_DOMAIN_POINT); - for (const int i : verts.index_range()) { - MVert &vert = verts[i]; + for (const int i : select_vert.span.index_range()) { if (hide_vert[i]) { - SET_FLAG_FROM_TEST(vert.flag, select, SELECT); + select_vert.span[i] = select; } } + select_vert.finish(); + /* Remove the hide attribute to reveal all vertices. */ attributes.remove(".hide_vert"); diff --git a/source/blender/editors/mesh/mesh_data.cc b/source/blender/editors/mesh/mesh_data.cc index 710cc6c88d9..dc3389844b8 100644 --- a/source/blender/editors/mesh/mesh_data.cc +++ b/source/blender/editors/mesh/mesh_data.cc @@ -1134,6 +1134,7 @@ void ED_mesh_update(Mesh *mesh, bContext *C, bool calc_edges, bool calc_edges_lo static void mesh_add_verts(Mesh *mesh, int len) { + using namespace blender; if (len == 0) { return; } @@ -1152,17 +1153,18 @@ static void mesh_add_verts(Mesh *mesh, int len) BKE_mesh_runtime_clear_cache(mesh); - const int old_vertex_num = mesh->totvert; mesh->totvert = totvert; - MutableSpan<MVert> verts = mesh->verts_for_write(); - for (MVert &vert : verts.drop_front(old_vertex_num)) { - vert.flag = SELECT; - } + bke::MutableAttributeAccessor attributes = mesh->attributes_for_write(); + bke::SpanAttributeWriter<bool> select_vert = attributes.lookup_or_add_for_write_span<bool>( + ".select_vert", ATTR_DOMAIN_POINT); + select_vert.span.take_back(len).fill(true); + select_vert.finish(); } static void mesh_add_edges(Mesh *mesh, int len) { + using namespace blender; CustomData edata; int totedge; @@ -1185,13 +1187,18 @@ static void mesh_add_edges(Mesh *mesh, int len) BKE_mesh_runtime_clear_cache(mesh); - const int old_edges_num = mesh->totedge; mesh->totedge = totedge; MutableSpan<MEdge> edges = mesh->edges_for_write(); - for (MEdge &edge : edges.drop_front(old_edges_num)) { - edge.flag = ME_EDGEDRAW | ME_EDGERENDER | SELECT; + for (MEdge &edge : edges.take_back(len)) { + edge.flag = ME_EDGEDRAW | ME_EDGERENDER; } + + bke::MutableAttributeAccessor attributes = mesh->attributes_for_write(); + bke::SpanAttributeWriter<bool> select_edge = attributes.lookup_or_add_for_write_span<bool>( + ".select_edge", ATTR_DOMAIN_EDGE); + select_edge.span.take_back(len).fill(true); + select_edge.finish(); } static void mesh_add_loops(Mesh *mesh, int len) @@ -1223,6 +1230,7 @@ static void mesh_add_loops(Mesh *mesh, int len) static void mesh_add_polys(Mesh *mesh, int len) { + using namespace blender; CustomData pdata; int totpoly; @@ -1245,13 +1253,13 @@ static void mesh_add_polys(Mesh *mesh, int len) BKE_mesh_runtime_clear_cache(mesh); - const int old_polys_num = mesh->totpoly; mesh->totpoly = totpoly; - MutableSpan<MPoly> polys = mesh->polys_for_write(); - for (MPoly &poly : polys.drop_front(old_polys_num)) { - poly.flag = ME_FACE_SEL; - } + bke::MutableAttributeAccessor attributes = mesh->attributes_for_write(); + bke::SpanAttributeWriter<bool> select_poly = attributes.lookup_or_add_for_write_span<bool>( + ".select_poly", ATTR_DOMAIN_FACE); + select_poly.span.take_back(len).fill(true); + select_poly.finish(); } /* -------------------------------------------------------------------- */ |