[Bf-blender-cvs] [060a5341419] master: Mesh: Move sculpt face sets to a generic attribute

Hans Goudey noreply at git.blender.org
Fri Sep 23 15:19:56 CEST 2022


Commit: 060a5341419412fd7996cf99a56db1f581a4c30c
Author: Hans Goudey
Date:   Fri Sep 23 08:19:40 2022 -0500
Branches: master
https://developer.blender.org/rB060a5341419412fd7996cf99a56db1f581a4c30c

Mesh: Move sculpt face sets to a generic attribute

Similar to the other refactors from T95965, this commit moves sculpt
face sets to use a generic integer attribute named `".sculpt_face_set"`.
This makes face sets accessible in the Python API.

The attribute is not visible in the attributes list or the spreadsheet
because it is meant for internal use, though that could be an option
in the future along with other similar attributes. Currently the change
is small, but in the future this could simplify code by allowing use
of more generic attribute APIs.

Differential Revision: https://developer.blender.org/D16045

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

M	source/blender/blenkernel/BKE_mesh_legacy_convert.h
M	source/blender/blenkernel/intern/customdata.cc
M	source/blender/blenkernel/intern/mesh.cc
M	source/blender/blenkernel/intern/mesh_legacy_convert.cc
M	source/blender/blenkernel/intern/mesh_remesh_voxel.cc
M	source/blender/blenkernel/intern/paint.cc
M	source/blender/blenkernel/intern/pbvh.c
M	source/blender/blenloader/intern/versioning_400.cc
M	source/blender/bmesh/intern/bmesh_log.c
M	source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_sculpt_data.cc
M	source/blender/editors/mesh/editmesh_mask_extract.c
M	source/blender/editors/mesh/meshtools.cc
M	source/blender/editors/sculpt_paint/paint_mask.c
M	source/blender/editors/sculpt_paint/sculpt_dyntopo.c
M	source/blender/editors/sculpt_paint/sculpt_face_set.cc
M	source/blender/editors/sculpt_paint/sculpt_undo.c
M	source/blender/editors/space_view3d/view3d_draw.c
M	source/blender/makesdna/DNA_customdata_types.h

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

diff --git a/source/blender/blenkernel/BKE_mesh_legacy_convert.h b/source/blender/blenkernel/BKE_mesh_legacy_convert.h
index e67aec0b9ce..b0bc5fe8efe 100644
--- a/source/blender/blenkernel/BKE_mesh_legacy_convert.h
+++ b/source/blender/blenkernel/BKE_mesh_legacy_convert.h
@@ -17,6 +17,15 @@ struct CustomData;
 struct Mesh;
 struct MFace;
 
+/**
+ * Move face sets to the legacy type from a generic type.
+ */
+void BKE_mesh_legacy_face_set_from_generic(struct Mesh *mesh);
+/**
+ * Copy face sets to the generic data type from the legacy type.
+ */
+void BKE_mesh_legacy_face_set_to_generic(struct Mesh *mesh);
+
 /**
  * Copy bevel weights from separate layers into vertices and edges.
  */
diff --git a/source/blender/blenkernel/intern/customdata.cc b/source/blender/blenkernel/intern/customdata.cc
index 0589e1ef8c7..892b1599bce 100644
--- a/source/blender/blenkernel/intern/customdata.cc
+++ b/source/blender/blenkernel/intern/customdata.cc
@@ -1967,7 +1967,7 @@ static const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = {
     {sizeof(short[4][3]), "", 0, nullptr, nullptr, nullptr, nullptr, layerSwap_flnor, nullptr},
     /* 41: CD_CUSTOMLOOPNORMAL */
     {sizeof(short[2]), "vec2s", 1, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr},
-    /* 42: CD_SCULPT_FACE_SETS */
+    /* 42: CD_SCULPT_FACE_SETS */ /* DEPRECATED */
     {sizeof(int), "", 0, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr},
     /* 43: CD_LOCATION */
     {sizeof(float[3]), "vec3f", 1, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr},
@@ -2127,8 +2127,7 @@ const CustomData_MeshMasks CD_MASK_MESH = {
     /* emask */ (CD_MASK_MEDGE | CD_MASK_FREESTYLE_EDGE | CD_MASK_PROP_ALL | CD_MASK_BWEIGHT),
     /* fmask */ 0,
     /* pmask */
-    (CD_MASK_MPOLY | CD_MASK_FACEMAP | CD_MASK_FREESTYLE_FACE | CD_MASK_PROP_ALL |
-     CD_MASK_SCULPT_FACE_SETS),
+    (CD_MASK_MPOLY | CD_MASK_FACEMAP | CD_MASK_FREESTYLE_FACE | CD_MASK_PROP_ALL),
     /* lmask */
     (CD_MASK_MLOOP | CD_MASK_MDISPS | CD_MASK_MLOOPUV | CD_MASK_CUSTOMLOOPNORMAL |
      CD_MASK_GRID_PAINT_MASK | CD_MASK_PROP_ALL),
@@ -2140,8 +2139,7 @@ const CustomData_MeshMasks CD_MASK_DERIVEDMESH = {
     /* emask */ (CD_MASK_ORIGINDEX | CD_MASK_FREESTYLE_EDGE | CD_MASK_BWEIGHT | CD_MASK_PROP_ALL),
     /* fmask */ (CD_MASK_ORIGINDEX | CD_MASK_ORIGSPACE | CD_MASK_PREVIEW_MCOL | CD_MASK_TANGENT),
     /* pmask */
-    (CD_MASK_ORIGINDEX | CD_MASK_FREESTYLE_FACE | CD_MASK_FACEMAP | CD_MASK_PROP_ALL |
-     CD_MASK_SCULPT_FACE_SETS),
+    (CD_MASK_ORIGINDEX | CD_MASK_FREESTYLE_FACE | CD_MASK_FACEMAP | CD_MASK_PROP_ALL),
     /* lmask */
     (CD_MASK_MLOOPUV | CD_MASK_CUSTOMLOOPNORMAL | CD_MASK_PREVIEW_MLOOPCOL |
      CD_MASK_ORIGSPACE_MLOOP | CD_MASK_PROP_ALL), /* XXX MISSING CD_MASK_MLOOPTANGENT ? */
@@ -2152,7 +2150,7 @@ const CustomData_MeshMasks CD_MASK_BMESH = {
     /* emask */ (CD_MASK_BWEIGHT | CD_MASK_CREASE | CD_MASK_FREESTYLE_EDGE | CD_MASK_PROP_ALL),
     /* fmask */ 0,
     /* pmask */
-    (CD_MASK_FREESTYLE_FACE | CD_MASK_FACEMAP | CD_MASK_PROP_ALL | CD_MASK_SCULPT_FACE_SETS),
+    (CD_MASK_FREESTYLE_FACE | CD_MASK_FACEMAP | CD_MASK_PROP_ALL),
     /* lmask */
     (CD_MASK_MDISPS | CD_MASK_MLOOPUV | CD_MASK_CUSTOMLOOPNORMAL | CD_MASK_GRID_PAINT_MASK |
      CD_MASK_PROP_ALL),
@@ -2171,7 +2169,7 @@ const CustomData_MeshMasks CD_MASK_EVERYTHING = {
      CD_MASK_PROP_ALL),
     /* pmask */
     (CD_MASK_MPOLY | CD_MASK_BM_ELEM_PYPTR | CD_MASK_ORIGINDEX | CD_MASK_FACEMAP |
-     CD_MASK_FREESTYLE_FACE | CD_MASK_PROP_ALL | CD_MASK_SCULPT_FACE_SETS),
+     CD_MASK_FREESTYLE_FACE | CD_MASK_PROP_ALL),
     /* lmask */
     (CD_MASK_MLOOP | CD_MASK_BM_ELEM_PYPTR | CD_MASK_MDISPS | CD_MASK_NORMAL | CD_MASK_MLOOPUV |
      CD_MASK_CUSTOMLOOPNORMAL | CD_MASK_MLOOPTANGENT | CD_MASK_PREVIEW_MLOOPCOL |
diff --git a/source/blender/blenkernel/intern/mesh.cc b/source/blender/blenkernel/intern/mesh.cc
index 7da9acc3cf6..6b1179b0cc9 100644
--- a/source/blender/blenkernel/intern/mesh.cc
+++ b/source/blender/blenkernel/intern/mesh.cc
@@ -252,6 +252,7 @@ static void mesh_blend_write(BlendWriter *writer, ID *id, const void *id_address
       BKE_mesh_legacy_convert_hide_layers_to_flags(mesh);
       BKE_mesh_legacy_convert_material_indices_to_mpoly(mesh);
       BKE_mesh_legacy_bevel_weight_from_layers(mesh);
+      BKE_mesh_legacy_face_set_from_generic(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", "material_index"});
 
diff --git a/source/blender/blenkernel/intern/mesh_legacy_convert.cc b/source/blender/blenkernel/intern/mesh_legacy_convert.cc
index 627c0057a28..efbea35bfcf 100644
--- a/source/blender/blenkernel/intern/mesh_legacy_convert.cc
+++ b/source/blender/blenkernel/intern/mesh_legacy_convert.cc
@@ -917,6 +917,33 @@ void BKE_mesh_add_mface_layers(CustomData *fdata, CustomData *ldata, int total)
 
 /** \} */
 
+/* -------------------------------------------------------------------- */
+/** \name Face Set Conversion
+ * \{ */
+
+void BKE_mesh_legacy_face_set_from_generic(Mesh *mesh)
+{
+  using namespace blender;
+  for (CustomDataLayer &layer : MutableSpan(mesh->pdata.layers, mesh->pdata.totlayer)) {
+    if (StringRef(layer.name) == ".sculpt_face_set") {
+      layer.type = CD_SCULPT_FACE_SETS;
+    }
+  }
+}
+
+void BKE_mesh_legacy_face_set_to_generic(Mesh *mesh)
+{
+  using namespace blender;
+  for (CustomDataLayer &layer : MutableSpan(mesh->pdata.layers, mesh->pdata.totlayer)) {
+    if (layer.type == CD_SCULPT_FACE_SETS) {
+      BLI_strncpy(layer.name, ".sculpt_face_set", sizeof(layer.name));
+      layer.type = CD_PROP_INT32;
+    }
+  }
+}
+
+/** \} */
+
 /* -------------------------------------------------------------------- */
 /** \name Bevel Weight Conversion
  * \{ */
diff --git a/source/blender/blenkernel/intern/mesh_remesh_voxel.cc b/source/blender/blenkernel/intern/mesh_remesh_voxel.cc
index a77879fb573..12f42dbc4ec 100644
--- a/source/blender/blenkernel/intern/mesh_remesh_voxel.cc
+++ b/source/blender/blenkernel/intern/mesh_remesh_voxel.cc
@@ -24,6 +24,7 @@
 #include "DNA_meshdata_types.h"
 
 #include "BKE_attribute.h"
+#include "BKE_attribute.hh"
 #include "BKE_bvhutils.h"
 #include "BKE_customdata.h"
 #include "BKE_editmesh.h"
@@ -318,24 +319,28 @@ void BKE_mesh_remesh_reproject_paint_mask(Mesh *target, const Mesh *source)
 
 void BKE_remesh_reproject_sculpt_face_sets(Mesh *target, const Mesh *source)
 {
+  using namespace blender;
+  using namespace blender::bke;
+  const AttributeAccessor src_attributes = source->attributes();
+  MutableAttributeAccessor dst_attributes = target->attributes_for_write();
   const MPoly *target_polys = (const MPoly *)CustomData_get_layer(&target->pdata, CD_MPOLY);
   const MVert *target_verts = (const MVert *)CustomData_get_layer(&target->vdata, CD_MVERT);
   const MLoop *target_loops = (const MLoop *)CustomData_get_layer(&target->ldata, CD_MLOOP);
-  const int *source_face_sets = (const int *)CustomData_get_layer(&source->pdata,
-                                                                  CD_SCULPT_FACE_SETS);
-  if (source_face_sets == nullptr) {
-    return;
-  }
 
-  int *target_face_sets;
-  if (CustomData_has_layer(&target->pdata, CD_SCULPT_FACE_SETS)) {
-    target_face_sets = (int *)CustomData_get_layer(&target->pdata, CD_SCULPT_FACE_SETS);
+  const VArray<int> src_face_sets = src_attributes.lookup<int>(".sculpt_face_set",
+                                                               ATTR_DOMAIN_FACE);
+  if (!src_face_sets) {
+    return;
   }
-  else {
-    target_face_sets = (int *)CustomData_add_layer(
-        &target->pdata, CD_SCULPT_FACE_SETS, CD_CONSTRUCT, nullptr, target->totpoly);
+  SpanAttributeWriter<int> dst_face_sets = dst_attributes.lookup_or_add_for_write_only_span<int>(
+      ".sculpt_face_set", ATTR_DOMAIN_FACE);
+  if (!dst_face_sets) {
+    return;
   }
 
+  const VArraySpan<int> src(src_face_sets);
+  MutableSpan<int> dst = dst_face_sets.span;
+
   const MLoopTri *looptri = BKE_mesh_runtime_looptri_ensure(source);
   BVHTreeFromMesh bvhtree = {nullptr};
   BKE_bvhtree_from_mesh_get(&bvhtree, source, BVHTREE_FROM_LOOPTRI, 2);
@@ -349,13 +354,14 @@ void BKE_remesh_reproject_sculpt_face_sets(Mesh *target, const Mesh *source)
     BKE_mesh_calc_poly_center(mpoly, &target_loops[mpoly->loopstart], target_verts, from_co);
     BLI_bvhtree_find_nearest(bvhtree.tree, from_co, &nearest, bvhtree.nearest_callback, &bvhtree);
     if (nearest.index != -1) {
-      target_face_sets[i] = source_face_sets[looptri[nearest.index].poly];
+      dst[i] = src[looptri[nearest.index].poly];
     }
     else {
-      target_face_sets[i] = 1;
+      dst[i] = 1;
     }
   }
   free_bvhtree_from_mesh(&bvhtree);
+  dst_face_sets.finish();
 }
 
 void BKE_remesh_reproject_vertex_paint(Mesh *target, const Mesh *source)
diff --git a/source/blender/blenkernel/intern/paint.cc b/source/blender/blenkernel/intern/paint.cc
index 0181c6e7eac..7475d2b5144 100644
--- a/source/blender/blenkernel/intern/paint.cc
+++ b/source/blender/blenkernel/intern/paint.cc
@@ -1739,7 +1739,8 @@ static void sculpt_update_object(
 
   /* Sculpt Face Sets. */
   if (use_face_sets) {
-    ss->face_sets = static_cast<int *>(CustomData_get_layer(&me->pdata, CD_SCULPT_FACE_SETS));
+    ss->face_sets = static_cast<int *>(
+        CustomData_get_layer_named(&me->pdata, CD_PROP_INT32, ".sculpt_face_set"));
   }
   else {
     ss->face_sets = nullptr;
@@ -1966,22 +1967,17 @@ int *BKE_sculpt_face_sets_ensure(Mesh *mesh)
 {
   using namespace blender;
   using namespace blender::bke;
-  if (CustomData_has_layer(&mesh->pdata, CD_SCULPT_FACE_SETS)) {
-    return static_cast<int *>(CustomData_get_layer(&mesh->pdata, CD_SCULPT_FACE_SETS));
+  MutableAttributeAccessor attributes = mesh->attributes_for_write();
+  if (!attributes.contains(".sculpt_face_set")) {
+    SpanAttributeWriter<int> face_sets = attributes.lookup_or_add_for_write_only_span<int>(
+        ".sculpt_face_set", ATTR_DOMAIN_FACE);
+    face_sets.span.fill(1);
+    mesh->face_sets_color_default = 1;
+    face_sets.finish();
   }
 
-  const AttributeAccessor attributes = mesh->attributes_for_write();
-  const VAr

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list