2
$\begingroup$

Is there a way to call your own undo and or redo method when your operator is being 'undone'? I already used this bpy built-in method:

bl_info = {'UNDO'}

but this didn't do the full undo.

$\endgroup$

2 Answers 2

6
$\begingroup$

Blender's undo system works by saving the blend file to memory, and loading it to perform the undo. If your operator doesn't fully undo, you're probably calling other operators from within its code. That's usually a bad idea; modify object properties and call functions instead.

There are no undo/redo functions that you can override.

$\endgroup$
2
  • $\begingroup$ So there is no real workaround if you call a 'add modifier' operator from within your own operator to undo this action? $\endgroup$ Commented Dec 3, 2017 at 11:30
  • 2
    $\begingroup$ Look at the source code of the operator. If it calls into a Python function to do the real work, you can just call that function instead of the operator itself. $\endgroup$ Commented Dec 3, 2017 at 23:37
1
$\begingroup$

There is a way.

  • Register a BoolProperty to the scene, here we call it checker.
  • In your operator, access checker via context and run checker = not checker.
  • Define two global bool property: checker_cache and skip_checker_update.
  • In the update function of checker, if skip_checker_update is False, sync the checker and the checker_cache.
  • Define your own post undo function my_undo_post(scene, _) and a pre undo function my_undo_pre(_, _).
  • In your pre undo/redo function, set skip_checker_update as True so the blender's undo/redo logic will change back the checker because it is registed, but the checker_cache will keep unchanged.
  • In your post undo/redo function, check if the checker not equals checker_cache, if is, sync them and run your undo logic. Then turn skip_checker_update back to False
  • Append your own pre/post undo/redo functions to blender's. E.g. run bpy.app.handlers.undo_post.append(my_undo_post).

It's a little complex but basically works.

$\endgroup$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.