Search code examples
gimppython-fu

GIMP Python-fu nested group layers


I can't seem to find any methods for adding a Group Layer to another Group Layer anywhere in the python-fu interface.

I've tried to find methods on the Gimp.Layer objects as well but with no luck.

How do I add a group layer to another group layer with python-fu?


Solution

  • The support to layer groups on Python-fu was added on the last minutes before 2.8 release, and is rather incomplete.

    So, the only way to create a proper layer group in GIMP 2.8 is to use the pdb call:

    group = pdb.gimp_layer_group_new(img)
    group.name = "my group"
    

    (Using the img.GroupLayer call is buggy on gimp 2.8 - should be the way to go in the future)

    Once you have your group, you can insert it anywhere on the image using a

    pdb.gimp_image_insert_layer(<img>, <layer>, <parent>, <position>)
    

    Like in:

    >>> img = gimp.Image(640, 480, RGB)
    >>> pdb.gimp_display_new(img)
    <display>
    >>> parent_group = pdb.gimp_layer_group_new(img)
    >>> child_group_1 = pdb.gimp_layer_group_new(img)
    >>> child_group_2 = pdb.gimp_layer_group_new(img)
    >>> grand_child_group = pdb.gimp_layer_group_new(img)
    >>> img.add_layer(parent_group, 0)
    >>> pdb.gimp_image_insert_layer(img, child_group_1, parent_group,0)
    >>> pdb.gimp_image_insert_layer(img, child_group_2, parent_group,1)
    >>> pdb.gimp_image_insert_layer(img, grand_child_group, child_group_1,0)
    >>> l1 = gimp.Layer(img, "test", 320,240)
    >>> pdb.gimp_image_insert_layer(img,l1, grand_child_group,0)
    

    So, indeed, there is this extreme API asymmetry, in which you add layers and groups to the image through an "add_layer" method on the parent, but have to add either to a layer group, you have to go troguh the pdb.gimp_image_insert_layer call.

    update (Feb/2015) - The bug for gimp.GroupLayer() is fixed in GIMP's git and it will work properly from GIMP 2.8.16 onward. Now all one has to do to add a new group layer is:

    >>> g = gimp.GroupLayer(img)
    >>> pdb.gimp_image_insert_layer(img, g, None, 0)