How to get bpy.data.is_dirty updating when changes are done by script?

My addon relies on checking bpy.data.is_dirty. This seems to work fine in interactive mode, however when I tried adding automated tests for my addon I have faced issue with bpy.data.is_dirty not updating correctly when things are getting changed from my tests (Python script).

Example script to reproduce (on Blender 2.79):

>>> bpy.data.scenes[0].frame_start = bpy.data.scenes[0].frame_start + 1
>>> bpy.data.is_dirty
False

I can see starting frame change on the timeline, however there is no asterisk “*” visible in title (which generally indicates unsaved changes).
If I change starting frame by clicking on small arrow by number it marks the file correctly as changed (both the asterisk appears and bpy.data.is_dirty returns `True).

I have also managed to get a state in tests that have bpy.data.is_dirty returns True even on the next line just after call to bpy.ops.wm.save_mainfile().

Is there some function I need to call to get Blender’s internal state to update bpy.data.is_dirty?

Alternatively, as I need it only for tests, is there some way to make sure that bpy.data.is_dirty is in a desired state? (i.e. I need to be able to set it both to True and False depending on scenario)

I read https://docs.blender.org/api/2.79/info_gotcha.html#no-updates-after-setting-values and tried using bpy.context.scene.update(), but it doesn’t seem to impact bpy.data.is_dirty.

Note: this is clone of my question on blender.stackexchange

1 Like

is_dirty is intended to indicate if the file has been modified by the user, when they run operators or edit values in the UI. It is not intended to be set whenever a script changes the scene, since that may just be for animation or automation purposes.

Any operator that does an undo push would change this value, so maybe running an operator from the script would help.

I guess that can make a sense for most common use cases.
If I have to “hack” it for tests, I might as well mock it. Doesn’t seem that beneficial to do a lot of odd actions to just get one test to go through.
At least nice to know that it is by design and not some obscure bug I found.

It would be nice if documentation was more explicit about it

https://docs.blender.org/api/2.79/bpy.types.BlendData.html?highlight=is_dirty#bpy.types.BlendData.is_dirty only says:

Have recent edits been saved to disk

I suspect I would need to know how “edit” is defined in context of this documentation.

Can we add a flag that switches to True when blend file data content changes?
Adding a dummy operator to all other addon operators seems like a hack. +50 lines of code in my case, just for one check.

This doesn’t work. is_dirty flips to True only when an operator with Undo was invoked from the interface.

My proposal how we can improve this issue: