Gimp-Forum.net
GIMP 2.10.32 Python-Fu - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions)
+---- Thread: GIMP 2.10.32 Python-Fu (/Thread-GIMP-2-10-32-Python-Fu)



GIMP 2.10.32 Python-Fu - alecpepe - 07-25-2022

I have successfully written a script, which when ran in the Python-Fu console does what I want. I want to make a keyboard shortcut, so I can run this quickly when necessary, rather than pasting it into the console every time. How should I do this? I've tried writing the code in VS code and saving it in the GIMP scripts folder as both a .py and .scm file. The .scm file raises 'unbound variable: import' when I hit 'refresh scripts' under Script-Fu. I cannot find where to run the script as a .py, but it seems to refresh fine. I also do not know how I would bind a keyboard shortcut to a script.

Here's the code, though I doubt this is the issue as it works fine in the console:

import gimpfu
current_image = gimp.image_list()[0]
layer=pdb.gimp_layer_new(current_image, 1, 1, gimpfu.RGB_IMAGE, "white_layer", 100, gimpfu.NORMAL_MODE)
pdb.gimp_image_add_layer(current_image, layer, 0)
pdb.gimp_layer_resize_to_image_size(layer)
pdb.gimp_item_set_visible(layer, 0)


RE: GIMP 2.10.32 Python-Fu - Ofnuts - 07-25-2022

To make you code a real "script":

1) Since it's Python it is technically no a script, but a plugin so it goes in the "plug-ins", directory
2) It has to "register" to make it known to Gimp. This means declaring a procedure, the parameter types, the menu position, the kind of images it can work on, and miscellaneous metadata (author...)

So your code ends up looking like this:

Code:
# Preferred form of import, avoids "gimpfu" prefixes all over.
from gimpfu import *

# The real code. The parameter "image" is automatically set to the current image
def addWhiteLayer(image):
   # Create layer directly at the image size. RGB_IMAGE type will only work in RGB images
   layer=pdb.gimp_layer_new(image,image.width,image.height,RGB_IMAGE, "white_layer", 100, NORMAL_MODE)
   # Force white fill
   pdb.gimp_drawable_fill(layer,FILL_WHITE)
   pdb.gimp_image_add_layer(image, layer, 0)
#    pdb.gimp_item_set_visible(layer, 0) # Not really necessary
   
register(
   'add-white-layer',                  # Unique ID, I prefix mine with "ofn-" to avoid clashes
   'Add a white layer',                # Description/title (short)
   'Add a white layer',                # Help (can be longer...)
   "Author",                           # Name of author
   "Author",                           # Copyrihgt owner
   "2022",                             # Copyright year
   'Add white layer',                  # The menu label
   "RGB*",                             # The type of images it can work on. Use "*" for all types
   [                                   # List of input parameters (just one here)
       (PF_IMAGE, "image", "Input image", None)    # Just an image, since it's the first one, Gimp
                                                   # Sets it implicitly without showing a dialog
   ],
   [],                                 # List of output parameters
   addWhiteLayer,                      # The Python code that implementsthe plugin
   menu="<Image>/Layer",               # Where the menu label above appears
)

main()

When your  code registers correctly, the menu entry appears at the bottom of the Layer menu.

You can then edit the keyboard shortcuts and search the add-white-layer identifier to assign a shortcut:

[attachment=8399]