Accessing Undo History via Python API

I’ve been trying to build a simple Addon that just displays the current Undo History on the 3D ViewPort (SpaceView3D?) so it’s always visible. So basically I need read-only access to the undo stack/history.

I’ve almost been successful using existing API, but what I’m doing isn’t reliable enough. I’m always able to find a way to break it so the Edit->Undo History gets out of sync with what I’m showing. Basically it would look something like this:

What does seem reliable is the output from bpy.context.window_manager.print_undo_steps(), but redirecting that from the console back to Python for display in the ViewPort is pretty hacky. Also I need a solution that works on Linux and the example posted in the other thread I linked looks Win specific.

Undo 12 Steps (*: active, #=applied, M=memfile-active, S=skip)
[    ]   0 {0x7fe9a8a69908} type='Global Undo', name='Original'
[    ]   1 {0x7fe9a6c40f08} type='Global Undo', name='Console Execute'
[    ]   2 {0x7fe9a72bf808} type='Global Undo', name='Select'
[    ]   3 {0x7fe9a53f7808} type='Global Undo', name='Select'
[    ]   4 {0x7fe9a5700ee8} type='Edit Mesh', name='Toggle Edit Mode'
[    ]   5 {0x7fe9a8a60c08} type='Global Undo', name='Toggle Edit Mode'
[    ]   6 {0x7fe9a8a60f88} type='Global Undo', name='Select'
[    ]   7 {0x7fe9a635e188} type='Edit Mesh', name='Toggle Edit Mode'
[    ]   8 {0x7fe9a56792e8} type='Edit Mesh', name='Select'
[    ]   9 {0x7fe9a8a5b708} type='Global Undo', name='Toggle Edit Mode'
[    ]  10 {0x7fe9d8114388} type='Global Undo', name='Select'
[* M ]  11 {0x7fe9a53f7d08} type='Global Undo', name='Select'

Of course it would be preferable to get this in a proper data structure, rather than text that needs to be parsed.

This thread sounds similar and maybe an API upgrade could solve some of those problems too. Access to the history stack

I was considering trying a PR to enhance the API, but it’s been a while since I’ve coded in C and I’m uncertain how difficult an API upgrade for this would be. Am I headed down a feasible path to make this possible?


Background: I ended up down this rabbit hole because I’m new to Blender, and I’m finding myself constantly accessing the Edit->Undo History menu option to have better visibility into what operations were recently done (or not done). When I wasn’t checking this, I found that I was making mistakes more often that I didn’t notice right away.

Hmm, I guess either I’m in the wrong place or everyone who might have useful feedback for this prefers live chat or something. :sweat_smile:

TLDR: Would a pull request that adds a call to the Python API which exposes the data that this print_undo_steps() function displays even be accepted, or would I be wasting my time trying to do this?