[Bf-blender-cvs] [1394e14ad9b] refactor-mesh-selection-generic: Merge branch 'master' into refactor-mesh-selection-generic

Hans Goudey noreply at git.blender.org
Wed Aug 31 18:38:40 CEST 2022


Commit: 1394e14ad9be138816c760d1996e3f7388f8c8b4
Author: Hans Goudey
Date:   Wed Aug 31 09:50:19 2022 -0500
Branches: refactor-mesh-selection-generic
https://developer.blender.org/rB1394e14ad9be138816c760d1996e3f7388f8c8b4

Merge branch 'master' into refactor-mesh-selection-generic

===================================================================



===================================================================

diff --cc source/blender/blenkernel/BKE_mesh_legacy_convert.h
index c9c428e0dec,11ee86c62a7..1ce2f0c9dcc
--- a/source/blender/blenkernel/BKE_mesh_legacy_convert.h
+++ b/source/blender/blenkernel/BKE_mesh_legacy_convert.h
@@@ -27,16 -27,16 +27,26 @@@ void BKE_mesh_legacy_convert_hide_layer
   */
  void BKE_mesh_legacy_convert_flags_to_hide_layers(struct Mesh *mesh);
  
 +/**
 + * Convert the selected element attributes to the old flag format for writing.
 + */
 +void BKE_mesh_legacy_convert_selection_layers_to_flags(struct Mesh *mesh);
 +/**
 + * Convert the old selection flags (#SELECT/#ME_FACE_SEL) to the selected element attribute for
 + * reading. Only add the attributes when there are any elements in each domain selected.
 + */
 +void BKE_mesh_legacy_convert_flags_to_selection_layers(struct Mesh *mesh);
 +
+ /**
+  * Move material indices from a generic attribute to #MPoly.
+  */
+ void BKE_mesh_legacy_convert_material_indices_to_mpoly(struct Mesh *mesh);
+ /**
+  * Move material indices from the #MPoly struct to a generic attributes.
+  * Only add the attribute when the indices are not all zero.
+  */
+ void BKE_mesh_legacy_convert_mpoly_to_material_indices(struct Mesh *mesh);
+ 
  /**
   * Recreate #MFace Tessellation.
   *
diff --cc source/blender/blenkernel/intern/customdata.cc
index 65f05d34f51,51edf8308c3..5dfcb3105a2
--- a/source/blender/blenkernel/intern/customdata.cc
+++ b/source/blender/blenkernel/intern/customdata.cc
@@@ -2346,13 -2375,7 +2375,14 @@@ bool CustomData_merge(const CustomData 
  
  static bool attribute_stored_in_bmesh_flag(const StringRef name)
  {
 -  return ELEM(name, ".hide_vert", ".hide_edge", ".hide_poly", "material_index");
 +  return ELEM(name,
 +              ".hide_vert",
 +              ".hide_edge",
 +              ".hide_poly",
 +              ".selection_vert",
 +              ".selection_edge",
-               ".selection_poly");
++              ".selection_poly",
++              "material_index");
  }
  
  static CustomData shallow_copy_remove_non_bmesh_attributes(const CustomData &src)
diff --cc source/blender/blenkernel/intern/mesh.cc
index 8496f37736b,b44a956eec4..caeb1da57d9
--- a/source/blender/blenkernel/intern/mesh.cc
+++ b/source/blender/blenkernel/intern/mesh.cc
@@@ -249,14 -254,9 +254,15 @@@ static void mesh_blend_write(BlendWrite
      Set<std::string> names_to_skip;
      if (!BLO_write_is_undo(writer)) {
        BKE_mesh_legacy_convert_hide_layers_to_flags(mesh);
 +      BKE_mesh_legacy_convert_selection_layers_to_flags(mesh);
+       BKE_mesh_legacy_convert_material_indices_to_mpoly(mesh);
        /* When converting to the old mesh format, don't save redundant attributes. */
 -      names_to_skip.add_multiple_new({".hide_vert", ".hide_edge", ".hide_poly"});
 +      names_to_skip.add_multiple_new({".hide_vert",
 +                                      ".hide_edge",
 +                                      ".hide_poly",
 +                                      ".selection_vert",
 +                                      ".selection_edge",
 +                                      ".selection_poly"});
      }
  
      CustomData_blend_write_prepare(mesh->vdata, vert_layers, names_to_skip);
@@@ -343,7 -343,7 +349,8 @@@ static void mesh_blend_read_data(BlendD
  
    if (!BLO_read_data_is_undo(reader)) {
      BKE_mesh_legacy_convert_flags_to_hide_layers(mesh);
 +    BKE_mesh_legacy_convert_flags_to_selection_layers(mesh);
+     BKE_mesh_legacy_convert_mpoly_to_material_indices(mesh);
    }
  
    /* We don't expect to load normals from files, since they are derived data. */
diff --cc source/blender/blenkernel/intern/mesh_legacy_convert.cc
index bfe7c588ca0,2fc984997b8..345cb38bb07
--- a/source/blender/blenkernel/intern/mesh_legacy_convert.cc
+++ b/source/blender/blenkernel/intern/mesh_legacy_convert.cc
@@@ -963,89 -963,42 +964,129 @@@ void BKE_mesh_legacy_convert_flags_to_h
  }
  
  /** \} */
 +
+ /* -------------------------------------------------------------------- */
+ /** \name Material Index Conversion
+  * \{ */
+ 
+ void BKE_mesh_legacy_convert_material_indices_to_mpoly(Mesh *mesh)
+ {
+   using namespace blender;
+   using namespace blender::bke;
+   const AttributeAccessor attributes = mesh_attributes(*mesh);
+   MutableSpan<MPoly> polys(mesh->mpoly, mesh->totpoly);
+   const VArray<int> material_indices = attributes.lookup_or_default<int>(
+       "material_index", ATTR_DOMAIN_FACE, 0);
+   threading::parallel_for(polys.index_range(), 4096, [&](IndexRange range) {
+     for (const int i : range) {
+       polys[i].mat_nr = material_indices[i];
+     }
+   });
+ }
+ 
+ void BKE_mesh_legacy_convert_mpoly_to_material_indices(Mesh *mesh)
+ {
+   using namespace blender;
+   using namespace blender::bke;
+   MutableAttributeAccessor attributes = mesh_attributes_for_write(*mesh);
+   const Span<MPoly> polys(mesh->mpoly, mesh->totpoly);
+   if (std::any_of(
+           polys.begin(), polys.end(), [](const MPoly &poly) { return poly.mat_nr != 0; })) {
+     SpanAttributeWriter<int> material_indices = attributes.lookup_or_add_for_write_only_span<int>(
+         "material_index", ATTR_DOMAIN_FACE);
+     threading::parallel_for(polys.index_range(), 4096, [&](IndexRange range) {
+       for (const int i : range) {
+         material_indices.span[i] = polys[i].mat_nr;
+       }
+     });
+     material_indices.finish();
+   }
+ }
+ 
+ /** \} */
++
 +/* -------------------------------------------------------------------- */
 +/** \name Selection Attribute and Legacy Flag Conversion
 + * \{ */
 +
 +void BKE_mesh_legacy_convert_selection_layers_to_flags(Mesh *mesh)
 +{
 +  using namespace blender;
 +  using namespace blender::bke;
 +  const AttributeAccessor attributes = mesh_attributes(*mesh);
 +
 +  MutableSpan<MVert> verts(mesh->mvert, mesh->totvert);
 +  const VArray<bool> selection_vert = attributes.lookup_or_default<bool>(
 +      ".selection_vert", ATTR_DOMAIN_POINT, false);
 +  threading::parallel_for(verts.index_range(), 4096, [&](IndexRange range) {
 +    for (const int i : range) {
 +      SET_FLAG_FROM_TEST(verts[i].flag, selection_vert[i], SELECT);
 +    }
 +  });
 +
 +  MutableSpan<MEdge> edges(mesh->medge, mesh->totedge);
 +  const VArray<bool> selection_edge = attributes.lookup_or_default<bool>(
 +      ".selection_edge", ATTR_DOMAIN_EDGE, false);
 +  threading::parallel_for(edges.index_range(), 4096, [&](IndexRange range) {
 +    for (const int i : range) {
 +      SET_FLAG_FROM_TEST(edges[i].flag, selection_edge[i], SELECT);
 +    }
 +  });
 +
 +  MutableSpan<MPoly> polys(mesh->mpoly, mesh->totpoly);
 +  const VArray<bool> selection_poly = attributes.lookup_or_default<bool>(
 +      ".selection_poly", ATTR_DOMAIN_FACE, false);
 +  threading::parallel_for(polys.index_range(), 4096, [&](IndexRange range) {
 +    for (const int i : range) {
 +      SET_FLAG_FROM_TEST(polys[i].flag, selection_poly[i], ME_FACE_SEL);
 +    }
 +  });
 +}
 +
 +void BKE_mesh_legacy_convert_flags_to_selection_layers(Mesh *mesh)
 +{
 +  using namespace blender;
 +  using namespace blender::bke;
 +  MutableAttributeAccessor attributes = mesh_attributes_for_write(*mesh);
 +
 +  const Span<MVert> verts(mesh->mvert, mesh->totvert);
 +  if (std::any_of(
 +          verts.begin(), verts.end(), [](const MVert &vert) { return vert.flag & SELECT; })) {
 +    SpanAttributeWriter<bool> selection_vert = attributes.lookup_or_add_for_write_only_span<bool>(
 +        ".selection_vert", ATTR_DOMAIN_POINT);
 +    threading::parallel_for(verts.index_range(), 4096, [&](IndexRange range) {
 +      for (const int i : range) {
 +        selection_vert.span[i] = verts[i].flag & SELECT;
 +      }
 +    });
 +    selection_vert.finish();
 +  }
 +
 +  const Span<MEdge> edges(mesh->medge, mesh->totedge);
 +  if (std::any_of(
 +          edges.begin(), edges.end(), [](const MEdge &edge) { return edge.flag & SELECT; })) {
 +    SpanAttributeWriter<bool> selection_edge = attributes.lookup_or_add_for_write_only_span<bool>(
 +        ".selection_edge", ATTR_DOMAIN_EDGE);
 +    threading::parallel_for(edges.index_range(), 4096, [&](IndexRange range) {
 +      for (const int i : range) {
 +        selection_edge.span[i] = edges[i].flag & SELECT;
 +      }
 +    });
 +    selection_edge.finish();
 +  }
 +
 +  const Span<MPoly> polys(mesh->mpoly, mesh->totpoly);
 +  if (std::any_of(
 +          polys.begin(), polys.end(), [](const MPoly &poly) { return poly.flag & ME_FACE_SEL; })) {
 +    SpanAttributeWriter<bool> selection_poly = attributes.lookup_or_add_for_write_only_span<bool>(
 +        ".selection_poly", ATTR_DOMAIN_FACE);
 +    threading::parallel_for(polys.index_range(), 4096, [&](IndexRange range) {
 +      for (const int i : range) {
 +        selection_poly.span[i] = polys[i].flag & ME_FACE_SEL;
 +      }
 +    });
 +    selection_poly.finish();
 +  }
 +}
 +
 +/** \} */
diff --cc source/blender/bmesh/intern/bmesh_mesh_convert.cc
index cef41aef3ea,47ad5080451..884fbc189ac
--- a/source/blender/bmesh/intern/bmesh_mesh_convert.cc
+++ b/source/blender/bmesh/intern/bmesh_mesh_convert.cc
@@@ -357,19 -357,14 +357,20 @@@ void BM_mesh_bm_from_me(BMesh *bm, cons
                                             CustomData_get_offset(&bm->vdata, CD_SHAPE_KEYINDEX) :
                                             -1;
  
-   const bool *hide_vert = (const bool *)CustomData_get_layer_named(
-       &me->vdata, CD_PROP_BOOL, ".hide_vert");
-   const bool *hide_edge = (const bool *)CustomData_get_layer_named(
-       &me->edata, CD_PROP_BOOL, ".hide_edge");
-   const bool *hide_poly = (const bool *)CustomData_get_layer_named(
-       &me->pdata, CD_PROP_BOOL, ".hide_poly");
- 
 +  const bool *selection_vert = (const bool *)CustomData_get_layer_named(
 +      &me->vdata, CD_PROP_BOOL, ".selection_vert");
 +  const bool *selection_edge = (const bool *)CustomData_get_layer_named(
 +      &me->edata, CD_PROP_BOOL, ".selection_edge");
 +  const bool *selection_poly = (const bool *)CustomData_get_layer_named(
 +      &me->pdata, CD_PROP_BOOL, ".selection_poly");
+   const bool *hide_vert = (const bool *)CustomData_get_layer_named(
+       &me->vdata, CD_PROP_BOOL, ".hide_vert");
+   const bool *hide_edge = (const bool *)CustomData_get_layer_named(
+       &me->edata,

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list