4

I'm writing a python plugin for GIMP. The plugin manipulates the image but the undo stack does not actually undo anything my plugin did.

How do I get undo to work in this context?

For example, the following will set the first tile in an RGBA image to transparent. The undo stack will show "Example" and "[Base Image]", but switching to the base image will not undo the transparency.

#!/usr/bin/env python
from gimpfu import *
import struct

def do_python_fu_example(img, layer):

    pdb.gimp_undo_push_group_start(img)

    # example: set the first tile to completely transparent
    tile = layer.get_tile(False, 0, 0)
    for y in xrange(tile.eheight):
        for x in xrange(tile.ewidth):
            pixel = struct.unpack("BBBB",tile[x,y])
            pixel = struct.pack("BBBB", pixel[0], pixel[1], pixel[2], 0)
            tile[x,y] = pixel

    pdb.gimp_undo_push_group_end(img)

    layer.update(0,0,layer.width,layer.height)
    layer.flush()
# end do_swap

register(
    "python_fu_example",
    "Example",
    "Example",
    "Example",
    "Example",
    "2016",
    "Example...",
    "RGBA",      # Alternately use RGB, RGB*, GRAY*, INDEXED etc.
    [
        (PF_IMAGE, "img", "Input image", None),
        (PF_DRAWABLE, "layer", "Input layer", None),
    ],
    [],
    do_python_fu_example, menu="<Image>/Colors")

main()
Clay
  • 1,159
  • 1
  • 9
  • 20

0 Answers0