1

I'm trying to write a script that slice a layer into small peaces. It run correctly but nothing is pasted into a new layer.

def explode_layer( i, l, dx, dy ):
    T=[]
    for ix,ox in enumerate(range(l.offsets[0], l.offsets[0]+l.width, dx )):
        for iy,oy in enumerate(range(l.offsets[1], l.offsets[1]+l.height, dy)):
            pdb.gimp_rect_select(i, ox, oy, dx, dy, 2, False, 0)
            if not pdb.gimp_edit_copy(l):
                continue
            layer = pdb.gimp_layer_new(i, dx, dy, 1, 
                                       l.name+" %d,%d"%(ix,iy), 100, 0)
            i.add_layer(layer)
            floating_sel = pdb.gimp_edit_paste(layer, True)
            pdb.gimp_layer_set_offsets(floating_sel, *layer.offsets)
            pdb.gimp_floating_sel_anchor(floating_sel)
            T.append(layer)
    return T

I use gimp 2.6.8 on Ubuntu 10.04. How can I fix it? Is there a better approach?

Arpegius
  • 5,817
  • 38
  • 53
  • Besides your main question, there are a few issues with your code, whcih can generate maintanance headaches later on - some of which could be huge: Avoid naming variables with single letters - specially fully fledged objects like the i for the "image" and "l" for layer. Above all - don't use the single letter "l" for a variable name - ever - it is too hard to distinguish it from "1" for it to be confortable. 2) Avoid using "magic numbers" - GIMP Python does define the constantes, use them - so using "CHANEL_OP_REPLACE" instead of the number "2" is the way to make the script future proof. – jsbueno May 24 '12 at 02:54
  • Thanks for advice, I am not going to write a plug-in, rather than just use it in python-fu console. If I got better with scripting gimp, I would consider to make one. – Arpegius May 24 '12 at 03:05
  • ok - such shortcuts are fine for "run once" console window - however, this code, once fixed, is a plug-in already - you will just have to write the "register" fucntion call for it. – jsbueno May 24 '12 at 03:10

1 Answers1

1

I am taking a look at your script now - it is a good approach -and I found out what is wrong: when you call gimp_edit_paste, the selection you created (with gimp_rect_select) is still active, and the contents of your floated layer are clipped by it. (Actually I think they are clipepd just at the "selection_anchor" call, but that is irrelevant).

So, adding a pdb.gimp_selection_none(i) line just before floating_sel = pdb.gimp_edit_paste(layer, True) fixes your function:

def explode_layer( i, l, dx, dy ):
    T=[]
    for ix,ox in enumerate(range(l.offsets[0], l.offsets[0]+l.width, dx )):
        for iy,oy in enumerate(range(l.offsets[1], l.offsets[1]+l.height, dy)):
            pdb.gimp_rect_select(i, ox, oy, dx, dy, 2, False, 0)
            if not pdb.gimp_edit_copy(l):
                continue
            layer = pdb.gimp_layer_new(i, dx, dy, 1, 
                                       l.name+" %d,%d"%(ix,iy), 100, 0)
            i.add_layer(layer)
            pdb.gimp_selection_none(i)
            floating_sel = pdb.gimp_edit_paste(layer, True)
            pdb.gimp_layer_set_offsets(floating_sel, *layer.offsets)
            pdb.gimp_floating_sel_anchor(floating_sel)
            T.append(layer)
    return T

A simpler way, is not to create a new layer explicitly (btw, there is a handy, though undocumented "new_layer" method on the image object which creates and adds a new layer, and has sane defaults for most parameters - so, i.new_layer(<name>, <width>, <height>) would suffice) - but you can simply copy, paste, and them call new_layer = pdb.gimp_floating_sel_to_layer(<floating_sel>) instead.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • Is there a `gimp_floating_sel_*` method that paste to a new layer? I didn't find one. – Arpegius May 24 '12 at 02:52
  • Thanks it work well. I forget that pasting use selection too, also if I move the new layer to the floating one, the selection is right over the new layer. In documentation of `gimp_floating_sel_to_layer`, there's nothing about creating new layer so I just missed it. – Arpegius May 25 '12 at 08:33