• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Javascript get all Layers inside a Layer Group tree

Participant ,
Sep 11, 2015 Sep 11, 2015

Copy link to clipboard

Copied

Hi there,

How can I get all the Layers inside a group tree from an extension?

I have the following tree:

Group1

> Layer 1

> Layer 2

> Group 2

> > Layer 3

> > Layer 4

> > Group 3

> > > Layer 5

> > > Layer 6

> Layer 3

So in this case I should get this: Layer 1, Layer 2, Layer 3, Layer 4, Layer 5, Layer 6

Thanks in advance

Views

3.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 15, 2015 Sep 15, 2015

Copy link to clipboard

Copied

LATEST

You need to use a recursive function then you do whatever you want with all the layers in the array layerList:

var doc = activeDocument;

var layerList = new Array()

getLayers(doc);

function getLayers(lSet){

    for(var i=0;i<lSet.layers.length;i++){

        doc.activeLayer = lSet.layers;

        if(doc.activeLayer.typename == 'LayerSet'){

            if(doc.activeLayer.layers.length>0){

                getLayers (doc.activeLayer)

                }

            }

        else{

            layerList.push(doc.activeLayer)

            }       

        };//end loop

    };//end function

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines