Skip to content

Instantly share code, notes, and snippets.

@gorenje
Last active February 24, 2023 20:11
Show Gist options
  • Save gorenje/4064292be119a466f01f8005a5a7cb49 to your computer and use it in GitHub Desktop.
Save gorenje/4064292be119a466f01f8005a5a7cb49 to your computer and use it in GitHub Desktop.
Gimp 2.10 python plugin for moving all visible layers into an existing or new layer group
This plugin moves all visible, non-group layers into an existing layer group (if that is the actie layer) or a new group.
The idea is you make all layers visible that you want to move into an existing group or a new group and then call this plugin.
To install it, move the python code into the plug-ins directory of gimp and restart gimp. The plugin is then available via
<ToolBar> > Layer > Move > Move Visible To Group...
Hope it helps!
P.S. Motivation was that you can only move one layer at a time in gimp, so that makes it painful moving more than 2 layers into
a layer group .... ;)
import os
from gimpfu import *
def _rec(layers, lst):
for l in lst:
if not l.visible: continue
if pdb.gimp_item_is_group(l): _rec(layers,l.layers)
else: layers.append(l)
return layers
def _get_layers(image, dont_recurse=False):
layers = None
if dont_recurse:
layers = [layer for layer in image.layers if (
layer.visible and (not pdb.gimp_item_is_group(layer)))]
else:
layers = _rec([], image.layers)
return (layers, len(layers), 0)
def run(*args):
image, group_name = args
group_layer = image.active_layer
# selection = image.selection
gimp.progress_init("Moving Layers")
# Since we're moving stuff into groups, don't recurse into existing
# groups, just use the non-group, visible, top-level layers.
layers, total_layers, cnt = _get_layers(image, dont_recurse=True)
mod_lmt = int(total_layers * 0.20)
pdb.gimp_image_undo_disable(image)
if group_layer == None or (not pdb.gimp_item_is_group(group_layer)):
group_layer = gimp.GroupLayer(image)
group_layer.name = group_name
pdb.gimp_image_insert_layer(image, group_layer, None,0)
for layer in layers:
if layer == group_layer: continue
orig_name = layer.name
ly2 = pdb.gimp_layer_copy(layer,True)
pdb.gimp_image_insert_layer(image, ly2, group_layer, 0)
pdb.gimp_image_remove_layer(image,layer)
ly2.name = orig_name
if cnt % mod_lmt == 0: gimp.progress_update(cnt / total_layers)
cnt += 1
pdb.gimp_progress_end()
pdb.gimp_image_undo_enable(image)
register(
"layers_move_visible_to_new_group",
"Move To Layer Group",
"Move all visible layers to a new Layer Group",
"Osmond Oswald <gimp@plugin.dildodonkey.de>",
"Osmond Oswald <gimp@plugin.dildodonkey.de>",
"20-02-2020",
"Move Visible To Group...",
"*",
[
(PF_IMAGE, "arg0", "Image To Use", None),
(PF_STRING, "arg1", "New Group Name", "New Group"),
],
[],
run, menu="<Image>/Layer/Move"
)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment