Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Constraint: Layer Permutations #89

Open
monyarm opened this issue Aug 26, 2020 · 5 comments
Open

New Constraint: Layer Permutations #89

monyarm opened this issue Aug 26, 2020 · 5 comments

Comments

@monyarm
Copy link

monyarm commented Aug 26, 2020

I'd like to request a constraint to export layer group permutations,
for example if i have the structure

a
-1
-2
b
-1
-2

I'd like to get a1b1.png ,a1b2.png ,a2b1.png and a2b2.png.
Would such a feature be possible?

@khalim19
Copy link
Collaborator

Hi, could you elaborate on how the layers should be combined? Not sure if I understand - I'm assuming that for a1b1.png, a1 is the background/foreground?

Either way, I'll think about it, although this seems to be a candidate for a completely separate plug-in that creates these permutations and then Export Layers would be used to export all the created combinations.

@monyarm
Copy link
Author

monyarm commented Sep 17, 2022

a and b are layer groups, while the digits are the actual layers in those groups.

@khalim19
Copy link
Collaborator

khalim19 commented Sep 17, 2022

So when I take 1 from a and 1 from b, then a single layer a1b1 should be produced and exported? My question is, how they should be combined together, e.g. "first insert a1, then insert b1, then merge these two layers into one".

@monyarm
Copy link
Author

monyarm commented Sep 17, 2022

The way they're ordered and combined would be how gimp normally does layers.
And you wouldn't be creating a layer a1b1, but rather would be disabling all layers except a1 and b1, and exporting that.

@khalim19
Copy link
Collaborator

You could try inserting the following code into Filters -> Python-Fu -> Console:

import itertools
import os


def get_layers_from_group(group):
    layers = []
    inner_groups = [group]
    
    while inner_groups:
        inner_group = inner_groups[0]
        del inner_groups[0]
        for child in inner_group.children:
            if isinstance(child, gimp.GroupLayer):
                inner_groups.append(child)
            else:
                layers.append(child)
    
    return layers


# output directory of your choosing
output_dirpath = r'C:\Users\khalim\Downloads\test'

# This assumes that the first image opened is the image you want to export layers from.
image = gimp.image_list()[0]

groups = image.layers
layers_per_group = []

for group in groups:
    layers_per_group.append(get_layers_from_group(group))

layer_visible_pairs = list(itertools.product(*layers_per_group))

for layer1, layer2 in layer_visible_pairs:
    for layer in layers:
        layer.visible = False
    
    layer1.visible = True
    layer2.visible = True
    
    image_copy = pdb.gimp_image_duplicate(image)
    final_layer = pdb.gimp_image_merge_visible_layers(image_copy, 0)
    
    filename = '-'.join([layer1.name, layer2.name]) + '.png'
    
    pdb.file_png_save(image_copy, final_layer, os.path.join(output_dirpath, filename), filename, 0, 9, 0, 0, 0, 0, 0)
    
    pdb.gimp_image_delete(image_copy)

This actually does not use Export Layers at all. If you still need to leverage additional features from Export Layers (such as custom procedures), I think the only way to achieve this is to:

  1. move groups A and B under a single layer group
  2. in Export Layers, uncheck Include layers and check Include layer groups and Only top-level layers
  3. save settings (Settings -> Save Settings).
  4. in the code above, replace the pdb.file_png_save(... line with
pdb.plug_in_export_layers(image, 'png', output_dirpath, None, 0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants