It is currently Fri Jul 05, 2024 12:50 pm


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: How to read out if a layer exsits
PostPosted: Sun Aug 22, 2021 10:16 am  (#1) 
Offline
New Member

Joined: Aug 22, 2021
Posts: 4
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.


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: How to read out if a layer exsits
PostPosted: Sun Aug 22, 2021 12:14 pm  (#2) 
Offline
New Member

Joined: Aug 22, 2021
Posts: 4
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.


Top
 Post subject: Re: How to read out if a layer exsits
PostPosted: Sun Aug 22, 2021 12:21 pm  (#3) 
Offline
GimpChat Member
User avatar

Joined: Jul 04, 2019
Posts: 136
Location: Lake Havasu City, Arizona, USA
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)

_________________
Charles


Top
 Post subject: Re: How to read out if a layer exsits
PostPosted: Mon Aug 23, 2021 2:26 am  (#4) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4756
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)

_________________
Image


Top
 Post subject: Re: How to read out if a layer exsits
PostPosted: Mon Aug 23, 2021 4:04 pm  (#5) 
Offline
GimpChat Member
User avatar

Joined: Jul 04, 2019
Posts: 136
Location: Lake Havasu City, Arizona, USA
I find the information about the layer tattoo's to be very useful. Thank you for sharing.

_________________
Charles


Top
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Krita development sprint report (interesting read)

5

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

0

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 Add layer mask hides the entire layer

3



* Login  



Powered by phpBB3 © phpBB Group