6 Replies Latest reply on Oct 9, 2017 12:16 AM by c.pfaffenbichler

    full colour brush dabbing simulation script

    c.pfaffenbichler Level 9

      Recently the issue of full-color brush-tips has been raised yet again and I got to thinking about how to emulate if not a proper brush stroke then at least dabbing.

       

      One tool very few image editors and illustrators seem to be using with any considerable frequency in Photoshop and that is intended to give position is the Count Tool.

      So by linking a Script to creating counters it would be possible to create Smart Object instances at manually defined positions without sacrificing a more frequently used tool (like Colour Sampler Tool or Pen Tool).

      Depending on the size of the SO this could go fairly speedily or take some time but still …

      countToolColorBrushDabSim.gif

       

      So if anyone would like to give it a try let me know what your impression is.

      One can add the appropriate event in the file »Script Events Manager.xml« and link the Script to the event in Script Events Manager, then clicking with the Count Tool should create a copy of the active Layer (or one of the selected Layers) with randomisation depending on the filename.

      countToolScriptEventsManagerXml.jpgcountToolScriptEventsManager.jpg

       

      // duplicate layer to position of last counter of last count group and remove the counter;

      // only if one pixel layer is selected;

      // if "_minXX" and "_maxXX" are part of the layer name scale to between the two numbers;

      // 2017, use it at your own risk;

      #target photoshop

      if (app.documents.length > 0) {

      activeDocument.suspendHistory ("duplicate", "main ()")

      };

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

      ////// main //////

      function main () {

      var myDocument = app.activeDocument;

      // pixels,;

      var originalRulerUnits = app.preferences.rulerUnits;

      app.preferences.rulerUnits = Units.PIXELS

      // collect layers;

      var theLayers = smartifyAndGetSelectedLayersIdxEtc();

      // get coordinates;

      var ref = new ActionReference();

      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

      var docDesc = executeActionGet(ref);

      var counter =docDesc.getList(stringIDToTypeID("countClass"));

      var thePoints = new Array;

      for (var c = 0; c < counter.count; c++) {

      var thisOne = counter.getObjectValue(c);

      thePoints.push([thisOne.getUnitDoubleValue(stringIDToTypeID("x")), thisOne.getUnitDoubleValue(stringIDToTypeID("y")), thisOne.getUnitDoubleValue(stringIDToTypeID("group")), thisOne.getUnitDoubleValue(stringIDToTypeID("itemIndex"))])

      };

      // duplicate to last point;

      if (counter.count > 0) {

      var theIndex = 0;

      // chose a layer if more than one are selected;

      if (theLayers.length > 1) {

      theIndex = Math.floor((theLayers.length)*Math.random());

      };

      // get numbers;

      var theMinRegExp = new RegExp(/_min[0-9]*/i);

      var theMaxRegExp = new RegExp(/_max[0-9]*/i);

      var theAngRegExp = new RegExp(/_ang[0-9]*/i);

      // set range;

      var theMin = theLayers[theIndex][5].match(theMinRegExp);

      var theMax = theLayers[theIndex][5].match(theMaxRegExp);

      var theAng = theLayers[theIndex][5].match(theAngRegExp);

      if (theMin != null && theMax != null) {

      var random = Math.random();

      var theScale = random*(Number(String(theMax).slice(4))-Number(String(theMin).slice(4)))+Number(String(th eMin).slice(4));

      } else {var theScale = 100};

      if (theAng != null) {

      var random = Math.random()-0.5;

      var theAngle = random*(Number(String(theAng).slice(4)));

      } else {var theAngle = 0};

      layerDuplicateOffsetScaleRotate (theLayers[theIndex][0], thePoints[thePoints.length - 1][0] - theLayers[theIndex][1], thePoints[thePoints.length - 1][1] - theLayers[theIndex][2], theScale, theScale, theAngle)

      // move to top;

      if (theLayers.length > 1) {

      // =======================================================

      var idmove = charIDToTypeID( "move" );

          var desc6 = new ActionDescriptor();

          var idnull = charIDToTypeID( "null" );

              var ref3 = new ActionReference();

              var idLyr = charIDToTypeID( "Lyr " );

              var idOrdn = charIDToTypeID( "Ordn" );

              var idTrgt = charIDToTypeID( "Trgt" );

              ref3.putEnumerated( idLyr, idOrdn, idTrgt );

          desc6.putReference( idnull, ref3 );

          var idT = charIDToTypeID( "T   " );

              var ref4 = new ActionReference();

              var idLyr = charIDToTypeID( "Lyr " );

              var idOrdn = charIDToTypeID( "Ordn" );

              var idFrnt = charIDToTypeID( "Frnt" );

              ref4.putEnumerated( idLyr, idOrdn, idFrnt );

          desc6.putReference( idT, ref4 );

      executeAction( idmove, desc6, DialogModes.NO );

      };

      // reselect layers;

      selectLayerByID(theLayers[0][0], false);

      if (theLayers.length > 1) {

      for (var d = 1; d < theLayers.length; d++) {

      selectLayerByID(theLayers[d][0], true);

      };

      };

      // delete last counter;

      // =======================================================

          var desc11 = new ActionDescriptor();

          desc11.putInteger( charIDToTypeID( "ItmI" ), counter.count );

          desc11.putInteger( charIDToTypeID( "Grup" ), 0 );

      executeAction( stringIDToTypeID( "countDelete" ), desc11, DialogModes.NO );

      };

      // reset preferences;

      app.preferences.rulerUnits = originalRulerUnits;

      };

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

      ////// get array of arrays of smart objects witrh index, center and half-dimensions //////

      function smartifyAndGetSelectedLayersIdxEtc(){

      var selectedLayers = new Array;

      var ref = new ActionReference();

      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

      var desc = executeActionGet(ref);

      if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){

        desc = desc.getList( stringIDToTypeID( 'targetLayers' ));

        var c = desc.count;

        var selectedLayers = new Array();

        for(var i=0;i<c;i++){

        try{

        activeDocument.backgroundLayer;

        selectedLayers.push(  desc.getReference( i ).getIndex() );

        }catch(e){

        selectedLayers.push(  desc.getReference( i ).getIndex()+1 );

        };

        }

      }else{

        var ref = new ActionReference();

        ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));

        ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

        try{

        activeDocument.backgroundLayer;

        selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);

        }catch(e){

        selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));

        };

      };

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

      var theArray = new Array;

      var theIDs = new Array;

      for (var m = 0; m < selectedLayers.length; m++) {

      var thisIndex = selectedLayers[m];

      var ref = new ActionReference();

      ref.putIndex( charIDToTypeID("Lyr "), thisIndex);

      var layerDesc = executeActionGet(ref);

      var thisID = layerDesc.getInteger(stringIDToTypeID("layerID"));

      var theKind = layerDesc.getInteger(stringIDToTypeID("layerKind"));

      var theName = layerDesc.getString(stringIDToTypeID("name"));

      var theVisibility = layerDesc.getInteger(stringIDToTypeID("visible"));

      var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));

      var halfWidth = theBounds.getUnitDoubleValue(stringIDToTypeID("width")) / 2;

      var halfHeight = theBounds.getUnitDoubleValue(stringIDToTypeID("height")) / 2;

      var theX = theBounds.getUnitDoubleValue(stringIDToTypeID("left")) + halfWidth;

      var theY = theBounds.getUnitDoubleValue(stringIDToTypeID("top")) + halfHeight;

      // is normal, shape, smart object, pattern, gradiet, solid color;

      if (theKind == 1 || theKind == 4 || theKind == 5 || theKind == 9 || theKind == 10 || theKind == 11) {

      if (theVisibility == true) {

      theIDs.push ([thisID, theX, theY, halfWidth, halfHeight, theName])

      }

      }

      };

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

      for (var n = 0; n < theIDs.length; n++) {

      if (hasSmartObject(theIDs[n][0]) == false) {

        try {

        selectLayerByID(theIDs[n][0], false);

        var id557 = charIDToTypeID( "slct" );

        var desc108 = new ActionDescriptor();

        var id558 = charIDToTypeID( "null" );

        var ref77 = new ActionReference();

        var id559 = charIDToTypeID( "Mn  " );

        var id560 = charIDToTypeID( "MnIt" );

        var id561 = stringIDToTypeID( "newPlacedLayer" );

        ref77.putEnumerated( id559, id560, id561 );

        desc108.putReference( id558, ref77 );

        executeAction( id557, desc108, DialogModes.NO );

        theArray.push([getLayerId(app.activeDocument.activeLayer), theIDs[n][1], theIDs[n][2], theIDs[n][3], theIDs[n][4], theIDs[n][5]]);

        } catch (e) {}

        }

      else {theArray.push(theIDs[n])};

      };

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

      //alert (theArray[0].join("\n"))

      return theArray

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

      };

      // by mike hale, via paul riggott;

      function getLayerId(theLayer){

      app.activeDocument.activeLayer = theLayer;

      //Assumes activeDocument and activeLayer

          var ref = new ActionReference();

          ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));

          d = executeActionGet(ref);

      return d.getInteger(charIDToTypeID('LyrI'));

      };

      ////// smart object or not //////

      function hasSmartObject(idx){

      var ref = new ActionReference();

      ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID( "smartObject" ));

      ref.putIdentifier( charIDToTypeID( "Lyr " ), idx);

      var desc = executeActionGet(ref);

      if(desc.hasKey(stringIDToTypeID('smartObject'))) {return true}

      else {return false};

      };

      ////// based on code by mike hale and paul riggott //////

      function selectLayerByID(index,add){

      add = undefined ? add = false:add

      var ref = new ActionReference();

          ref.putIdentifier(charIDToTypeID("Lyr "), index);

          var desc = new ActionDescriptor();

          desc.putReference(charIDToTypeID("null"), ref );

             if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );

            desc.putBoolean( charIDToTypeID( "MkVs" ), false );

         try{

          executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );

      }catch(e){

      alert(e.message);

      }

      };

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

      ////// duplicate layer (id, xOffset, yOffset, theXScale, theYScale, theAngle) //////

      function layerDuplicateOffsetScaleRotate (theID, xOffset, yOffset, theXScale, theYScale, theAngle) {

      selectLayerByID(theID, false);

      // =======================================================

          var desc23 = new ActionDescriptor();

          var idnull = charIDToTypeID( "null" );

              var ref2 = new ActionReference();

      //        ref2.putIdentifier ( charIDToTypeID( "Lyr " ), theID );

              ref2.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

          desc23.putReference( idnull, ref2 );

          desc23.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ) );

          var idOfst = charIDToTypeID( "Ofst" );

              var desc24 = new ActionDescriptor();

              var idPxl = charIDToTypeID( "#Pxl" );

              desc24.putUnitDouble( charIDToTypeID( "Hrzn" ), idPxl, xOffset );

              desc24.putUnitDouble( charIDToTypeID( "Vrtc" ), idPxl, yOffset );

          var idOfst = charIDToTypeID( "Ofst" );

          desc23.putObject( idOfst, idOfst, desc24 );

          var idPrc = charIDToTypeID( "#Prc" );

          desc23.putUnitDouble( charIDToTypeID( "Wdth" ), idPrc, theXScale );

          desc23.putUnitDouble( charIDToTypeID( "Hght" ), idPrc, theXScale );

          desc23.putUnitDouble( charIDToTypeID( "Angl" ), charIDToTypeID( "#Ang" ), theAngle );

          desc23.putEnumerated( charIDToTypeID( "Intr" ), charIDToTypeID( "Intp" ), stringIDToTypeID( "bicubicAutomatic" ) );

          desc23.putBoolean( charIDToTypeID( "Cpy " ), true );

      executeAction( charIDToTypeID( "Trnf" ), desc23, DialogModes.NO );

      };

      ////// duplicate layer (id, xOffset, yOffset, theXScale, theYScale, theAngle) //////

      function layerDuplicateOffset (theID, xOffset, yOffset, theXScale, theYScale, theAngle) {

      selectLayerByID(theID, false);

      // =======================================================

      var idcopy = charIDToTypeID( "copy" );

          var desc6 = new ActionDescriptor();

          var idnull = charIDToTypeID( "null" );

              var ref1 = new ActionReference();

              var idLyr = charIDToTypeID( "Lyr " );

              ref1.putIdentifier ( idLyr, theID );

          desc6.putReference( idnull, ref1 );

          var idT = charIDToTypeID( "T   " );

              var desc7 = new ActionDescriptor();

              var idHrzn = charIDToTypeID( "Hrzn" );

              var idPxl = charIDToTypeID( "#Pxl" );

              desc7.putUnitDouble( idHrzn, idPxl, xOffset );

              var idVrtc = charIDToTypeID( "Vrtc" );

              desc7.putUnitDouble( idVrtc, idPxl, yOffset );

          var idOfst = charIDToTypeID( "Ofst" );

          desc6.putObject( idT, idOfst, desc7 );

      executeAction( idcopy, desc6, DialogModes.NO );

      };

      ////// bring to front //////

      function bringToFront () {

      // =======================================================

      var idmove = charIDToTypeID( "move" );

          var desc6 = new ActionDescriptor();

          var idnull = charIDToTypeID( "null" );

              var ref3 = new ActionReference();

              var idLyr = charIDToTypeID( "Lyr " );

              var idOrdn = charIDToTypeID( "Ordn" );

              var idTrgt = charIDToTypeID( "Trgt" );

              ref3.putEnumerated( idLyr, idOrdn, idTrgt );

          desc6.putReference( idnull, ref3 );

          var idT = charIDToTypeID( "T   " );

              var ref4 = new ActionReference();

              var idLyr = charIDToTypeID( "Lyr " );

              var idOrdn = charIDToTypeID( "Ordn" );

              var idFrnt = charIDToTypeID( "Frnt" );

              ref4.putEnumerated( idLyr, idOrdn, idFrnt );

          desc6.putReference( idT, ref4 );

      executeAction( idmove, desc6, DialogModes.NO );

      };