PyAPI: add undo redo handlers
authorCampbell Barton <ideasman42@gmail.com>
Thu, 12 Jul 2018 12:43:35 +0000 (14:43 +0200)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 12 Jul 2018 12:43:35 +0000 (14:43 +0200)
Useful so Python can clean up before/after undo steps.

source/blender/blenlib/BLI_callbacks.h
source/blender/editors/undo/ed_undo.c
source/blender/python/intern/bpy_app_handlers.c

index 6eb451074885027959401a0a1fbaebcc7c7a5a70..3db7d2d87f182962d377a3fe2f12486c60f1b21c 100644 (file)
@@ -49,6 +49,10 @@ typedef enum {
        BLI_CB_EVT_LOAD_POST,
        BLI_CB_EVT_SAVE_PRE,
        BLI_CB_EVT_SAVE_POST,
+       BLI_CB_EVT_UNDO_PRE,
+       BLI_CB_EVT_UNDO_POST,
+       BLI_CB_EVT_REDO_PRE,
+       BLI_CB_EVT_REDO_POST,
        BLI_CB_EVT_SCENE_UPDATE_PRE,
        BLI_CB_EVT_SCENE_UPDATE_POST,
        BLI_CB_EVT_GAME_PRE,
index 5f5b7b330f9081850e31cd2c1cc187a396343026..b4268dac8637161940714d67dd4500ab7bb0355d 100644 (file)
@@ -38,6 +38,8 @@
 #include "DNA_scene_types.h"
 
 #include "BLI_utildefines.h"
+#include "BLI_callbacks.h"
+#include "BLI_listbase.h"
 
 #include "BLT_translation.h"
 
@@ -119,17 +121,50 @@ static int ed_undo_step(bContext *C, int step, const char *undoname)
                return ED_undo_gpencil_step(C, step, undoname);
        }
 
+       UndoStep *step_data_from_name = NULL;
+       int step_for_callback = step;
+       if (undoname != NULL) {
+               step_data_from_name = BKE_undosys_step_find_by_name(wm->undo_stack, undoname);
+               if (step_data_from_name == NULL) {
+                       return OPERATOR_CANCELLED;
+               }
+
+               /* TODO(campbell), could use simple optimization. */
+               BLI_assert(step_data_from_name != wm->undo_stack->step_active);
+               step_for_callback = (
+                       BLI_findindex(&wm->undo_stack->steps, step_data_from_name) <
+                       BLI_findindex(&wm->undo_stack->steps, wm->undo_stack->step_active)) ? 1 : -1;
+       }
+
+       /* App-Handlers (pre). */
+       {
+               /* Note: ignore grease pencil for now. */
+               Main *bmain = CTX_data_main(C);
+               wm->op_undo_depth++;
+               BLI_callback_exec(bmain, &scene->id, (step_for_callback > 0) ? BLI_CB_EVT_UNDO_PRE : BLI_CB_EVT_REDO_PRE);
+               wm->op_undo_depth--;
+       }
+
+
        /* Undo System */
        {
                if (undoname) {
-                       UndoStep *step_data = BKE_undosys_step_find_by_name(wm->undo_stack, undoname);
-                       BKE_undosys_step_undo_with_data(wm->undo_stack, C, step_data);
+                       BKE_undosys_step_undo_with_data(wm->undo_stack, C, step_data_from_name);
                }
                else {
                        BKE_undosys_step_undo_compat_only(wm->undo_stack, C, step);
                }
        }
 
+       /* App-Handlers (post). */
+       {
+               Main *bmain = CTX_data_main(C);
+               scene = CTX_data_scene(C);
+               wm->op_undo_depth++;
+               BLI_callback_exec(bmain, &scene->id, step_for_callback > 0 ? BLI_CB_EVT_UNDO_PRE : BLI_CB_EVT_REDO_PRE);
+               wm->op_undo_depth--;
+       }
+
        WM_event_add_notifier(C, NC_WINDOW, NULL);
        WM_event_add_notifier(C, NC_WM | ND_UNDO, NULL);
 
index 90aa22de5bfe822911d05cc12d9865d620d49337..ded5ead93aa580cb5d5f1fc6d4b5f3983d1015a4 100644 (file)
@@ -59,6 +59,10 @@ static PyStructSequence_Field app_cb_info_fields[] = {
        {(char *)"load_post",         (char *)"on loading a new blend file (after)"},
        {(char *)"save_pre",          (char *)"on saving a blend file (before)"},
        {(char *)"save_post",         (char *)"on saving a blend file (after)"},
+       {(char *)"undo_pre",          (char *)"on loading an undo step (before)"},
+       {(char *)"undo_post",         (char *)"on loading an undo step (after)"},
+       {(char *)"redo_pre",          (char *)"on loading a redo step (before)"},
+       {(char *)"redo_post",         (char *)"on loading a redo step (after)"},
        {(char *)"scene_update_pre",  (char *)"on every scene data update. Does not imply that anything changed in the "
                                           "scene, just that the dependency graph is about to be reevaluated, and the "
                                           "scene is about to be updated by Blender's animation system."},