07-25-2022, 10:52 PM
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:
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:
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: