loonghao / photoshop-python-api

Python API for Photoshop.
https://loonghao.github.io/photoshop-python-api/
MIT License
641 stars 73 forks source link

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

Closed gucheng599 closed 3 weeks ago

gucheng599 commented 2 months ago

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

gucheng599 commented 2 months ago

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