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

Script to cycle only trough all the layers in a layerset/group and stop

Explorer ,
Apr 27, 2017 Apr 27, 2017

Copy link to clipboard

Copied

Hello all

i need a script that will cycle only trough all layers contained in a group called "Colors" and stops once the last layer has been reached.

So far i do have a script that cycles trough ALL layers in the document, but i need it to cycle trough the defined Layerset/Group ONLY.

doc = app.activeDocument;

front = doc.layerSets.getByName("Front");

colors = front.layerSets.getByName("Colors");

cLayer = doc.activeLayer;

////////////////////////////////////////////////////////////////////////////////////

///////////////// FUNCTION TO HIDE ALL LAYERS IN LAYERSET "Colors" ////////////////

////////////////////////////////////////////////////////////////////////////////////

function resetColorsGroup() {

      for (var i = colors.layers.length - 1; i >= 0; i--)

        {

        colors.layers.visible = false; // Hide all Layers

        }

  };

////////////////////////////////////////////////////////////////////////////////////

/////////////// FUNCTION TO SELECT THE NEXT LAYER EVEN IF NOT VISIBLE //////////////

////////////////////////////////////////////////////////////////////////////////////

function activ (nLayer_f)

{

  var check = nLayer_f.visible;

  doc.activeLayer = nLayer_f;

  if (check == false)

  doc.activeLayer.visible = false;

}

var parL = doc.activeLayer.parent;

var parLln = parL.layers.length;

for(i=0; i<parLln;)

{

  if(parL.layers==cLayer)

  {

    try

    {

      nLayer = cLayer.layers[0];

    }

    catch(e)

    {

      if(i!=parLln-1)

      {

        nLayer=parL.layers[i+1];

      }

      else

      {

        upmem = parL;

        while (upmem!=doc && upmem.parent.layers[upmem.parent.layers.length-1]==upmem)

        {

          upmem = upmem.parent;

        }

        if (upmem==doc)

        {

          upmem=upmem.layers[0];

          var lastmem = 1;

        }

        for(k=0;k<upmem.parent.layers.length;)

        {

          if(upmem.parent.layers==upmem)

          {

            aa=k;

            if(lastmem==1)

            {aa=-1;}

            nLayer=upmem.parent.layers[aa+1];

            k=upmem.parent.layers.length;

          }

          else {k++;}

        }

      }

    }

    resetColorsGroup(); //Toggle visibility to OFF for all layers in the "Colors" layerset.

    activ (nLayer);

    i=parLln-1;

    var currentLayer = activeDocument.activeLayer;

    currentLayer.visible = !currentLayer.visible; // Make currently selected Layer Visible.

  }

  i++;

}

Thanks everyone, got stuck on this

TOPICS
Actions and scripting

Views

4.0K

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

correct answers 1 Correct answer

Community Expert , Apr 28, 2017 Apr 28, 2017

Here's a modified script. It will first turn off the visibility of all the layers in the Colors layer set. Then it will cycle back through and turn each one on, you insert whatever code you want to save the file at that point, then it turns the visibility off on that layer, and moves to the next.

#target photoshop

var doc = activeDocument;

var selectedGroup = doc.layers.getByName('Colors');

doc.activeLayer = selectedGroup;

var reset = true;

loopLayers (selectedGroup)//turn off visibility

reset = false;

...

Votes

Translate

Translate
Adobe
Community Expert ,
Apr 27, 2017 Apr 27, 2017

Copy link to clipboard

Copied

Your script seems more complicated that what I assume you want to do. You need a recursive function to loop through all the layers in a layerset if it contains other layersets:

#target photoshop

var doc = activeDocument;

var selectedGroup = doc.layers.getByName('Colors');

doc.activeLayer = selectedGroup;

loopLayers (selectedGroup)

function loopLayers(gp){

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

        if(gp.layers.typename =='LayerSet'){

            loopLayers (gp.layers);

            }

        else{

            gp.layers.visible = false;

            }

        }

    }

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
Explorer ,
Apr 28, 2017 Apr 28, 2017

Copy link to clipboard

Copied

Hello Chuck (and others)

i've tried to get your script working for what i need. Unfortunately i could not (my fault). Tried so many things now but could not get it working and help is highly appreciated.

I'll try to explain again what i need with some pseudo code:

select First layer in Group "Colors"  (i.E. "Green") and make only this Color/Layer visible.

{Play function "create"};

select next layer in Group "Colors" (i.E. "Yellow") and make only this Color/Layer visible.

{Play function "create"};

Repeat until all layers in Group "Colors" have been processed with function "create" (Amount and Names of layers in Group "Colors" is not consistent).

Screenshot_28_04_17__12_19.jpg

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
Community Expert ,
Apr 28, 2017 Apr 28, 2017

Copy link to clipboard

Copied

The Thing is that all layers in a docent can be processed.  It is usually done using a recursive function that process the document object passed.  If you passed the function a group instead of a document it would process the Group Object all the layers and other groups that may be in within a group.  You could write a function to ignore  groups in the object passes and just process the layers in the group,  However all the layers in the lower groups ignored are logically in the group you processes where the visibility can be controlled by the lower groups and layers visibility settings. 

JJMack

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
Community Expert ,
Apr 28, 2017 Apr 28, 2017

Copy link to clipboard

Copied

So you need to be able to determine what the coloe code of each layer is, and run sime code accordingly?

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
Explorer ,
Apr 28, 2017 Apr 28, 2017

Copy link to clipboard

Copied

https://forums.adobe.com/people/Chuck+Uebele  schrieb

So you need to be able to determine what the coloe code of each layer is, and run sime code accordingly?

Thanks Chuck. Not really I try to explain again:

  1. Group "Colors" contains solids which act as Background for content above the Group.
  2. Toggle only the Green Background to visible in Group "Colors" and run block of code named "Create" (apply filters and then save a jpg)
  3. Toggle only the Yellow Background to visible and run the same "Create" code/function again.
  4. Repeat this step for each Layer available in the "Colors" Group.
  5. Stop the loop (i.E. "Red") has been processed by the "Create" code/function.

Currently i have the following code that will check for the currently visible layer in the "Colors" group and select it.

After each repetition it will select the next layer in the "Colors" group and makes only the newly selected visible.

Code has been put together from snippets and could be done easier and is not yet ready to work as it should.

stepTroughColors()

function stepTroughColors(){

 

var doc = activeDocument;

var front = doc.layerSets.getByName("Front");

var colors = front.layerSets.getByName("Colors");

var cLay = doc.activeLayer; 

var layNum  

var aLay = new Array()

//var i = [0]

 

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

   

    theLayer = colors.layers;

    if(theLayer.visible == true){

    doc.activeLayer = theLayer; // Sets the visible Layer in "Colors" as active Layer.

    activeColor = theLayer.name; // Variable "activeColors" to use the Layername in the Save jpg actions.

    }

    aLay = colors.layers.name;

    if(aLay==cLay.name){

    layNum = i;

    colors.layers.visible = false;

      }

      if(doc.activeLayer.name.indexOf("END") >= 0){

      alert ("All Colors Have Been Processed and Saved!");

      break;

      }

    };

try{ 

    doc.activeLayer = colors.layers[layNum+1]; 

    doc.activeLayer.visible = true;

    alert (activeColor)

    } 

catch(e){} 

};

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
Community Expert ,
Apr 28, 2017 Apr 28, 2017

Copy link to clipboard

Copied

Here's a modified script. It will first turn off the visibility of all the layers in the Colors layer set. Then it will cycle back through and turn each one on, you insert whatever code you want to save the file at that point, then it turns the visibility off on that layer, and moves to the next.

#target photoshop

var doc = activeDocument;

var selectedGroup = doc.layers.getByName('Colors');

doc.activeLayer = selectedGroup;

var reset = true;

loopLayers (selectedGroup)//turn off visibility

reset = false;

loopLayers (selectedGroup)//run loop again to run your inserted code

function loopLayers(gp){

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

        if(gp.layers.typename =='LayerSet'){

            loopLayers (gp.layers);

            }

        else if(reset){

            gp.layers.visible = false;

            }

        else{

            gpLayers.visible = true;

            //enter code here to save or whatever

            gpLayers.visible = false;

            }

        }

    }

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
Explorer ,
May 01, 2017 May 01, 2017

Copy link to clipboard

Copied

https://forums.adobe.com/people/Chuck+Uebele  schrieb

Here's a modified script. It will first turn off the visibility of all the layers in the Colors layer set. Then it will cycle back through and turn each one on, you insert whatever code you want to save the file at that point, then it turns the visibility off on that layer, and moves to the next.

Hello Chuck

thank you so much for your kind help! That did the trick, just had to correct 2 typos (also corrected below).

Have a great week and happy to get such a great support here in the forum!

Regards

Daniel

  1. #target photoshop 
  2. var doc = activeDocument; 
  3. var selectedGroup = doc.layers.getByName('Colors'); 
  4. doc.activeLayer = selectedGroup; 
  5. var reset = true
  6. loopLayers (selectedGroup)//turn off visibility 
  7. reset = false
  8. loopLayers (selectedGroup)//run loop again to run your inserted code 
  9.  
  10. function loopLayers(gp){ 
  11.     for(var i=0;i<gp.layers.length;i++){ 
  12.         if(gp.layers.typename =='LayerSet'){ 
  13.             loopLayers (gp.layers); 
  14.             } 
  15.         else if(reset){ 
  16.             gp.layers.visible = false
  17.             } 
  18.         else
  19.             gp.layers.visible = true
  20.             //enter code here to save or whatever 
  21.             gp.layers.visible = false
  22.             } 
  23.         } 
  24.     }

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
New Here ,
Feb 14, 2020 Feb 14, 2020

Copy link to clipboard

Copied

LATEST

Я просто оставлю это здесь, может быть кому-то поможет:

 

1. Создайте группу switch

2. Разместите там слои которые требуется включать по очереди и сохранять (слои должны быть выключенными).

3. Создайте папку в C:/  с названием "wx"

4. Сохраните скрипт 'script.jsx'

5. File->Script->Browse выберите 'script.jsx'

 

Скрипт рекурсивно вызовет функцию, которая будет включать слой и сохранять результат в одноименный со слоем файл. Папка для сохранения указана в скрипте, это C:/wx

 

PS:

Отладка в Photoshop ужасная. Мне помогла утилита https://github.com/Trevor-/console

С помощью нее можно использовать консоль (тоже неудобно, но как есть)

Например  $.writeln('C:\\wx\\'+gp.layers[i].name+'.jpg');

 

(протестировано под Windows 10 & Adobe Photoshop CC 2017)

#target photoshop
var doc = activeDocument;
var gp = doc.layers.getByName('switch');
doc.activeLayer = gp;

function saveJPEG( doc, saveFile, qty ) {
    var saveOptions = new JPEGSaveOptions( );
    saveOptions.embedColorProfile = true;
    saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
    saveOptions.matte = MatteType.NONE;
    saveOptions.quality = qty; 
    doc.saveAs( saveFile, saveOptions, true );
}

function req( gp , qty) {
  
    if(qty > 0){
        var i = qty - 1;
        gp.layers[i].visible=true;
        saveJPEG( doc, new File('C:\\wx\\'+gp.layers[i].name+'.jpg'), 12 );
        req(gp, qty - 1);
    }
    
}

req(gp,gp.layers.length);

 

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