Search code examples
pythongimpgimpfu

gimp python execution error


I spent awhile trying to get GIMP to register an example of the classic "hello world" plugin that I wrote to begin learning. Finally, I got it to register after double checking the file location and the path for plugins in GIMP's preferences as well as changing user permissions for executing the .py in terminal using chmod.

Got it to work! The "hello" appears under the File drop down menu, as I had written in the code. The problem is it doesn't display the console box with my much awaited message!

If I go to the python-fu console in GIMP I can find my plugin in the PDB and when I click apply and run it from the console, I get an execution error,

Traceback (most recent call last):
  File "<input>", line 1, in <module>
RuntimeError: execution error

I was hoping I had the code correct and that my big issue was just the registration, but now my hopes are crushed! Is there anything wrong with my code:

#!/usr/bin/env python

from gimpfu import *

def hello_world(image, drawable):
    pdb.gimp_message("Hello World")

register("python-fu-hello-world", "hello", "helloooo", "X. D.", "X. D.", "2018", "Hello", "", [], [], hello_world, menu="<Image>/File")

main()

(Macbook OS 10.11, python 2.7, GIMP 2.8)


Solution

  • The problem is that the argument list of the python function (def hello_world(image, drawable) must match the declaration of arguments in the parameters to the register() call.

    The first [] in your registration code, being an empty list tells Gimp that the python function doesn't take arguments. In the typical case, you want these arguments (because they tell you on which layer/channel the script should be run), so you do:

    #!/usr/bin/env python
    
    from gimpfu import *
    
    def hello_world(image, drawable):
        pdb.gimp_message("Hello World")
    
    register("python-fu-hello-world", "hello", "helloooo", "X. D.", "X. D.", "2018", "Hello", 
            "", # Type of drawable on which the script runs 
            [
                (PF_IMAGE, 'image', 'Input image', None),
                (PF_DRAWABLE, 'drawable', 'Input drawable', None),
            ], 
            [], # this one is used if you return results 
            hello_world, menu="<Image>/Test")
    
    main()
    

    The drawable type is important. If you leave it empty ('') the script can be called without an image/layer, which is rarely the case. If you set it to '*' then the script can run on any drawable, but the menu will be grayed out if there is no image opened in Gimp. "Drawables" are layers, layer masks and channels (saved selections, etc), so '*' implies that you can process one-bit/channel items. If not so, use 'RGB*' which tells Gimp you only support RGB items (with or without alpha) so that the menu item is disabled when your are editing a layer mask or a channel.