3

Is there a way to automate the replacement of a Smart Image with a different graphic?

For example, if I have a master image of a mug, and a large number of flat rectangular images/designs I'd like on the mug, after some processing. I've set up a smart image using one of the designs and I've tweaked it with some filters to make it less rectangular (adjusting the perspective, bending the top and bottom to match the curve of the mug, etc.).

Now I need to figure out how to use Variables (if possible) to replace the image in the Smart Object so the same transformations/filters will be run on each image, which will then be placed on the mug and saved.

I see this tutorial which explains the basics of Variable data and shows how to use images as data -- but it's not clear to me how to use this to update the Smart Object so that all the transformations get applied to the images.

If Variables won't work with Smart Objects, I'm also open to using a script. (I'd love a snippet on how to update a smart object via script if anyone has one, as I've never used scripts with Photoshop before).

3
  • You can not actually update smart objects with it, time to write a script
    – joojaa
    Jun 9, 2017 at 8:02
  • I'll update the question to include details about scripts as an acceptable solution.
    – Laryn
    Jun 10, 2017 at 12:04
  • There is a faster way. Try a script. There are a few available online - just search it out. It will do the entire folder of images without using any of the above instructions.
    – dnaskrent
    Apr 25, 2018 at 14:55

3 Answers 3

5
+50

I found this page that has an answer that looked close to what you're looking for, I modified it to work with the latest version and to hopefully fit your needs better.

Here's how to use it:

  1. Save the code below in a file with a .js extension
  2. Open your template file and select the SmartObject
  3. Go to File->Scripts->Browse
  4. Find your .js file and open it
  5. Select your images/designs and open them
  6. You're done!

.js file contents:

// Replace SmartObject’s Content and Save as PSD
// 2017, 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 SmartObject;
    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) {
            for (var m = 0; m < theFiles.length; m++) {
                // Replace SmartObject
                theLayer = replaceContents(theFiles[m], theLayer);
                var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
                // 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 SmartObject 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
};
12
  • Thanks! I haven't had a chance to test this yet but it's the closest to what I asked for so I'll award you the bounty.
    – Laryn
    Jun 16, 2017 at 14:49
  • 1
    Let me know if you have trouble with it. I tested it on a file that had three layers in it (the Smart Object, an image layer and background), and in two versions of Ps, CC 2017 & CS6. It ran MUCH faster in CC than CS6, and note that the PSD must be saved before you can run the script.
    – Circle B
    Jun 16, 2017 at 15:57
  • 1
    This script works exactly as I had hoped! Thanks so much! For anyone who uses it in the future, just make sure the smart object replacements are exactly the same dimensions, including resolution. And I changed it to save as a JPG instead of a PSD -- gist.github.com/laryn/0a1f6bf0dab5b713395a835f9bfa805c
    – Laryn
    Jun 17, 2017 at 5:12
  • 1
    One improvement (for bulk use) would be to somehow avoid running out of memory. Currently each replacement is saved in Photoshop history and on a huge folder can run into memory errors after a large number of replacements. I'm not sure if you know of a way to replace, save, and then remove from memory before going to the next one, but that would be my only suggestion at this point!
    – Laryn
    Jun 18, 2017 at 19:52
  • 1
    I found this, I'll try to implement and test it in the next couple of days, but I thought I'd go ahead and post it...
    – Circle B
    Jun 19, 2017 at 19:59
2

open the mug PSD keep the mug layer below the image you want and you can import all the images you want on the mug and record actions for the perspective adjustments.and play those actions on every layer which contains the image you want on the mug. you can convert all the images to smart objects either using actions or by just doing it manually it will just take two clicks. you can also assign a function key for the action you recorded so that you can just press a button and the output will be put into the folder you selected while recording the action. this should make it one button press job if not complete Automation. but that's the closest you can get without having to code. if you still seek complete automation you will have to write some code.

2
  • The smart object already has all the adjustments on it. I need to import all the images into the smart object and then resave the mug file (which has the smart object placed and adjusted) for each of hundreds or thousands of images. Do you have any example scripts that would be a guide on how to update a smart object?
    – Laryn
    Jun 13, 2017 at 15:30
  • no, don't change the images inside the smart object just make a smart object for every image you want to put on the mug and apply the same effects on each smart object layer using actions. and make another action which just saves the document to a folder and after saving deletes the topmost smart object layer. and does the same thing again and again until all images are on the mug. ps: you don't need a script for this. try to learn more about actions u will understand what I am saying. Jun 14, 2017 at 16:31
1

If someone can profile a test document. I got a version work which opens the smart object and will place a new layer close it and then save a new file. I see many people asking this request for mockup templates.

I altered the code and code something working. However i need a proper test file to if script work well and as expected.

Hope this doesnt get any minor downvotes, kinda crueling how to answer here

4
  • share the code @Rombout Versluijs Oct 23 at 1:39
  • Check this gist, I've also done some work on that version. gist.github.com/laryn/…. I also have another version which adjust the links in embedded smart objects. With this you can also relink images if the main smartovject has perspective transformations applied which the user wants to keep Oct 24 at 16:04
  • I have tested your original script with Photoshop24. It throws error in function replaceContents(newFile, theSO). However, script works hence what i have done is to use try and catch and ignore error in catch. I confirm it works. Oct 25 at 1:13
  • Did setup the document as the example image showed. That version only works if the smart object is inside a folder and the folder needs that specific name. My idea is to merge the other version I have so the user gets a dialog and can choose what setup is used. So one will work with none transformed smart objects and the other will work with transformed smartobjects Oct 26 at 16:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.