Switch to full style
Post all Gimp scripts and script writing questions here
Post a reply

How to read out if a layer exsits

Sun Aug 22, 2021 10:16 am

Hi folks,

I'm new on writing plugins and maybe you can help.
I can create a new layer of all visible and insert it as new top layer by:
Code:
layer = pdb.gimp_layer_new_from_visible(image, image, "VisMerge")

I want to insert it outside any layergroup at the top position of the stack
Code:
pdb.gimp_image_insert_layer(image, layer, 0, 0)

For whatever reason it does not work.

Later on, I want to remove the layer before I modify again and create a new layer.
When I run this code the first time, the layer obviously cannot yet exist, so
Code:
layer = pdb.gimp_image_get_layer_by_name(image, "VisMerge")
pdb.gimp_image_remove_layer(image, layer)

gives me an error.

Can I check somehow if "layer" is a valid layer or not?
Code:
if layer == TRUE:
if layer != NULL:
if layer != -1:

doesn't work.

Re: How to read out if a layer exsits

Sun Aug 22, 2021 12:14 pm

Sometimes it helps not to give up searching.
I found some example code:

Code:
   
def layer_exists(self, layer):
      return layer != None and layer in self.img.layers


The insert code was also correct, I had an error somewhere else in the code.

But I found out, that it was not what I was actually looking for.
The update of the display I wanted to reach is easier to be realized with
Code:
gimp.displays_flush()


Sorry in case anyone feels annoyed.

Re: How to read out if a layer exsits

Sun Aug 22, 2021 12:21 pm

Hi TerraX,

With the code:
Code:
pdb.gimp_image_insert_layer(image, layer, 0, 0)

The third parameter is probably wrong. There are two things I would try.

Code:
# The image has layers.
# Insert the layer on top of the layer stack:
pdb.gimp_image_insert_layer(image, layer, image.layers[0], 0)


Code:
# The image does not have layers.
# Insert the layer where there are none:
pdb.gimp_image_insert_layer(image, layer, None, 0)


This code will let you know if the layer is valid.
Code:
is_valid = pdb.gimp_item_is_valid(layer)

Re: How to read out if a layer exsits

Mon Aug 23, 2021 2:26 am

The proper way to know if a given object exists is to "tattoo" it. You can then query/retrieve it using the tattoo. This doesn't rely on its position or on the assumption that the name hasn't changed. The tattoo is saved with the XCF, so s valid across editing sessions.

A "tattoo" is a 32-bit integer, so you have to come up with one. If this is during a single execution of your script, any random number (one drawn using the "random" module) will do. But if is has to be reused across several executions of the script this would have to be kept somewhere and this wouldn't be to practical.

You can also draw such a number when you write your script and then hard-code it in the script.

So the next best thing is to obtain a "nearly random" number from a source that will always give you the same result, for instance by hashing the executable name (so the tattoo value will remain the same as long a the script is not renamed):

Code:
import sys,os.path
tattoo=hash(os.path.basename(sys.argv[0])) & 0xFFFFFFFFF


Once you have a tattoo:
Code:
# set the tattoo:
pdb.gimp_item_set_tattoo(layer,1234)
# find by tattoo:, returns None if not found
layer = pdb.gimp_image_get_layer_by_tattoo(image,tattoo)

Re: How to read out if a layer exsits

Mon Aug 23, 2021 4:04 pm

I find the information about the layer tattoo's to be very useful. Thank you for sharing.
Post a reply