Fix T61049: Undo dynamic topology sculpt asserts
authorCampbell Barton <ideasman42@gmail.com>
Thu, 7 Feb 2019 02:22:31 +0000 (13:22 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 7 Feb 2019 02:29:59 +0000 (13:29 +1100)
source/blender/editors/sculpt_paint/sculpt_undo.c

index d8a85c33e5551123c83fa90abdca6acb4991a139..1b8e6da6ee3ef4173be44d1e51328c0a052da949 100644 (file)
@@ -48,6 +48,7 @@
 #include "BKE_key.h"
 #include "BKE_mesh.h"
 #include "BKE_mesh_runtime.h"
+#include "BKE_scene.h"
 #include "BKE_subsurf.h"
 #include "BKE_subdiv_ccg.h"
 #include "BKE_undo_system.h"
@@ -1098,11 +1099,35 @@ static void sculpt_undosys_step_decode_redo(struct bContext *C, SculptUndoStep *
        }
 }
 
-static void sculpt_undosys_step_decode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p, int dir)
+static void sculpt_undosys_step_decode(struct bContext *C, struct Main *bmain, UndoStep *us_p, int dir)
 {
-       /* TODO(campbell): undo_system: use low-level API to set mode. */
-       ED_object_mode_set(C, OB_MODE_SCULPT);
-       BLI_assert(sculpt_undosys_poll(C));
+       /* Ensure sculpt mode. */
+       {
+               Scene *scene = CTX_data_scene(C);
+               ViewLayer *view_layer = CTX_data_view_layer(C);
+               /* Sculpt needs evaluated state. */
+               BKE_scene_view_layer_graph_evaluated_ensure(bmain, scene, view_layer);
+               Object *ob = OBACT(view_layer);
+               if (ob && (ob->type == OB_MESH)) {
+                       Depsgraph *depsgraph = CTX_data_depsgraph(C);
+                       if (ob->mode & OB_MODE_SCULPT) {
+                               /* pass */
+                       }
+                       else {
+                               ED_object_mode_generic_exit(bmain, depsgraph, scene, ob);
+                               Mesh *me = ob->data;
+                               /* Don't add sculpt topology undo steps when reading back undo state.
+                                * The undo steps must enter/exit for us. */
+                               me->flag &= ~ME_SCULPT_DYNAMIC_TOPOLOGY;
+                               ED_object_sculptmode_enter_ex(bmain, depsgraph, scene, ob, NULL);
+                       }
+                       BLI_assert(sculpt_undosys_poll(C));
+               }
+               else {
+                       BLI_assert(0);
+                       return;
+               }
+       }
 
        SculptUndoStep *us = (SculptUndoStep *)us_p;
        if (dir < 0) {