Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Fu
#1
Tried to create my first script to save all open files and while it works, I've encountered a strange issue.
Code:
#!/usr/bin/env python

# Author: DoggoOfSpeed

from gimpfu import *
import os


def save(image, path):
   for layer in image.layers:
       image_name_clean = os.path.splitext(
           os.path.basename(image.filename))[0]
       full_path = os.path.join(path, image_name_clean + '.xcf')
       pdb.gimp_xcf_save(0, image, layer, full_path, full_path)


def save_all(path_type, custom_path, x, y):
   open_images = gimp.image_list()

   for single_image in open_images:
       location = os.path.dirname(single_image.filename)
       location_parent = os.path.dirname(location)
       if x == 0:
           save(single_image, location)
       elif x == 1:
           if not os.path.exists(location + "/Saved"):
               os.mkdir(location + "/Saved")
           save(single_image, location + "/Saved")
       elif x == 2:
           if not os.path.exists(location_parent + "/Saved"):
               os.mkdir(location_parent + "/Saved")
           save(single_image, location_parent + "/Saved")
       elif x == 3:
           save(single_image, y)
       single_image.clean_all()


register(
   "save_all",
   "Save All",
   "Saves all opened images",
   "DoggoOfSpeed",
   "DoggoOfSpeed",
   "2023",
   "<Image>/Save All Images",
   "RGB*, GRAY*",
   [
       (PF_OPTION, "path_type", "Save Path", 0, ("Next to Original",
        "In Sibling Folder", "In Parent", "In Custom Folder")),
       (PF_DIRNAME, "custom_path", "Custom Path", os.getcwd())
   ],
   [],
   save_all)

main()
For some reason, the function save_all() receives 4 arguments instead of the 2 requires (I added the variables x and y to accommodate that). Not only that, those variables actually contain the data from the PF_Option (in y) and PF_Dirname (in x) when I'd expect them to be in path_type and custom_path respectively. So my question is why and how do the 2 unwanted parameters appear there?
Reply
#2
(06-07-2023, 12:03 PM)DoggoOfSpeed Wrote: Tried to create my first script to save all open files and while it works, I've encountered a strange issue.
For some reason, the function save_all() receives 4 arguments instead of the 2 requires (I added the variables x and y to accommodate that). Not only that, those variables actually contain the data from the PF_Option (in y) and PF_Dirname (in x) when I'd expect them to be in path_type and custom_path respectively. So my question is why and how do the 2 unwanted parameters appear there?
I think that the first two arguments to save_all() should be image and layer, then your dialog arguments (from PF_OPTION and PF_DIRNAME) follow.
Reply
#3
(06-07-2023, 04:35 PM)programmer_ceds Wrote: I think that the first two arguments to save_all() should be image and layer, then your dialog arguments (from PF_OPTION and PF_DIRNAME) follow.

Yeah, that's pretty much what I found out as well... To be honest all is fine as I got it to work, but I'm just interested what causes this behavior as the script I took inspiration from (Github link) was able to proceed with just the two arguments for some reason. I wonder what is the difference causing this.

Also, sorry for the non-descriptive title :/
Reply
#4
This is because you are using the "old style" registration model (where the 7th arg is the complete menu path).

Code:
register(
    id,
    desc,help,
    author,author,'2023',
    '<Image>/Test/The menu item',  # Full menu path
    imageFilter,
    [argsIn],
    [argsOut],
    function
    )

In the new style, the 7th arg is only the menu item label, and there is an additional keyword argument menu= that specifies the menu hierarchy:

Code:
register(
    id,
    desc,help,
    author,author,'2023',
    'The menu item',  # Menu label
    imageFilter,
    [argsIn],
    [argsOut],
    function,
    menu='<Image>/Test'  # Menu hierarchy
    )

and when you use it, the image/drawable parameters are no longer implicit, and must be specified. But they are set automatically from current Image/drawable and are not shown  in the parameter dialog when Gimp can figure them out.
Reply


Forum Jump: