Gimp - Easy way to make many layers visible?
Asked Answered
B

1

17

In Gimp, I've created a .xcf file that consists of some 200 layers. Some are visible and some not. Now I want to create a picture that consists of all layers, so I have to make all layers visible. Later I'll have to return to the state where some layers are visible and some not. How can I achieve this without clicking several hundred clickboxes for visibility?

Briolette answered 22/10, 2015 at 6:7 Comment(0)
C
27

Shift+Click on the eye icon (eycon?) of a layer in the layers dialog, or the place where it should be, if the layer is currently invisible.

This will:

  • make the layer you are clicking visible
  • make all other layers invisible by the first click, and visible by the next click

See http://docs.gimp.org/2.8/en/gimp-dialogs-structure.html#gimp-layer-dialog

To get back to the previous state, I'd use File->Revert, this discards any changes and reloads the file from disk

But...

... this is Stack Overflow, so we need to do this in code...

I'd suggest to use the Python console in GIMP, Filters->Python-Fu->Console. Assuming the image is the only one you're working on, the following code sets all of its layers to be visible:

pdb.gimp_image_undo_group_start(gimp.image_list()[0])
for layer in gimp.image_list()[0].layers:
    layer.visible = True

pdb.gimp_image_undo_group_end(gimp.image_list()[0])

The code's main part is a loop over all layers of the image, setting them to visible. The loop is wrapped into an undo group, allowing for easy undo of all visiblity changes in one single step.

But... Layer groups?

Yes, we're not quite there yet.

If your image uses layer groups, you will notice that the above code will make any layer not in a group and the groups themselves visible, but it won't affect any layer in a group.

We can tell whether a layer we encounter in that for loop is a layer group - pdb.gimp_item_is_group(layer) will return true for those. So while iterating, we could check if the current item is a group, and start iterating over its children.

Python has nifty way for filtering lists (and gimp.Image.layers is one) by an arbitrary boolean filter-expression, and we got one of those, see above.

So instead of complicating our current loop with additional if statements, we can do this:

pdb.gimp_image_undo_group_start(gimp.image_list()[0])

# iterate layer groups
for group in [group for group in gimp.image_list()[0].layers if pdb.gimp_item_is_group(group)]:
    # you want a group.name check here to pick a specific group
    for layer in group.layers:
        layer.visible = True

# iterate non-group layers
for layer in gimp.image_list()[0].layers:
    layer.visible = True

pdb.gimp_image_undo_group_end(gimp.image_list()[0])

But... Nested layer groups?

Yes, still not quite there - if you have nested layer groups. The code just above only gets into the first level of groups, and won't affect any layer in a deeply nested group structure.

This is where a recursive procedure will be more useful than iterative loops, so stay tuned for an additional update.

Classmate answered 23/10, 2015 at 23:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.