Usually, when we undo our last action in Blender by pressing Ctrl + z, the entire action is undone, even if it’s complex. For example, if a script execution is completely undone, it’s impossible to undo only half of the code executed in the script. Or possible?
The Blender Python API provides the ability to specifically set undo points up to which the last action will be undone.
This undo point is created when calling the operator
|
1 |
bpy.ops.ed.undo_push() |
Each time this operator is called, the current state of the scene in Blender is pinned to the “global undo” stack. Now, when we press Ctrl + z, the scene will be rolled back to this pinned state.
Look at how this works using a simple script.
Let’s say our script adds three objects to the scene: Suzanne, a cube, and a sphere.
To do this, we need to call three operators in a row to create these objects in the scene:
|
1 2 3 4 5 6 7 8 9 10 11 |
bpy.ops.mesh.primitive_monkey_add( location=(0, 0, 0) ) bpy.ops.mesh.primitive_cube_add( location=(3, 0, 0) ) bpy.ops.mesh.primitive_uv_sphere_add( location=(-3, 0, 0) ) |
Execute the script by pressing the “Run Script” arrow in Blender’s text editor area. As we can see, the meshes we need have appeared in the scene.
Now press the undo key combination: Ctrl + z. All three objects added to the scene have disappeared. This happens because the “Run Script” action was completely canceled, and the scene in Blender was reverted to the state that Blender had automatically fixed before our script started executing.
Now let’s add undo point statements to the script between the existing object creation operators.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
bpy.ops.mesh.primitive_monkey_add( location=(0, 0, 0) ) bpy.ops.ed.undo_push() bpy.ops.mesh.primitive_cube_add( location=(3, 0, 0) ) bpy.ops.ed.undo_push() bpy.ops.mesh.primitive_uv_sphere_add( location=(-3, 0, 0) ) |
Execute the modified script. And again, try undoing the last action.
As we can see, pressing Ctrl + z once removed only the last object added to the scene — the sphere. This means the scene didn’t revert to the initial script execution point, as it had before, but to the last manually created undo point, which we created by calling the undo_push() operator before the sphere creation operator primitive_uv_sphere_add().
Press Ctrl + z again. Now the cube has disappeared. The scene has reverted to the previous undo point, which we created before calling the operator to add the cube to the scene.
Finally, pressing Ctrl + z a third time completely clears the scene, reverting it to the undo point that was created automatically before the script execution.
This way, by calling the undo_push() operator at the desired time, we can flexibly control the undo process in Blender.

.blend file on Patreon