Here are some random notes on Blender undo mechanism. This is to prepare for optimizing the memory usage of undo stack.

Undo is implemented as a Blender operator in the function ed_undo_exec1.

In the function ed_undo_exec, first the most recent executed operator is removed from the list wmWindowManager::operators2. And then the actual roll back of data happens in the function ed_undo_step1.

For Mesh type in edit mode, the undo happens in the function undo_editmode_step3, then steps into the function undo_restore3.

The undo stack is a ListBase, which is named as undobase. The top stack element is a struct UndoElem3, which is named as curundo.

Each element from undo stack knows how to manipulate, restore, and release the data. This is done by setting the function pointers in the struct UndoElem (C style polymorphism).

On the other hand, whenever an operator is executed, one node will be appended to the undo stack. This happens in the function undo_editmode_push[3]. The stack is shown here:

blender-app.exe!undo_editmode_push
blender-app.exe!undo_push_mesh
blender-app.exe!ED_undo_push
blender-app.exe!ED_undo_push_op
blender-app.exe!wm_operator_finished
blender-app.exe!wm_handler_operator_call
blender-app.exe!wm_handlers_do_intern
blender-app.exe!wm_handlers_do
blender-app.exe!wm_event_do_handlers
blender-app.exe!WM_main
blender-app.exe!main

The construction of UndoElem and memory replication happens here. This is the place which is supposed to be optimized. A new set of “virtual” functions of struct UndoElem need to be override to provide optimized undo implementation.

Functions/files references

  1. source/blender/editors/util/undo.c 2

  2. source/blender/makesdna/DNA_windowmanager_types.h

  3. source/blender/editors/util/editmode_undo.c 2 3