Page MenuHome

Python Operators that add/remove ID data without an undo step crash Blender
Confirmed, NormalPublicTO DO


System Information
Operating system: Win 10
Graphics card: GTX 1070

Blender Version
Broken: 2.8.3, 2.9 aed11c673efe
Worked: 2.8.0

Undoing after object linking to the scene crashes blender. I used the latest daily build

Exact steps for others to reproduce the error

  1. Load the crash blend file
  2. Run the script attached
  3. Click in viewport to add a new object, and then press ESC
  4. Press Ctrl+Z to undo
  5. Crash..

Related Objects

Event Timeline

Stack trace:
blender.exe :0x00007FF6A2A2C260 BKE_scene_set_background
blender.exe :0x00007FF6A555F220 setup_app_data
blender.exe :0x00007FF6A555E600 BKE_blendfile_read_from_memfile
blender.exe :0x00007FF6A557B740 BKE_memfile_undo_decode
blender.exe :0x00007FF6A31362B0 memfile_undosys_step_decode
blender.exe :0x00007FF6A555E2F0 undosys_step_decode
blender.exe :0x00007FF6A555DEF0 BKE_undosys_step_undo_with_data_ex
blender.exe :0x00007FF6A3137540 ed_undo_step_impl
blender.exe :0x00007FF6A3137300 ed_undo_exec
blender.exe :0x00007FF6A2BC77A0 wm_operator_invoke
blender.exe :0x00007FF6A2BC5630 wm_handler_operator_call
blender.exe :0x00007FF6A2BC6CE0 wm_handlers_do_keymap_with_keymap_handler
blender.exe :0x00007FF6A2BC65B0 wm_handlers_do_intern
blender.exe :0x00007FF6A2BC5BE0 wm_handlers_do
blender.exe :0x00007FF6A2BC3260 wm_event_do_handlers
blender.exe :0x00007FF6A2BAEDC0 WM_main
blender.exe :0x00007FF6A293A530 main

Migth be related to this T77595

Bastien Montagne (mont29) changed the task status from Needs Triage to Confirmed.Jun 8 2020, 4:06 PM
Bastien Montagne (mont29) changed the subtype of this task from "Report" to "To Do".

This script has two errors, it's returning CANCELLED instead of FINISHED even though it did change data.

And it does not specify UNDO to its bl_options flags.

@Campbell Barton (campbellbarton) looks like py-defined operators have no UNDO flag by default, think this is no longer acceptable? Any change to data without a proper undo step was (very) bad before, but now it is a guaranteed crash.

@Bastien Montagne (mont29) I am doing something very similar to this script in a different project, unrelated to modal operators, and it's crashing when linking to the scene. I can't share that code.. but what I can say is that I am using depsgraph_update_handler_post() to create and link the objects to the scene.
Maybe introducing new API calls similar to bpy.ops.ed.undo_push() in order to cut new UNDO checkpoints can be a solution for user defined scripts.. Then, they can decide when to cut an check point and when to ignore it.
Anyway, I think is very unusual to crash blender only because you missed an UNDO checkpoint

This is cost of new speedy undo code, it expect current datablocks to match those of current undo step, otherwise... bad things will happen

I agree that performance is important, but this cannot stay the way it is now. 'Fast crashing' blender is not a solution for anyone :) I am pretty sure there are many important third party plugins rendered unusable by this change. What can be done to fix it?

Calling the esoteric bpy.ops.ed.undo_push() after object link to the scene, appears to be working in this particular case..

@Radu Popovici (rpopovici) I already told you above what you need to do to fix that operator:

This script has two errors, it's returning CANCELLED instead of FINISHED even though it did change data.

And it does not specify UNDO to its bl_options flags.

Only thing at stake here, is whether we should enable undo by default for py operators (I think we should, but would like to have @Campbell Barton (campbellbarton) advice on this as well).

@Bastien Montagne (mont29) yes that works if you use operators to create stuff, but if you don't add new objects with a custom operator then adding the UNDO flag + FINISHED is not an option. Adding new objects can happen on any callback handler: timer handler, deps graph update handler, nodes updates, even the undo handler

@Radu Popovici (rpopovici) adding or removing objects (or any other datablock) in handlers is not a good idea nor practice, not at all. It might work in some cases, but this is typically things that are very likely to cause problems one way or the other.

Our python API is very permissive, which makes it powerful and quiet efficient, but also very easy to shoot yourself in the foot and crash blender in many imaginative ways...

OK then, what exactly can I do from a handler? Is it safe at least to call custom operators from handlers? And then I can create objects from inside the execute method of these ops..

Using application timers, execution queues and custom ops to react to events on main thread appears to be the acceptable solution to my problem, according to the docs

@Bastien Montagne (mont29), there was a similar report I replied to recently but I cant find it now.

Operators that don't set the undo flag but add/remove library data are going to crash.

A fix for this would be to detect the Main data-base as being modified, then add an undo step even if the operator isn't marked to undo.

@Campbell Barton (campbellbarton) batching the unexpected undo step into the previous one would feel more natural than if a new step is created. If you create a new undesired step, that could be cause for confusion
when ctrl+z is pressed and you have to press it multiple times to undo the unexpected steps.
And also this would be more consistent with the current definition of what UNDO flag means. basicaly, you are asking for a new undo step to be created whan an UNDO flag is present, and if none is pressent you want the last changes to be merged into the last existing step.
I think that this approach could address many of the UNDO related issues in blender and besided that it could be beneficial when post event operations are performed to be linked to the last undo step..

@Radu Popovici (rpopovici) the undo step can be added without it showing up to the user, that's supported already.

@Campbell Barton (campbellbarton) how? Can I access this feature from python? is it possible to add an undo step which will be jumped(undo multiple steps in one) when Ctrl+z is pressed?

As a side node here and potential workaround for anyone facing a similar problem: you can avoid this crash by unlinking post-event generated objects in undo_pre handler, and then link it back in undo_post

Campbell Barton (campbellbarton) renamed this task from UNDO crash in bledner 2.9(daily) while linking new object to the scene to Python Operators that add/remove ID data without an undo step crash Blender.Sep 11 2020, 6:15 AM

Just a heads up there are several add-ons shipped with Blender that have operators without "UNDO" in their bl_options = {"REGISTER"}. Not every one of them creates geometry, but they should all be checked and the option included where needed. For example the Discombombulator add-on, definitely has this bug (see T82763).

@Robert Guetzkow (rjg) Just wanted to comment as well, in case this is in response to Eric, whom I've sent here. His crashes are related to adding/removing ID data using layout.template_icon_view and so changing ID data from enum update callbacks. This is still unresolved, yet my reports about it were all closed.
I was told conflicting information: 1. Don't change ID data from update callbacks and don't run operators from them, 2. If you change ID data use operators. Following this advice means the template_icon_view layout element has become useless for its most popular use case: adding external assets. Since Blender is crashing when undoing after such an operation, it also risks data loss for a huge amount of Blender users.

@MACHIN3 (MACHIN3) No, this wasn't a reply to Eric. I've just stumbled upon the case where the bl_options weren't properly set in an add-on. If I have time I'll take a look at your ticket later this week.

They could tag this as a known issue already.