Making a cancellable operator in Blender uncancellable

When defining custom operators in Blender, we can make them cancellable and uncancellable by specifying the “UNDO” value in the “bl_options” parameter of the operator. But what if we need to exclude an existing operator, for example, a system operator, from the undo queue? This is very easy to do by redefining the operator with our own, without specifying “UNDO” in its parameters.

For example, let’s redefine the simplest operator that adds a cube to the scene – “mesh.primitive_cube_add”.

Define an overriding class, in the “execute” function of which we will simply call the system creating a cube operator.

In the “bl_options” parameter, we do not specify “UNDO” among the set values ​​so that our wrapper operator is irreversible and does not react to pressing the “Ctrl + z” key combination.

To test how our operator will work, create a custom “TEST” panel with two buttons in the N-panel. We will bind the common operator “mesh.primitive_cube_add” to the first one, and our overriding operator to the second one.

Register both classes in the Blender Python API.

Now in our test panel, press the “Add Cube” button several times, to which the common system operator for creating a cube is bound. After that, try to cancel these actions by pressing Ctrl + z. We see how each press cancels the creation of one last cube.

Now, press the “CUBE NO UNDO” button several times, which executes our overriding operator, and then also try to cancel these actions. In this case, we see that pressing Ctrl + z once removes all cubes created by the overridden operator in the scene at once.

This happens because our operator blocks the undo of the action, even though in fact it simply calls the system operator, which has an action cancellation. Since each separate execution of our overriding operator does not have a cancellation, the “rollback” is performed immediately to the state before the first click of the non-cancellable operator.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments