[Bf-blender-cvs] [0b2aee58419] temp_bmesh_multires: Sculpt dyntopo: Fix a few issues from last commit

Joseph Eagar noreply at git.blender.org
Wed Sep 15 11:51:23 CEST 2021


Commit: 0b2aee5841916806d9787d5e072d92d00b7c590c
Author: Joseph Eagar
Date:   Wed Sep 15 02:50:32 2021 -0700
Branches: temp_bmesh_multires
https://developer.blender.org/rB0b2aee5841916806d9787d5e072d92d00b7c590c

Sculpt dyntopo: Fix a few issues from last commit

* Fixed crashing on entering sculpt mode
* Fixed transitioning between sculpt and
  another undo system type sometimes
  corrupting stack.

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

M	source/blender/editors/include/ED_object.h
M	source/blender/editors/sculpt_paint/sculpt.c
M	source/blender/editors/sculpt_paint/sculpt_dyntopo.c
M	source/blender/editors/sculpt_paint/sculpt_intern.h
M	source/blender/editors/sculpt_paint/sculpt_undo.c
M	source/blender/editors/util/ed_util.c

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

diff --git a/source/blender/editors/include/ED_object.h b/source/blender/editors/include/ED_object.h
index a9cf04e1ad7..3f02be64294 100644
--- a/source/blender/editors/include/ED_object.h
+++ b/source/blender/editors/include/ED_object.h
@@ -266,7 +266,8 @@ void ED_object_sculptmode_enter_ex(struct Main *bmain,
                                    struct Scene *scene,
                                    struct Object *ob,
                                    const bool force_dyntopo,
-                                   struct ReportList *reports);
+                                   struct ReportList *reports,
+                                   bool do_undo);
 void ED_object_sculptmode_enter(struct bContext *C,
                                 struct Depsgraph *depsgraph,
                                 struct ReportList *reports);
diff --git a/source/blender/editors/sculpt_paint/sculpt.c b/source/blender/editors/sculpt_paint/sculpt.c
index fc2720bc31d..f8997ba5d0b 100644
--- a/source/blender/editors/sculpt_paint/sculpt.c
+++ b/source/blender/editors/sculpt_paint/sculpt.c
@@ -10215,7 +10215,8 @@ void ED_object_sculptmode_enter_ex(Main *bmain,
                                    Scene *scene,
                                    Object *ob,
                                    const bool force_dyntopo,
-                                   ReportList *reports)
+                                   ReportList *reports,
+                                   bool do_undo)
 {
   const int mode_flag = OB_MODE_SCULPT;
   Mesh *me = BKE_mesh_from_object(ob);
@@ -10270,18 +10271,34 @@ void ED_object_sculptmode_enter_ex(Main *bmain,
     if (!has_multires && ((message_unsupported == NULL) || force_dyntopo)) {
       /* Needed because we may be entering this mode before the undo system loads. */
       wmWindowManager *wm = bmain->wm.first;
-      bool has_undo = wm->undo_stack != NULL;
+      bool has_undo = do_undo && wm->undo_stack != NULL;
 
       /* Undo push is needed to prevent memory leak. */
       if (has_undo) {
         SCULPT_undo_push_begin(ob, "Dynamic topology enable");
       }
 
+      bool need_bmlog = !ob->sculpt->bm_log;
+
       SCULPT_dynamic_topology_enable_ex(bmain, depsgraph, scene, ob);
+
       if (has_undo) {
         SCULPT_undo_push_node(ob, NULL, SCULPT_UNDO_DYNTOPO_BEGIN);
         SCULPT_undo_push_end();
       }
+      else if (need_bmlog) {
+        if (ob->sculpt->bm_log) {
+          BM_log_free(ob->sculpt->bm_log, true);
+          ob->sculpt->bm_log = NULL;
+        }
+
+        SCULPT_undo_ensure_bmlog(ob);
+
+        // SCULPT_undo_ensure_bmlog failed to find a sculpt undo step
+        if (!ob->sculpt->bm_log) {
+          ob->sculpt->bm_log = BM_log_create(ob->sculpt->bm, ob->sculpt->cd_dyn_vert);
+        }
+      }
     }
     else {
       BKE_reportf(
@@ -10300,7 +10317,7 @@ void ED_object_sculptmode_enter(struct bContext *C, Depsgraph *depsgraph, Report
   Scene *scene = CTX_data_scene(C);
   ViewLayer *view_layer = CTX_data_view_layer(C);
   Object *ob = OBACT(view_layer);
-  ED_object_sculptmode_enter_ex(bmain, depsgraph, scene, ob, false, reports);
+  ED_object_sculptmode_enter_ex(bmain, depsgraph, scene, ob, false, reports, true);
 }
 
 void ED_object_sculptmode_exit_ex(Main *bmain, Depsgraph *depsgraph, Scene *scene, Object *ob)
@@ -10380,7 +10397,7 @@ static int sculpt_mode_toggle_exec(bContext *C, wmOperator *op)
     if (depsgraph) {
       depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
     }
-    ED_object_sculptmode_enter_ex(bmain, depsgraph, scene, ob, false, op->reports);
+    ED_object_sculptmode_enter_ex(bmain, depsgraph, scene, ob, false, op->reports, true);
     BKE_paint_toolslots_brush_validate(bmain, &ts->sculpt->paint);
 
     if (ob->mode & mode_flag) {
diff --git a/source/blender/editors/sculpt_paint/sculpt_dyntopo.c b/source/blender/editors/sculpt_paint/sculpt_dyntopo.c
index 7dfea504f9b..2d5d428e676 100644
--- a/source/blender/editors/sculpt_paint/sculpt_dyntopo.c
+++ b/source/blender/editors/sculpt_paint/sculpt_dyntopo.c
@@ -894,7 +894,9 @@ void SCULPT_dynamic_topology_enable_ex(Main *bmain, Depsgraph *depsgraph, Scene
   me->flag |= ME_SCULPT_DYNAMIC_TOPOLOGY;
 
   /* Enable logging for undo/redo. */
-  ss->bm_log = BM_log_create(ss->bm, ss->cd_dyn_vert);
+  if (!ss->bm_log) {
+    ss->bm_log = BM_log_create(ss->bm, ss->cd_dyn_vert);
+  }
 
   /* Update dependency graph, so modifiers that depend on dyntopo being enabled
    * are re-evaluated and the PBVH is re-created. */
diff --git a/source/blender/editors/sculpt_paint/sculpt_intern.h b/source/blender/editors/sculpt_paint/sculpt_intern.h
index 87bc45c5355..d1f90750445 100644
--- a/source/blender/editors/sculpt_paint/sculpt_intern.h
+++ b/source/blender/editors/sculpt_paint/sculpt_intern.h
@@ -1711,3 +1711,5 @@ struct BMesh *SCULPT_dyntopo_empty_bmesh();
        SCULPT_TOOL_ELASTIC_DEFORM, \
        SCULPT_TOOL_BOUNDARY, \
        SCULPT_TOOL_POSE)
+
+void SCULPT_undo_ensure_bmlog(struct Object *ob);
diff --git a/source/blender/editors/sculpt_paint/sculpt_undo.c b/source/blender/editors/sculpt_paint/sculpt_undo.c
index a68ab80a12b..12f4f90a6e4 100644
--- a/source/blender/editors/sculpt_paint/sculpt_undo.c
+++ b/source/blender/editors/sculpt_paint/sculpt_undo.c
@@ -1740,6 +1740,14 @@ void SCULPT_undo_ensure_bmlog(Object *ob)
 
   UndoStep *us = BKE_undosys_stack_active_with_type(ustack, BKE_UNDOSYS_TYPE_SCULPT);
 
+  if (!us) {
+    // check next step
+    if (ustack->step_active && ustack->step_active->next &&
+        ustack->step_active->next->type == BKE_UNDOSYS_TYPE_SCULPT) {
+      us = ustack->step_active->next;
+    }
+  }
+
   if (!us) {
     return;
   }
@@ -1786,6 +1794,10 @@ static SculptUndoNode *sculpt_undo_bmesh_push(Object *ob, PBVHNode *node, Sculpt
 
   SCULPT_undo_ensure_bmlog(ob);
 
+  if (!ss->bm_log) {
+    ss->bm_log = BM_log_create(ss->bm, ss->cd_dyn_vert);
+  }
+
   bool new_node = false;
 
   if (unode == NULL) {
@@ -2287,7 +2299,7 @@ static void sculpt_undosys_step_decode(
          * The undo steps must enter/exit for us. */
         me->flag &= ~ME_SCULPT_DYNAMIC_TOPOLOGY;
 #endif
-        ED_object_sculptmode_enter_ex(bmain, depsgraph, scene, ob, true, NULL);
+        ED_object_sculptmode_enter_ex(bmain, depsgraph, scene, ob, true, NULL, false);
       }
 
       if (ob->sculpt) {
diff --git a/source/blender/editors/util/ed_util.c b/source/blender/editors/util/ed_util.c
index 73f328f85d7..92d56ccf521 100644
--- a/source/blender/editors/util/ed_util.c
+++ b/source/blender/editors/util/ed_util.c
@@ -135,7 +135,7 @@ void ED_editors_init(bContext *C)
       else if (mode & OB_MODE_ALL_SCULPT) {
         if (obact == ob) {
           if (mode == OB_MODE_SCULPT) {
-            ED_object_sculptmode_enter_ex(bmain, depsgraph, scene, ob, true, reports);
+            ED_object_sculptmode_enter_ex(bmain, depsgraph, scene, ob, true, reports, true);
           }
           else if (mode == OB_MODE_VERTEX_PAINT) {
             ED_object_vpaintmode_enter_ex(bmain, depsgraph, scene, ob);



More information about the Bf-blender-cvs mailing list