14

Is it only me that thinks the CS5 scripts runs painfully slow? These few lines takes over 1 minute to execute.

for (n=0; n<app.activeDocument.layerSets.length; n++) {
  app.activeDocument.layerSets[n].visible = false;
}

The number of layerSets are 20. I'm running the CS5.1 64bit version on a Vista Home Premium system, AMD Athlon 64 X2 Dual Core 5200+ with 8GB RAM.

I tried to Export the script as a .JSXBIN but it still takes over 1 minute. CPU usage for CS5.1 goes from 3% to 57% when the CS5.1 is running the .JSXBIN script.

There must be something wrong here, how can I speed up the scripts?

// Thanks

* EDIT * It seems like CS5's own DOM implementation is the problem here. The script speeded up more than twice by reading DOM related values into local variables.

var LayerCount = app.activeDocument.layerSets.length;
var LayerRoot = app.activeDocument.layerSets;

for (n=0; n<LayerCount; n++) {
  LayerRoot[n].visible = false;
}

...but still, it far to much time to just change a property in 20 objects. Any help with optimizing would be appreciated :)

Max Kielland
  • 5,627
  • 9
  • 60
  • 95
  • Have you considered saving the innerHTML for each layer to an array and and just setting it to ""? Then to restore a layer, just copy its innerHTML back from the array. hide=>`temp[i]=layerSets[i].innerHTML;layerSets[i].innerHTML="";` ... show=>`layerSets[i].innerHTML=temp[i]`; – technosaurus Nov 30 '12 at 14:05
  • Are you running Vista by any chance? – Fraser Dec 26 '12 at 21:34
  • Same result on both Vista and Windows 7. – Max Kielland Dec 26 '12 at 23:09

2 Answers2

2

The only thing I can think of is try try to loop through the individual layers in app.activeDocument.layers which contains all the layers and groups. When you do this you'll notice that grouped layers will still retain their original visible property but are hidden because their parent group is hidden.

#target photoshop

var myLayers = app.activeDocument.layers;
var myLayersLength = myLayers.length;

for (var i=0; i<myLayersLength; i++) {
    myLayers[i].visible = false;
}

EDIT: So I tested this solution on a 400mb file with 50 layers and it worked in seriously less than a second. Are you sure the problem is with Photoshop?

If you have to iterate through every single layer and child-layer individually to perform an action you can do it recursively:

#target photoshop

var doc = app.activeDocument;
findLayers(doc);


function findLayers(set) {
    for (var i=0; i<set.layerSets.length; i++) {

        //recursive call
        findLayers(set.layerSets[i]);

        //iterate sub-layers and hide
        for (var j=0; j<set.layerSets[i].layers.length; j++) {
            set.layerSets[i].layers[j].visible = false;
        }
    }

    //hide top-level layers
    for (var l=0; l<set.layers.length; l++) {
        set.layers[l].visible = false;
    }
}

This takes a bit longer, ~20 seconds on my machine, but it will hit every single layer in a document.

NOTE: I also tested your original scripts from the question and they don't work on un-grouped layers because you're iterating through document.layerSets instead of document.layers

pdizz
  • 4,100
  • 4
  • 28
  • 42
  • I timed my original code and your original code with 38 layers at root level. They took equal long time (2.7 minutes). I'm runing the standard version that doesn't support GPU. Did you have GPU support enabled? – Max Kielland Aug 27 '12 at 23:20
  • I ran your first example again with 38 layers on an i7-2630 @ 2GHz with 8GB RAM. This time it took 1.7 minutes (1 minute faster), still to much for only turning off layers. Is there some debug information that can be switched off to speed up? – Max Kielland Aug 28 '12 at 01:45
  • 1
    I tested my first script (`for` loop) against a 605mb file with 50 layers of 2560x1712 px and it took ~8 seconds. Here are the performance settings I'm using in PS (all default AFAIK) http://i.imgur.com/VoRNg.jpg Anyway, I tried turning off GPU support and it had little effect since I'm using onboard graphics and don't have a video card. Either you're working with some massive, high-resolution files, or something else is bogging you down. I'm on a 64 bit AMD dual-core 3.20 GHz with 4gb RAM (only 2 gb allocated to photoshop though) Anyway, I don't think your code is the problem here. – pdizz Aug 28 '12 at 02:26
1

Have a look at this ps-scripts iteration over layers is slow - explanation Which may be able to help you as well.

Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125