0
$\begingroup$

I’ve written an operator which edits an image in the Image Editor, but it won't work properly with the undo system. I have created a minimal repro script:

import bpy

img = bpy.data.images['MyImage']

# fill bottom half with white pixels
for x in range(img.size[0] * img.size[1] * 2):
    img.pixels[x] = 1

img.update()

Creating a small black RGBA image (e.g. 8x8) called "MyImage" and running the code above, I am not able to undo the effect of the script properly.

I have also tried using bpy.ops.ed.undo_push() to try snapshotting before and after the image pixels are altered, as well as trying Image.pack() to pack the image into the blend file in the hope it gets snapshotted, to no avail. The built-in texture paint tools are able to create undo states which properly work, so I am hoping there is some API that I can use for this.

To put it simply - undo states behave weirdly when pixels is altered (the output seems different when undoing and redoing back to the same state) as can be seen in this video:

https://i.imgur.com/D9f5g5W.mp4

I have spent many hours searching for a solution. If anyone could offer any insights or workarounds I would greatly appreciate it.

$\endgroup$
2
  • $\begingroup$ What happens when you use bpy.ops.image.reload(), does it reload to the previous state? In your operator, do you use bl_options = {'REGISTER', 'UNDO'}? $\endgroup$ Commented Sep 20 at 2:14
  • $\begingroup$ Sadly does not work either. I have since made a bug report and it was confirmed, but I would be very interested in any workarounds I can use in the meantime (something to piggyback off of internal undo API calls? Maybe saving and reloading?) Anything that would create a valid, usable undo state. I haven't been able to find anything yet $\endgroup$
    – toipin
    Commented Sep 20 at 10:50

0

You must log in to answer this question.

Browse other questions tagged .