简体   繁体   中英

photoshop javascript channel content

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

I get a lot of .exr files with masks output as pure RG 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 RG 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

solved this myself by checking if there's a selection made, ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM