Script - Know if layer is background or not

Moderator: Kathy_9

Post Reply
TripleHeinz
Posts: 12
Joined: Tue Oct 09, 2012 5:51 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Gigabyte GA-B85N-WIFI
processor: Intel Core i3-4330 [3.5 Ghz]
ram: 4 GB
Video Card: nVidia GeForce GTX 750 Ti [EVGA 02G-P4-3751-KR]
sound_card: Realtek ALC898
Hard_Drive_Capacity: 960GB
Monitor/Display Make & Model: LG E2360 [1920x1080p]
Corel programs: PSP (7, 9, X5, X7), VS (X7)

Script - Know if layer is background or not

Post by TripleHeinz »

Hi,

I have a script that merge (Flatten) all layers but if an image has a single layer and that layer is a background layer then my script fails.
What comes to my mind is to get a layer count and if that count is 1 then check if that layer is a background layer, if it is then do not merge.

How can i get a layer count and how can i get the layer type of a specific layer?
Are there more resources about scripting in PSP, a library reference or something?

My script is for PSP9 and PSPX5, any help would be great.

Thank you!
LeviFiction
Advisor
Posts: 6831
Joined: Thu Oct 02, 2008 1:07 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Alienware M17xR4
processor: Intel Core i7-3630QM CPU - 2_40GH
ram: 6 GB
Video Card: NVIDIA GeForce GTX 660M
sound_card: Sound Blaster Recon3Di
Hard_Drive_Capacity: 500GB
Corel programs: PSP: 8-2023
Location: USA

Re: Script - Know if layer is background or not

Post by LeviFiction »

Two ways that I know of to get the layer count.

1) First is to import PSPUtils or JascUtils and using the GetLayerCount( Environment, Doc ) command.

PSPUtils replaced JascUtils in later versions of PSP. However you can still use JascUtils even up to PSPX5 for backwards compatibility with older scripts. In this case since you want to use it with PSP 9 we'll use JascUtils.

Code: Select all

import JascUtils

#If LayerCount is just one
if JascUtils.GetLayerCount( Environment, App.TargetDocument ) == 1:
      return
Now if you want to make sure it's flat (no transparency) you will also want to check if it's a background layer. If you don't care, then this suggestion can be skipped.

If you want to also make sure it's a background layer before giving up you can include "LayerIsBackground (Environment, Doc)"

Code: Select all

import JascUtils

#IF LayerCount is just one and it is a background layer
if JascUtils.GetLayerCount( Environment, App.TargetDocument ) == 1 and JascUtils.LayerIsBackground( Environment, App.TargetDocument) == True:
      return
2) Or you can program these requests yourself. They are all returned by the basic info.

Code: Select all

#Grab number of layers.

ImageInfo = App.Do(Environment, 'ReturnImageInfo', {}, App.TargetDocument)
NumOfLayers = ImageInfo['LayerNum']

#Is Layer Background Layer

LayerInfo = App.Do( Environment, 'ReturnLayerProperties', {}, App.TargetDocument)
IsBackground = LayerInfo['IsBackground']
Hope that helps
https://levifiction.wordpress.com/
TripleHeinz
Posts: 12
Joined: Tue Oct 09, 2012 5:51 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Gigabyte GA-B85N-WIFI
processor: Intel Core i3-4330 [3.5 Ghz]
ram: 4 GB
Video Card: nVidia GeForce GTX 750 Ti [EVGA 02G-P4-3751-KR]
sound_card: Realtek ALC898
Hard_Drive_Capacity: 960GB
Monitor/Display Make & Model: LG E2360 [1920x1080p]
Corel programs: PSP (7, 9, X5, X7), VS (X7)

Re: Script - Know if layer is background or not

Post by TripleHeinz »

Hope that helps
Thank you!!! Yes, it worked as needed (PSP9 and PSPX5). Though i used a different logic:

Code: Select all

if JascUtils.GetLayerCount(Environment, App.TargetDocument) > 1 or JascUtils.LayerIsBackground(Environment, App.TargetDocument) == False:
    # Merge All
...
# Script flow
If there're more than one layer then MergeAll function is available, even if there's a background layer present. If the second expresion gets evaluated it means there's only one layer, but, if that single layer is NOT a background layer then MergeAll function is still available. If the single layer is already a background one then there's no need to flatten. In the end you'll always end up with a flat image no matter what. This logic allows the script to continue with the rest of the commands.
Post Reply