photoshop javascript channel content

1.3k views Asked by At

trying to write something that extracts masks from the RGB-channels.

I get a lot of .exr files with masks output as pure R G and B layers.

i've done this:

    var doc = app.activeDocument;
        function showMasks(docGroups) {    

            //step through the groups
        for (var i=0; i<docGroups.length; i++) {

             try{   
             //step through the layers in each group
            for(layerIndex=0; layerIndex < docGroups[i].artLayers.length; layerIndex++) {
var RGB = [doc.channels.getByName('Red'),doc.channels.getByName('Green'),doc.channels.getByName('Blue')];


                    for(var a in RGB)
                   {

                //create slection from  channel
                doc.selection.load(RGB[a], SelectionType.REPLACE);
                //add new layer
                doc.artLayers.add();
                // REVEAL ALL from selection
                var idMk = charIDToTypeID( "Mk  " );
                var desc62 = new ActionDescriptor();
                var idNw = charIDToTypeID( "Nw  " );
                var idChnl = charIDToTypeID( "Chnl" );
                desc62.putClass( idNw, idChnl );
                var idAt = charIDToTypeID( "At  " );
                var ref20 = new ActionReference();
                var idChnl = charIDToTypeID( "Chnl" );
                var idChnl = charIDToTypeID( "Chnl" );
                var idMsk = charIDToTypeID( "Msk " );
                ref20.putEnumerated( idChnl, idChnl, idMsk );
                desc62.putReference( idAt, ref20 );
                var idUsng = charIDToTypeID( "Usng" );
                var idUsrM = charIDToTypeID( "UsrM" );
                var idRvlS = charIDToTypeID( "RvlS" );
                desc62.putEnumerated( idUsng, idUsrM, idRvlS );
                executeAction( idMk, desc62, DialogModes.NO );


                    }

                 //hide layer, move on to the next
                 docGroups[i].artLayers[layerIndex].visible = false;  

                }

              }   
               catch(e){continue;} 

     }

      }

    showMasks(doc.layerSets);

which works alright, steps through groups and layers and outputs new layers with layer-masks on them accordingly. however, it only works if a layer contains R G and B, if it's a layer with only one color, it stops. how do I condition it to keep running if a layer doesn't contain all 3 channel colors? or rewrite to do one channel at a time?

any ideas much appreciated, thanks /S

1

There are 1 answers

0
SEJM On

solved this myself by checking if there's a selection made, i.e. if there's no selection made, the channel is empty -> move on.

I work as a retoucher and currently I'm getting a lot of .exr:s with 3d-products. VRay outputs materialID/objectID as RGB-passes. this speeds up workflow tremendously.

use like this: put all the RGB-passes in a group, hide all other layers. call script.

var doc = app.activeDocument;
var a=0;

function hasSelection (doc) {
    var ret = false;
    var as = doc.activeHistoryState;
    doc.selection.deselect();
    if (as != doc.activeHistoryState) {
        ret = true;
        doc.activeHistoryState = as;
    }
    return ret;
}

    function showMasks(docGroups) {    

        //this steps through the groups
        for (var i=0; i<docGroups.length; i++) {

         try{  

            // this steps through the layers in each group
            for(layerIndex=0; layerIndex < docGroups[i].artLayers.length; layerIndex++) {

             //visible layers only    
            if(docGroups[i].artLayers[layerIndex].visible == true){

            //var layer=docGroups[i].artLayers[layerIndex];
           var RGB = [doc.channels.getByName('Red'),doc.channels.getByName('Green'),doc.channels.getByName('Blue')];

                for(a in RGB)
                {
                    //create slection from  channel
                    doc.selection.load(RGB[a], SelectionType.REPLACE);

                    if(hasSelection(activeDocument)){

                    doc.artLayers.add();
                    // REVEAL ALL from selection
                    var idMk = charIDToTypeID( "Mk  " );
                    var desc62 = new ActionDescriptor();
                    var idNw = charIDToTypeID( "Nw  " );
                    var idChnl = charIDToTypeID( "Chnl" );
                    desc62.putClass( idNw, idChnl );
                    var idAt = charIDToTypeID( "At  " );
                    var ref20 = new ActionReference();
                    var idChnl = charIDToTypeID( "Chnl" );
                    var idChnl = charIDToTypeID( "Chnl" );
                    var idMsk = charIDToTypeID( "Msk " );
                    ref20.putEnumerated( idChnl, idChnl, idMsk );
                    desc62.putReference( idAt, ref20 );
                    var idUsng = charIDToTypeID( "Usng" );
                    var idUsrM = charIDToTypeID( "UsrM" );
                    var idRvlS = charIDToTypeID( "RvlS" );
                    desc62.putEnumerated( idUsng, idUsrM, idRvlS );
                    executeAction( idMk, desc62, DialogModes.NO );

                    }
                    else{a++;}

                }
             //hide layer, move on to the next
            docGroups[i].artLayers[layerIndex].visible = false;
          }
      }

          }       
           catch(e){return;} 

    }
 }

showMasks(doc.layerSets);

cheers. /S