Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Plug-in that Changes SubLayers Visibility and Export Images as PNG Files
#1
Python 
Hello everybody!
I’m not very good not in GIMP, nor in Python, nor English, so please excuse me for my mistakes)

I have a .xcf file with a lot of layers and sublayers, for example:

(visible)        Layer_1
(visible)            Sublayer_1.1
(invisible)          Sublayer_1.2
(visible)        Layer_2
(invisible)        Sublayer_2.1
(invisible)        Sublayer_2.2
(invisible)        Sublayer_2.3
(visible)        Layer_3

I need to change the visibility of each sublayer in a selected layer in turn and export the entire image with all visible layers.

For example:

(visible)        Layer_1
(visible)            Sublayer_1.1
(invisible)          Sublayer_1.2
(visible)        Layer_2
(visible)         Sublayer_2.1
(invisible)        Sublayer_2.2
(invisible)        Sublayer_2.3
(visible)        Layer_3

Export image_2.1.png

then

(visible)        Layer_1
(visible)            Sublayer_1.1
(invisible)          Sublayer_1.2
(visible)        Layer_2
(invisible)      Sublayer_2.1
(visible)         Sublayer_2.2
(invisible)        Sublayer_2.3
(visible)        Layer_3

Export image_2.2.png

then 

(visible)        Layer_1
(visible)            Sublayer_1.1
(invisible)          Sublayer_1.2
(visible)        Layer_2
(invisible)        Sublayer_2.1
(invisible)      Sublayer_2.2
(visible)         Sublayer_2.3
(visible)        Layer_3

Export image_2.3.png


The best that I did with the help of ChatGPT is this plug-in in Python:

Code:
#!/usr/bin/env python

from gimpfu import *

def export_sublayers(image, drawable, layer_name, path, prefix):
   # Find the target layer
   target_layer = None
   for layer in image.layers:
       if layer.name == layer_name:
           target_layer = layer
           break

   # If the target layer is found
   if target_layer:
       # Iterate through sublayers
       for sublayer in target_layer.children:
           # Make the current sublayer visible and others invisible
           for child in target_layer.children:
               pdb.gimp_item_set_visible(child, child == sublayer)

           # Export the image
           path = path + "\\" if not path.endswith("\\") else path
           filename = path + prefix + "_" + sublayer.name + ".png"
           pdb.file_png_save_defaults(image, image.active_layer, filename, filename)

       # Make all sublayers visible again
       for sublayer in target_layer.children:
           pdb.gimp_item_set_visible(sublayer, True)

register(
   "python_fu_combine_layers_and_export",
   "Combine layers and export",
   "Combine layers and export",
   "Your Name",
   "Your Name",
   "2024",
   "<Image>/File/Combine layers and export",
   "*",
   [
       (PF_STRING, "layer_name", "Layer name", ""),
       (PF_DIRNAME, "path", "Export path", "/path/to/export/"),
       (PF_STRING, "prefix", "Prefix", "")
   ],
   [],
   export_sublayers)

main()

But this plug-in exports only sublayers in a selected layer and I can't make it export the entire image with all visible layers.

Is anybody able to help me with it, please?

Or maybe are there other ways to do it?

Many thanks for any help!
Reply
#2
Looks like my ofn-export-layer-combinations script
Reply
#3
(03-02-2024, 01:45 AM)Ofnuts Wrote: Looks like my ofn-export-layer-combinations script

That's exactly what I need!

Thank you a lot!
Reply


Forum Jump: