photoshop-python-api icon indicating copy to clipboard operation
photoshop-python-api copied to clipboard

How to determine whether a artLayer is a layer or a layer group

Open gucheng599 opened this issue 1 year ago β€’ 1 comments

Describe the bug We know that app.activeDocument.layers usually represent a collection of layers and layerSets How do I know if the current layer is a layerSet when I traverse app.activeDocument.layers

To Reproduce Steps to reproduce the behavior: In fact, using JavaScript directly can easily achieve this, but how can I use the Photoshop Python API to do it JavaScript example:

var layers = app.activeDocument.layers;
if (layers[1].typename == "LayerSet"){
    alert("this is layer group")
} else {
    alert("this is layer")
}

but, in photoshop-python-api, Typename is not a meta attribute of layer, but a fixed class name

# my demo
import photoshop.api as ps
app = ps.Application()
doc = app.activeDocument

for layer in doc.layers:
    print(layer.typename)

# in photoshop\api\_core.py L108
    @property
    def typename(self) -> str:
        """str: Current typename."""
        return self.__class__.__name__

I tried to use layer.app.typename to get the typename in the metadata of the layer, but got nothing. How can I determine if the layer is actually a layer group

this is bad example

import photoshop.api as ps
app = ps.Application()
doc = app.activeDocument

for layer in doc.layers:
    print(layer.app.typename)

Expected behavior As stated in the title

Screenshots my screenshots: image image

Desktop (please complete the following information):

  • OS: Windows11
  • Photoshop Version: Photoshop-2022
  • Python Version: python3.8

gucheng599 avatar Sep 05 '24 03:09 gucheng599

I think I know how to judge now

active_document = self.app.activeDocument
layer_group_id_list = []
layer_group_list = active_document.layerSets
for layer_group in layer_group_list:
    layer_group_id_list.append(layer_group.id)
layer_list = active_document.layers
for layer in layer_list:
    if layer.app.id in layer_group_id_list:
        print("this is layer group")
    else:
        print("this is layer")

But I still have one questions

# photoshop\api\_layers.py L41
def __iter__(self):
    for layer in self._layers:
        yield ArtLayer(layer)

# photoshop\api\_layerSets.py L24
def __iter__(self):
    for layer_set in self.app:
        yield layer_set

Why isn't the COM object instantiated in layerset

gucheng599 avatar Sep 05 '24 12:09 gucheng599

https://github.com/loonghao/photoshop-python-api/issues/358#issuecomment-2809206355

same problem! πŸ‘

@gucheng599 @loonghao

laughingg avatar Apr 16 '25 11:04 laughingg

Bot detected the issue body's language is not English, translate it automatically. πŸ‘―πŸ‘­πŸ»πŸ§‘β€πŸ€β€πŸ§‘πŸ‘«πŸ§‘πŸΏβ€πŸ€β€πŸ§‘πŸ»πŸ‘©πŸΎβ€πŸ€β€πŸ‘¨πŸΏπŸ‘¬πŸΏ


https://github.com/loonghao/photoshop-python-api/issues/358#issuecomment-2809206355

same problem! πŸ‘

@gucheng599 @loonghao

Issues-translate-bot avatar Apr 16 '25 11:04 Issues-translate-bot