5 Replies Latest reply on Mar 2, 2015 3:01 AM by c.pfaffenbichler

    Batch replace smart objects with displacement map


      Hi there, i am using this script for replace smart object with images.

      Problem is that on the layer in which I need to change the image imposed displacement map, and each time a window pops up asking to update the displacement map.

      Displacement Map.jpg

      How to add a change of the displacement map in the script?

      #target photoshop

      if (app.documents.length > 0) {

      var myDocument = app.activeDocument;

      var theName= myDocument.name.match(/(.*)\.[^\.]+$/)[1];

      var thePath = myDocument.path;

      var theLayer = myDocument.activeLayer;

      // jpg options;

      var jpgopts = new JPEGSaveOptions();

      jpgopts.embedProfile = true;

      jpgopts.formatOptions = FormatOptions.STANDARDBASELINE;

      jpgopts.matte = MatteType.NONE;

      jpgopts.quality = 8;

      // check if layer is smart object;

      if (theLayer.kind != "LayerKind.SMARTOBJECT") {alert ("selected layer is not a smart object")}

      else {

      // select files;

      if ($.os.search(/windows/i) != -1) {var theFiles = File.openDialog ("please select files", "*.psd;*.tif;*.jpg", true)}

      else {var theFiles = File.openDialog ("please select files", getFiles, true)};

      if (theFiles) {

      // work through the array;

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

      // replace smart object;

                          theLayer = replaceContents (theFiles[m], theLayer);

                          var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];

      //save jpg;

                          myDocument.saveAs((new File(thePath+"/"+theName+"_"+theNewName+".jpg")),jpgopts,true);





      ////// get psds, tifs and jpgs from files //////

      function getFiles (theFile) {

           if (theFile.name.match(/\.(psd|tif)$/i) != null || theFile.constructor.name == "Folder") {

                return true



      ////// replace contents //////

      function replaceContents (newFile, theSO) {

      app.activeDocument.activeLayer = theSO;

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

      var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );

          var desc3 = new ActionDescriptor();

          var idnull = charIDToTypeID( "null" );

          desc3.putPath( idnull, new File( newFile ) );

          var idPgNm = charIDToTypeID( "PgNm" );

          desc3.putInteger( idPgNm, 1 );

      executeAction( idplacedLayerReplaceContents, desc3, DialogModes.NO );

      return app.activeDocument.activeLayer


        • 1. Re: Batch replace smart objects with displacement map
          JJMack Most Valuable Participant

          I took a quick look at your script.


          I do not know where the the displacement map come in,


          The script seem the be replacing the object content of the current smart object layer.


          That layer can have had all sort of photoshop functions associated with it and has an associated transform.  All smart object layer have an associated transform and may have smart filters and filter mask as well.


          I see no code to handle the smart object size and make the replacement object have the same size or insure the replacement object is the aspect ratio and size.  Replacing  a smart object content does not change the current layer's associated the transform that transformed the original object. If the replacement object is not the same size as the original object the transform may not work like your expecting.


          An associated displacement map may come into play because of smart filters on the smart object layer. Displacement  maps content are either for the layers content or for lower layers content to make the current layer to conform to lower layers contours.


          In fact I create a four image picture package template that used a smart object for the image. Added CS2 for a file type that could be used to replace the image  in your script and ran your script.  It did pop up the ACR dialog if I select a raw file but it worked perfectly for my portrait cr2 files are the same size and aspect ratio. Ast the object.


          And a jpeg was saved to my desktop.


          When a different size landscape orianted portrait image is selected here is the result


          • 2. Re: Batch replace smart objects with displacement map
            Degreez Level 1

            Here is my psd:


            And after changing the image, i am need to update the displacement map.

            I don't know javascript and I need help with changing the script.

            • 3. Re: Batch replace smart objects with displacement map
              JJMack Most Valuable Participant

              That script would need a lot of work to be able to handle any size art work.  The script may need to create a temp work files the size of the original object some cropping it to the corrects aspect ratio then replace the object using that temp work file. The Artwork composition may be quite different the the desired artwork. The displacement adjustment filter also has an associated image most likely the contours of your t-shirt.  I do not know if the dialog would pop-up if the object was replaced with a object the same size as the original artwork.  I don't know javascript either I just hack at it.  I can point you to forums and javascript documentation and Adobe Scripting documentation.  You will not be able to hack that script without referencing the documentation and perhaps learning about Adobe scriptlistener plug-in.  You most likely do not want to put that much effort into it.  You can hire a programmer of perhaps find someone that know scripting willing to modify it for you.  It is not something I would want to do. For its to specific its for your t-shirt template.

              • 4. Re: Batch replace smart objects with displacement map
                c.pfaffenbichler Level 9

                Have you tried recording the replacing of the SO and applying of the Smart Filter with ScriptingListener.plugin?

                • 5. Re: Batch replace smart objects with displacement map
                  c.pfaffenbichler Level 9

                  Which version of Photoshop do you use anyway?

                  Because in my tests replacing the contents of a SO with Displace applied as a Smart Filter does not raise that dialog.