GIMP Chat
http://gimpchat.com/

How to read out if a layer exsits
http://gimpchat.com/viewtopic.php?f=9&t=19559
Page 1 of 1

Author:  TerraX [ Sun Aug 22, 2021 10:16 am ]
Post subject:  How to read out if a layer exsits

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:
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
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
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?
if layer == TRUE:
if layer != NULL:
if layer != -1:

doesn't work.

Author:  TerraX [ Sun Aug 22, 2021 12:14 pm ]
Post subject:  Re: How to read out if a layer exsits

Sometimes it helps not to give up searching.
I found some example 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
gimp.displays_flush()


Sorry in case anyone feels annoyed.

Author:  gasMask [ Sun Aug 22, 2021 12:21 pm ]
Post subject:  Re: How to read out if a layer exsits

Hi TerraX,

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

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

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


# 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.
is_valid = pdb.gimp_item_is_valid(layer)

Author:  ofnuts [ Mon Aug 23, 2021 2:26 am ]
Post subject:  Re: How to read out if a layer exsits

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):

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


Once you have a tattoo:
# 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)

Author:  gasMask [ Mon Aug 23, 2021 4:04 pm ]
Post subject:  Re: How to read out if a layer exsits

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

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/