Batch replace smart objects v2

Explorer ,
Jan 21, 2019 Jan 21, 2019

Copy link to clipboard

Copied

Greetings,

Unfortunately, my scripting knowledge is very minimal.  In using this script, the script times out and no output files occur.  I am on a pc running Photoshop CC (online subscription, latest update).  It was suggested to me by another forum member to start a new discussion thread here to assist with this problem.

This is what I have:

  •     a mockup file of a canvas wall print with the main single image set as a transformed smart object
  •     a folder of images resized to fit said smart object

This is what I would like a script to do (if possible):

  •     for every image in said folder
  •     replace smart object with images
  •     save each new mockup (one for each new image) as a JPG file.
  •     saved name should be the original image file name (saved in a different directory).  if this naming convention is not possible, its ok as i can rename the files later.

We have over 50k of mockups to do for this particular canvas shape/size....

layers.png

Any suggestions on how I could streamline this process without having to manually replace and save?  THANK YOU!!!

// replace smart object’s content and save psd;

// 2011, use it at your own risk;

#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;

// psd options;

psdOpts = new PhotoshopSaveOptions();

psdOpts.embedColorProfile = true;

psdOpts.alphaChannels = true;

psdOpts.layers = true;

psdOpts.spotColors = true;

// 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, theLayer);

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

//Raise color picker for Back cover;

try {

app.activeDocument.activeLayer = app.activeDocument.layers[app.activeDocument.layers.length - 1];

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

var idsetd = charIDToTypeID( "setd" );

var desc7 = new ActionDescriptor();

var idnull = charIDToTypeID( "null" );

var ref2 = new ActionReference();

var idcontentLayer = stringIDToTypeID( "contentLayer" );

var idOrdn = charIDToTypeID( "Ordn" );

var idTrgt = charIDToTypeID( "Trgt" );

ref2.putEnumerated( idcontentLayer, idOrdn, idTrgt );

desc7.putReference( idnull, ref2 );

var idT = charIDToTypeID( "T   " );

var desc8 = new ActionDescriptor();

var idClr = charIDToTypeID( "Clr " );

var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );

desc7.putObject( idT, idsolidColorLayer, desc8 );

executeAction( idsetd, desc7, DialogModes.ALL );

} catch (e) {};

//save jpg;

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

                    }

          }

}

};

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

function getFiles (theFile) {

     if (theFile.name.match(/\.(psd|tif|jpg)$/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

};
TOPICS
Actions and scripting

Views

8.6K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Explorer , Jan 22, 2019 Jan 22, 2019
thank you JJMack​ !!this script is working great now!  Here is the final working script:// replace smart object’s content and save psd;// 2011, use it at your own risk;#target photoshopif (app.documents.length > 0) {    var myDocument = app.activeDocument;    var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];    var thePath = myDocument.path;    var theLayer = myDocument.activeLayer;    // psd options;    psdOpts = new PhotoshopSaveOptions();    psdOpts.embedColorProfile = true;    psdOpts...

Likes

Translate

Translate
Explorer ,
May 02, 2020 May 02, 2020

Copy link to clipboard

Copied

JJMack said

 

 

 

 

The video also look doctored to me for it showed replacement label thumbnail that where for various aspect ratio jpeg images some of which had transparent backgrounds where jpeg file formats does not support transparency.

 

 

 

 

Funny that I did not notice those jpeg files with seemingly transparent backgrounds. I checked one of them out and they are actually not transparent, just a checkerboard of gray and white. Looks like they were badly converted from webp format. Here is the actual output image from that demo:

 

This was how it also turned out in the video demo.This was how it also turned out in the video demo.

 

This is how it also turned out in the video demo but I did not bother to correct it. I probably should have and avoid suspicions. The extension supports transparency and viewers would think that it does not.

 

Regarding the different aspect ratios of the replacement files, it was not a problem in this case because they were set to be resized using the "Crop" option. This meant that some of them have their top and bottom slightly cropped out as a result, while others that are too wide have their left and right cropped out, in order to fully fill the space occupied by the smart layer in the 3D object.

 

JJMack said:

 

 

 

The video showed the above thumbnail which would not be the correct size for a good replacement of content for all possible 3D Objects. The script would need to edit these replacement jpeg images for the 3D object surface.  I would not know how a script could edits any size jpeg imget to fit on any 3D object surface.

 

 

 

 

There is no need to worry about that. In most cases, Photoshop has already fitted the dimension of the smart layer to match the surface of the 3D surface. In other cases, the mock-up author has created the 3D texture to use a UV image map, which is a single smart layer distributed around different surface parts of the 3D object. I created a video demo on how to deal with UV-mapped textures here:

 

 

In this case, the mock-up author had not used smart layers to divide parts of the image map and the video demo shows how to prepare the mock-up to have smart layers for replacement within the 3D texture. These are the complete steps:

 

  1. save the UV texture map as a separate PSD

  2. prepare a smart layer for the replacement image in the UV texture map

  3. use Batch Smart Layer Replace to auto-replace the smart layer in the UV texture map

  4. use the exported files from the UV texture map to auto-replace the UV texture map of the 3D Layer in the mock-up

 

Your 3D soccer ball is basically set up this way but it's using way so many smart layers. Job Queue add-on would probably be able to handle auto-replacing all of them. In a future version of the 3D Layer add-on, I will have it support replacing smart layers placed within 3D layer UV textures without having to separately output intermediate replacement UV map image files.

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 27, 2020 May 27, 2020

Copy link to clipboard

Copied

Does that matter you script only replaces the 3d object surface material.  If the user has a very complex object the user has a huge job creating the replacement surfaces materials.  That the hard part.  Your script does the easy part. Replace the object surface with the replacement the user had to provide which required a great deal of work.  Some Simple objects like a can label may be easy.  In you bed pillow example what would your script do if each pillow and the spread were three 3d object in a single bedroom scene.  The 3D layer would have three object surfaces that need replacing.

 

It would be easier to rasterize the 3d Layer then create a mockup template for the three part you want to replace.  Perhaps two smart object. One for the pillow fabric and one for the spread fabric. Or perhaps all three smart object can share the same fabric materials image.

 

Your example are using example the have 3D objects the have simple surfaces.  Show is the Soccer Ball example when your script can handle it. As is it has a simple surface as created.  All I needed to do the generate replacement surfaces materials was to created a photo collage template to generate replacement Images the aligned to the simple UV surface areas.  After creating the Photo Collage template.  Replacement  Surfaces materials were easy to batch generate Using more or less a 1:1 centered selection of any image. 

 

I have seen 3D layer surface UV maps I find would be impossible to automate generating replacement surfaces materials.  I have no ideas of how much 3D work Phoroshop's 3D feature can do. I do  know Photoshop is not a full blown 3D application and I do not care for I do not know 3D editing it look like a lot of work not something I want to learn.

JJMack

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 07, 2020 Oct 07, 2020

Copy link to clipboard

Copied

Hello JJMack,
I downloaded your script but when I am trying to run it I have the below error message, could you please help me? 
Capture d’écran 2020-10-07 à 17.40.41.pngCapture d’écran 2020-10-07 à 18.21.18.png

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 16, 2020 Oct 16, 2020

Copy link to clipboard

Copied

It hard to tell what file my script is processing that should be a PSD template. You failed to post the full screen capture. The layer document you show has a smart object on top. What document was my script processing when it put out that message. It should be on the screen the message is for.

image.png

JJMack

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 25, 2020 Apr 25, 2020

Copy link to clipboard

Copied

paid via paypal. please check your email

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 25, 2020 Apr 25, 2020

Copy link to clipboard

Copied

Let us know how it works for you..... Here is a link to my SoccerBall.psd Template  Use the link to download it. See how jpeg images will work  your paypal purchase and my template. My template is a single 3d Layer. Rendered as an MP4 the exported file is 7MB the template is 9MB and exported as an animated Gif its 19MB.   As a Jpeg still its 154KB

image.png

JJMack

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 25, 2020 Apr 25, 2020

Copy link to clipboard

Copied

To be done correctly the replacement should look something like this:

image.png

 

JJMack

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 27, 2020 Apr 27, 2020

Copy link to clipboard

Copied

For your 3D Phone case mockup you mat want to look at Adobe Dimension. 

JJMack

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines