[Bf-blender-cvs] [99530ef4ed7] master: Sculpt: Face Sets Init operator

Pablo Dobarro noreply at git.blender.org
Thu Mar 26 16:20:45 CET 2020


Commit: 99530ef4ed70a479523d8e99a40fce24eb9ba658
Author: Pablo Dobarro
Date:   Sat Mar 21 23:24:53 2020 +0100
Branches: master
https://developer.blender.org/rB99530ef4ed70a479523d8e99a40fce24eb9ba658

Sculpt: Face Sets Init operator

This operator initializes all face sets in the sculpt at once using
different mesh properties. It can create face sets by mesh connectivity,
material slots, face normals, UV seams, creases, sharp edges, bevel
weights and face maps.

For properties that are already in the faces, this is implemented as a
loop. Properties that depend on edge attributes use a similar operation
to sculpt flood fill, but using face adjacency instead of edge vertex
connectivity.

As Multires also stores the face sets in the base mesh, this should work
in the face sets Multires implementation without any changes.

This is implemented as a separate operator as this resets the visibility
and creates all face sets at once, while the create face set operator
creates a single face sets, leaving the rest of the face sets in the
mesh as they are.

Reviewed By: jbakker

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

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

M	release/scripts/startup/bl_ui/space_view3d.py
M	source/blender/editors/sculpt_paint/sculpt.c

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

diff --git a/release/scripts/startup/bl_ui/space_view3d.py b/release/scripts/startup/bl_ui/space_view3d.py
index 4f4272c879a..db128c14cf9 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -3135,6 +3135,10 @@ class VIEW3D_MT_face_sets(Menu):
         op = layout.operator("sculpt.face_sets_create", text='Face Set From Visible')
         op.mode = 'VISIBLE'
 
+        layout.separator()
+        
+        layout.menu("VIEW3D_MT_face_sets_init", text="Init Face Sets")
+
         layout.separator()
 
         op = layout.operator("sculpt.face_set_change_visibility", text='Invert Visible Face Sets')
@@ -3169,6 +3173,37 @@ class VIEW3D_MT_sculpt_set_pivot(Menu):
         props = layout.operator("sculpt.set_pivot_position", text="Pivot to Surface Under Cursor")
         props.mode = 'SURFACE'
 
+class VIEW3D_MT_face_sets_init(Menu):
+    bl_label = "Face Sets Init"
+
+    def draw(self, _context):
+        layout = self.layout
+
+        op = layout.operator("sculpt.face_sets_init", text='By Loose Parts')
+        op.mode = 'LOOSE_PARTS'
+
+        op = layout.operator("sculpt.face_sets_init", text='By Materials')
+        op.mode = 'MATERIALS'
+
+        op = layout.operator("sculpt.face_sets_init", text='By Normals')
+        op.mode = 'NORMALS'
+
+        op = layout.operator("sculpt.face_sets_init", text='By UV Seams')
+        op.mode = 'UV_SEAMS'
+
+        op = layout.operator("sculpt.face_sets_init", text='By Edge Creases')
+        op.mode = 'CREASES'
+
+        op = layout.operator("sculpt.face_sets_init", text='By Edge Bevel Weight')
+        op.mode = 'BEVEL_WEIGHT'
+
+        op = layout.operator("sculpt.face_sets_init", text='By Sharp Edges')
+        op.mode = 'SHARP_EDGES'
+
+        op = layout.operator("sculpt.face_sets_init", text='By Face Maps')
+        op.mode = 'FACE_MAPS'
+
+
 
 class VIEW3D_MT_particle(Menu):
     bl_label = "Particle"
@@ -7335,6 +7370,7 @@ classes = (
     VIEW3D_MT_sculpt_set_pivot,
     VIEW3D_MT_mask,
     VIEW3D_MT_face_sets,
+    VIEW3D_MT_face_sets_init,
     VIEW3D_MT_particle,
     VIEW3D_MT_particle_context_menu,
     VIEW3D_MT_particle_showhide,
diff --git a/source/blender/editors/sculpt_paint/sculpt.c b/source/blender/editors/sculpt_paint/sculpt.c
index a99b6e44d18..f6ca36668bc 100644
--- a/source/blender/editors/sculpt_paint/sculpt.c
+++ b/source/blender/editors/sculpt_paint/sculpt.c
@@ -11180,6 +11180,349 @@ static void SCULPT_OT_face_sets_create(wmOperatorType *ot)
       ot->srna, "mode", prop_sculpt_face_set_create_types, SCULPT_FACE_SET_MASKED, "Mode", "");
 }
 
+typedef enum eSculptFaceSetsInitMode {
+  SCULPT_FACE_SETS_FROM_LOOSE_PARTS = 0,
+  SCULPT_FACE_SETS_FROM_MATERIALS = 1,
+  SCULPT_FACE_SETS_FROM_NORMALS = 2,
+  SCULPT_FACE_SETS_FROM_UV_SEAMS = 3,
+  SCULPT_FACE_SETS_FROM_CREASES = 4,
+  SCULPT_FACE_SETS_FROM_SHARP_EDGES = 5,
+  SCULPT_FACE_SETS_FROM_BEVEL_WEIGHT = 6,
+  SCULPT_FACE_SETS_FROM_FACE_MAPS = 7,
+} eSculptFaceSetsInitMode;
+
+static EnumPropertyItem prop_sculpt_face_sets_init_types[] = {
+    {
+        SCULPT_FACE_SETS_FROM_LOOSE_PARTS,
+        "LOOSE_PARTS",
+        0,
+        "Face Sets From Loose Parts",
+        "Create a Face Set per loose part in the mesh",
+    },
+    {
+        SCULPT_FACE_SETS_FROM_MATERIALS,
+        "MATERIALS",
+        0,
+        "Face Sets From Material Slots",
+        "Create a Face Set per Material Slot",
+    },
+    {
+        SCULPT_FACE_SETS_FROM_NORMALS,
+        "NORMALS",
+        0,
+        "Face Sets From Mesh Normals",
+        "Create Face Sets for Faces that have similar normal",
+    },
+    {
+        SCULPT_FACE_SETS_FROM_UV_SEAMS,
+        "UV_SEAMS",
+        0,
+        "Face Sets From UV Seams",
+        "Create Face Sets using UV Seams as boundaries",
+    },
+    {
+        SCULPT_FACE_SETS_FROM_CREASES,
+        "CREASES",
+        0,
+        "Face Sets From Edge Creases",
+        "Create Face Sets using Edge Creases as boundaries",
+    },
+    {
+        SCULPT_FACE_SETS_FROM_BEVEL_WEIGHT,
+        "BEVEL_WEIGHT",
+        0,
+        "Face Sets From Bevel Weight",
+        "Create Face Sets using Bevel Weights as boundaries",
+    },
+    {
+        SCULPT_FACE_SETS_FROM_SHARP_EDGES,
+        "SHARP_EDGES",
+        0,
+        "Face Sets From Sharp Edges",
+        "Create Face Sets using Sharp Edges as boundaries",
+    },
+    {
+        SCULPT_FACE_SETS_FROM_FACE_MAPS,
+        "FACE_MAPS",
+        0,
+        "Face Sets From Face Maps",
+        "Create a Face Set per Face Map",
+    },
+    {0, NULL, 0, NULL, NULL},
+};
+
+typedef bool (*face_sets_flood_fill_test)(
+    BMesh *bm, BMFace *from_f, BMEdge *from_e, BMFace *to_f, const float threshold);
+
+static bool sculpt_face_sets_init_loose_parts_test(BMesh *UNUSED(bm),
+                                                   BMFace *UNUSED(from_f),
+                                                   BMEdge *UNUSED(from_e),
+                                                   BMFace *UNUSED(to_f),
+                                                   const float UNUSED(threshold))
+{
+  return true;
+}
+
+static bool sculpt_face_sets_init_normals_test(
+    BMesh *UNUSED(bm), BMFace *from_f, BMEdge *UNUSED(from_e), BMFace *to_f, const float threshold)
+{
+  return fabsf(dot_v3v3(from_f->no, to_f->no)) > threshold;
+}
+
+static bool sculpt_face_sets_init_uv_seams_test(BMesh *UNUSED(bm),
+                                                BMFace *UNUSED(from_f),
+                                                BMEdge *from_e,
+                                                BMFace *UNUSED(to_f),
+                                                const float UNUSED(threshold))
+{
+  return !BM_elem_flag_test(from_e, BM_ELEM_SEAM);
+}
+
+static bool sculpt_face_sets_init_crease_test(
+    BMesh *bm, BMFace *UNUSED(from_f), BMEdge *from_e, BMFace *UNUSED(to_f), const float threshold)
+{
+  return BM_elem_float_data_get(&bm->edata, from_e, CD_CREASE) < threshold;
+}
+
+static bool sculpt_face_sets_init_bevel_weight_test(
+    BMesh *bm, BMFace *UNUSED(from_f), BMEdge *from_e, BMFace *UNUSED(to_f), const float threshold)
+{
+  return BM_elem_float_data_get(&bm->edata, from_e, CD_BWEIGHT) < threshold;
+}
+
+static bool sculpt_face_sets_init_sharp_edges_test(BMesh *UNUSED(bm),
+                                                   BMFace *UNUSED(from_f),
+                                                   BMEdge *from_e,
+                                                   BMFace *UNUSED(to_f),
+                                                   const float UNUSED(threshold))
+{
+  return BM_elem_flag_test(from_e, BM_ELEM_SMOOTH);
+}
+
+static void sculpt_face_sets_init_flood_fill(Object *ob,
+                                             face_sets_flood_fill_test test,
+                                             const float threshold)
+{
+  SculptSession *ss = ob->sculpt;
+  Mesh *mesh = ob->data;
+  BMesh *bm;
+  const BMAllocTemplate allocsize = BMALLOC_TEMPLATE_FROM_ME(mesh);
+  bm = BM_mesh_create(&allocsize,
+                      &((struct BMeshCreateParams){
+                          .use_toolflags = true,
+                      }));
+
+  BM_mesh_bm_from_me(bm,
+                     mesh,
+                     (&(struct BMeshFromMeshParams){
+                         .calc_face_normal = true,
+                     }));
+
+  bool *visited_faces = MEM_callocN(sizeof(bool) * mesh->totpoly, "visited faces");
+  const int totfaces = mesh->totpoly;
+
+  int *face_sets = ss->face_sets;
+
+  BM_mesh_elem_table_init(bm, BM_FACE);
+  BM_mesh_elem_table_ensure(bm, BM_FACE);
+
+  int next_face_set = 1;
+
+  for (int i = 0; i < totfaces; i++) {
+    if (!visited_faces[i]) {
+      GSQueue *queue;
+      queue = BLI_gsqueue_new(sizeof(int));
+
+      face_sets[i] = next_face_set;
+      visited_faces[i] = true;
+      BLI_gsqueue_push(queue, &i);
+
+      while (!BLI_gsqueue_is_empty(queue)) {
+        int from_f;
+        BLI_gsqueue_pop(queue, &from_f);
+
+        BMFace *f, *f_neighbor;
+        BMEdge *ed;
+        BMIter iter_a, iter_b;
+
+        f = BM_face_at_index(bm, from_f);
+
+        BM_ITER_ELEM (ed, &iter_a, f, BM_EDGES_OF_FACE) {
+          BM_ITER_ELEM (f_neighbor, &iter_b, ed, BM_FACES_OF_EDGE) {
+            if (f_neighbor != f) {
+              int neighbor_face_index = BM_elem_index_get(f_neighbor);
+              if (!visited_faces[neighbor_face_index]) {
+                if (test(bm, f, ed, f_neighbor, threshold)) {
+                  face_sets[neighbor_face_index] = next_face_set;
+                  visited_faces[neighbor_face_index] = true;
+                  BLI_gsqueue_push(queue, &neighbor_face_index);
+                }
+              }
+            }
+          }
+        }
+      }
+
+      next_face_set += 1;
+
+      BLI_gsqueue_free(queue);
+    }
+  }
+
+  MEM_SAFE_FREE(visited_faces);
+
+  BM_mesh_free(bm);
+}
+
+static void sculpt_face_sets_init_loop(Object *ob, const int mode)
+{
+  Mesh *mesh = ob->data;
+  SculptSession *ss = ob->sculpt;
+  BMesh *bm;
+  const BMAllocTemplate allocsize = BMALLOC_TEMPLATE_FROM_ME(mesh);
+  bm = BM_mesh_create(&allocsize,
+                      &((struct BMeshCreateParams){
+                          .use_toolflags = true,
+                      }));
+
+  BM_mesh_bm_from_me(bm,
+                     mesh,
+                     (&(struct BMeshFromMeshParams){
+                         .calc_face_normal = true,
+                     }));
+  BMIter iter;
+  BMFace *f;
+
+  const int cd_fmaps_offset = CustomData_get_offset(&bm->pdata, CD_FACEMAP);
+
+  BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
+    if (mode == SCULPT_FACE_SETS_FROM_MATERIALS) {
+      ss->face_sets[BM_elem_index_get(f)] = (int)(f->mat_nr + 1);
+    }
+    else if (mode == SCULPT_FACE_SETS_FROM_FACE_MAPS) {
+      if (cd_fmaps_offset != -1) {
+        ss->face_sets[BM_elem_index_get(f)] = BM_ELEM_CD_GET_INT(f, cd_fmaps_offset) + 2;
+      }
+      else {
+        ss->face_sets[BM_elem_index_get(f)] = 1;
+      }
+    }
+  }
+  BM_mesh_free(bm);
+}
+
+static int sculpt_face_set_init_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
+{
+  Object *ob = CTX_data_a

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list