It is currently Wed Jul 24, 2024 4:28 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Script-fu add a layer from one image to another as a layer?
PostPosted: Sat Jan 25, 2014 1:45 pm  (#1) 
Offline
New Member

Joined: Jan 25, 2014
Posts: 2
I have two equal sized images (1 and 2) with a single layer each. I would have thought the following would add the layer from 1 to 2

  (gimp-image-insert-layer 2 (car (gimp-image-get-layers 1)) 0 -1)


but instead I get an error
Error: ( : 1) Procedure execution of gimp-image-insert-layer failed on invalid input arguments: Procedure 'gimp-image-insert-layer' has been called with an invalid ID for argument 'layer'. Most likely a plug-in is trying to work on a layer that doesn't exist any longer.


PS I'm not sure about the -1 but the documentation says
    (
      "position"
      "GIMP_PDB_INT32"
      "The layer position"
    )


and I for the life of me can't find where "GIMP_PDB_INT32" is documented (nor any of the types).

PPS The script-fu community doesn't seem to have a strong presence on stackoverflow. Is this the preferred place to ask questions?


Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: Script-fu add a layer from one image to another as a layer?
PostPosted: Sat Jan 25, 2014 2:30 pm  (#2) 
Offline
GimpChat Founder
User avatar

Joined: May 22, 2008
Posts: 5242
Location: Gimpville
Greetings and welcome to GC, togakangaroo.

gimp-image-get-layers returns a list of all the layers in an image. You would need to parse the list to find the single layer you're after. You might find it easier to use gimp-image-get-active-drawable for a single layer. If you've created the layer previously, you could have just saved it for use with gimp-image-insert-layer.

The last argument in gimp-image-insert-layer is the layer position in the layer stack, so it just depends on where you want it inserted, as to what that value would be.

And yes, this is a great site to get help with GIMP Scripting. There are lots of knowledgeable members here. ;)

_________________
“If you reach for the stars, you just might land on a decently sized hill.” - Stuart Hill


Top
 Post subject: Re: Script-fu add a layer from one image to another as a layer?
PostPosted: Sat Jan 25, 2014 2:39 pm  (#3) 
Offline
New Member

Joined: Jan 25, 2014
Posts: 2
Thanks for the reply. Does the fact that I do

(car (gimp-image-get-layers 1))


Not get the first (and in this case, only) layer? What is car returning then?

As for GIMP_PDB_INT32, I understand it means position - where can I find documentation on possible values for that (ie I just put -1 because I saw it in an example and it got me past an error, I have no idea what it actually means).


Top
 Post subject: Re: Script-fu add a layer from one image to another as a layer?
PostPosted: Sat Jan 25, 2014 3:53 pm  (#4) 
Offline
Script Coder
User avatar

Joined: Jun 22, 2010
Posts: 1171
Location: Here and there
You need to use the Procedure Browser (Help>>Procedure Browser) where the help for gimp-image-insert-layer says:

This procedure adds the specified layer to the image at the given position. If the specified parent is a valid layer group (See 'gimp-item-is-group' and 'gimp-layer-group-new') then the layer is added inside the group. If the parent is 0, the layer is added inside the main stack, outside of any group. The position argument specifies the location of the layer inside the stack (or the group, if a valid parent was supplied), starting from the top (0) and increasing. If the position is specified as -1 and the parent is specified as 0, then the layer is inserted above the active layer, or inside the group if the active layer is a layer group. The layer type must be compatible with the image base type.


And try not to use fixed numbers for the image and layer id's as it's not going to work the moment you open another image.

Kevin


Top
 Post subject: Re: Script-fu add a layer from one image to another as a layer?
PostPosted: Sat Jan 25, 2014 4:10 pm  (#5) 
Offline
GimpChat Founder
User avatar

Joined: May 22, 2008
Posts: 5242
Location: Gimpville
Kevin is right. Try to avoid using fixed numbers for the Image ID.

To copy a layer from an image (with only one layer) to another image, you could do something like this..

From your example, where the source-image=1 and the destination-image=2

(set! copied-layer (car (gimp-layer-new-from-drawable (car (gimp-image-get-active-drawable source-image)) destination-image)))
(gimp-image-insert-layer destination-image copied-layer 0 layer-position)

An alternate solution, using the method you were initially asking about, would go something like this..

(gimp-image-insert-layer destination-image (car (gimp-layer-new-from-drawable (car (vector->list (cadr (gimp-image-get-layers source-image)))) destination-image)) 0 layer-position)

The layer-position is the position in the layer stack: 0 to n layers, where 0 is the top layer.

_________________
“If you reach for the stars, you just might land on a decently sized hill.” - Stuart Hill


Top
 Post subject: Re: Script-fu add a layer from one image to another as a layer?
PostPosted: Sun Jan 26, 2014 12:02 am  (#6) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
togakangaroo wrote:
Thanks for the reply. Does the fact that I do

(car (gimp-image-get-layers 1))


Not get the first (and in this case, only) layer? What is car returning then?

'gimp-image-get-layers' returns a list containing two elements. The second element of the list is an array of layer-IDs, and the first element is the number of layer-IDs in the array.

The first element (the number of layer-IDs) is provided for those programming languages (such as C) that do not know the size of an array. Script-fu, however, is always able to determine the size of an array (or a list) without needing a separate variable to keep track of it. This means that you can safely ignore the first element of the return value.

The second element of the returned list (i.e., the actual array of layer-IDs) can be accessed by taking the 'car' of the 'cdr' of the returned list, which can be abbreviated to the 'cadr'. The PDB "int32array" is converted to a Script-fu 'vector' before it is returned, and there a few different procedures available for working with a Scheme vector. The most important of these are:
  • (vector-length v) -- return the number of elements in vector 'v'
  • (vector-ref v n) - return the n'th element in the vector. The parameter 'n' should be in the range 0 to one less than the number of elements in the vector (in other words, the 'vector-length'); with n=0 being the first element and n=vector-length - 1 being the last.
  • (vector-set! v n x) - set the value of the n'th element to 'x'. 'x' can be of any type you want, a number, a string, a list, even another vector.
  • (vector->list v) - return a list having the same elements as the vector 'v'.

So to find the first (or only) layer in an image, you could use the expression:
(vector-ref (cadr (gimp-image-get-layers image)) 0)

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Attachment(s) Adding a image as a layer, moving layer, and then flattening

2

No new posts How do I script a new white layer to the bottom of the layer stack

7

No new posts Shortcuts to 2.10 documentation re: Layer & Layer-group modes

0

No new posts Add layer mask hides the entire layer

3

No new posts Attachment(s) Unable to load an image as layer

16



* Login  



Powered by phpBB3 © phpBB Group