Python-fu GIMP script do not paste image

1.4k views Asked by At

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?

1

There are 1 answers

2
jsbueno On BEST ANSWER

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.