Sub topic 14
Automating the flip-and-fade steps with a script
This chapter turns the repeatable part of a reflection into a script. Photoshop's flip, opacity, blur and mask steps are the same every time, so a short ExtendScript can run them for you. The script does not judge the light, the surface or the color, which you still do by hand.
It assumes you have read the earlier chapters on reflections. You should know how to flip a duplicate and fade it with a gradient mask. You should also be comfortable with basic JavaScript: variables, functions, if statements and objects.
What to automate and what to leave manual
Scripting pays off when a step has no judgment in it. The flip, the layer name, the starting opacity and the empty mask are identical for every subject. The gradient direction, the ripple distortion and the color match depend on the scene.
| Step | Automate? | Reason |
|---|---|---|
| Duplicate and name the layer | Yes | Same every time |
| Flip around the bottom edge | Yes | Pure geometry |
| Set starting opacity and blur | Yes | Reasonable defaults, easy to tweak later |
| Add an empty layer mask | Yes | Mechanical |
| Draw the fade gradient | Manual | Length depends on surface glossiness |
| Ripples, curvature, color match | Manual | Depends on the scene |
How Photoshop scripting works
Photoshop runs ExtendScript, an older dialect of JavaScript (roughly ES3). It has no let, const, arrow functions, Array.prototype.forEach or built-in JSON. Use var, for loops and plain functions.
You run a script in one of two ways:
- Choose File > Scripts > Browse and pick a
.jsxfile. This is the easiest way while you are testing. - Save the file in Photoshop's
Presets/Scriptsfolder and restart Photoshop. It then appears in the File > Scripts menu.
The scripting model is a tree. app is the application, app.activeDocument is the open document, and doc.activeLayer is the selected layer. Layers are ArtLayer objects (pixels, text, smart objects) or LayerSet objects (groups).
The core of the script
The two code examples in this tutorial's plan use duplicate(), resize() and opacity. The flip works like this:
var refl = src.duplicate();
refl.resize(100, -100, AnchorPosition.BOTTOMCENTER);
refl.opacity = 40;resize(horizontal, vertical, anchor) takes percentages. A vertical value of -100 scales to 100% and mirrors the layer. The anchor is the fixed point of the mirror. With BOTTOMCENTER, the copy flips around the subject's bottom edge and lands directly below it.
A complete, reusable script
The script below adds four things to that core. It checks that it is safe to run, adds a gap for subjects that do not touch the floor, moves the copy below the original, and creates an empty mask. It also wraps the work in one history step, so a single undo removes everything.
#target photoshop
// Edit these values to change the starting look.
var CONFIG = {
opacity: 40, // percent
blur: 1.5, // pixels; 0 skips the blur
gap: 0 // pixels between subject and reflection
};
function addRevealAllMask() {
var desc = new ActionDescriptor();
desc.putClass(charIDToTypeID("Nw "), charIDToTypeID("Chnl"));
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("Chnl"), charIDToTypeID("Chnl"), charIDToTypeID("Msk "));
desc.putReference(charIDToTypeID("At "), ref);
desc.putEnumerated(charIDToTypeID("Usng"), charIDToTypeID("UsrM"), charIDToTypeID("RvlA"));
executeAction(charIDToTypeID("Mk "), desc, DialogModes.NO);
}
function addReflection(doc, src, cfg) {
var oldUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
try {
var refl = src.duplicate();
refl.name = src.name + " reflection";
refl.resize(100, -100, AnchorPosition.BOTTOMCENTER);
refl.move(src, ElementPlacement.PLACEAFTER); // put it below the subject
refl.opacity = cfg.opacity;
if (cfg.gap !== 0) {
refl.translate(new UnitValue(0, "px"), new UnitValue(cfg.gap, "px"));
}
if (cfg.blur > 0 && refl.kind === LayerKind.NORMAL) {
refl.applyGaussianBlur(cfg.blur);
}
doc.activeLayer = refl;
addRevealAllMask(); // the mask is now the active target
} finally {
app.preferences.rulerUnits = oldUnits;
}
}
function main() {
if (app.documents.length === 0) {
alert("Open a document first.");
return;
}
var doc = app.activeDocument;
var src = doc.activeLayer;
if (src.typename !== "ArtLayer" || src.isBackgroundLayer) {
alert("Select a single cut-out layer (not a group or the Background).");
return;
}
doc.suspendHistory("Add reflection", "addReflection(app.activeDocument, app.activeDocument.activeLayer, CONFIG)");
}
main();After the script runs, press G, choose a black-to-white linear gradient, and drag on the mask from the subject's bottom edge downward. Where the mask is white the reflection shows. Where it is black the reflection is hidden, so drag from white at the subject's feet to black at the end of the fade.
Why each safeguard is there
rulerUnits.translate()and other geometry calls read the ruler unit setting. The script switches to pixels and restores your original setting infinally, so a failure cannot leave your preferences changed.suspendHistory. It groups every step into one entry named "Add reflection" in the History panel. The code is passed as a string because Photoshop evaluates it in the global scope. That is whyaddReflectionandCONFIGsit at the top level of the file and not inside a wrapper function.kind === LayerKind.NORMAL.applyGaussianBlurfails on smart objects and text layers. The script skips the blur for those layers.- Layer bounds. The flip anchors to the layer's bounds, which cover only non-transparent pixels. If your cut-out has stray, nearly invisible pixels below the feet, the reflection sits too low. Clean the mask edge first.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| "General Photoshop error occurred" on blur | The layer is a smart object or text layer | Rasterize the copy first, or leave the blur at 0 |
| Reflection overlaps the subject | The layer has transparent padding, or the anchor is wrong | Check that you used `BOTTOMCENTER` and trim transparent edges |
| The reflection is not mirrored | `-100` was entered as `100` | Check the sign on the vertical value |
| `addReflection is not defined` | The function is declared inside another function | Move it to the top level of the file |
| Script does nothing on a group | `typename` is `LayerSet` | Select a single layer, or merge a copy of the group |
| Mask gradient paints on pixels, not on the mask | The mask was not the active target | Click the mask thumbnail before dragging |
Going further
Once the script works for one layer, you can run it on several. Loop over doc.layers, set doc.activeLayer to each item, and call addReflection. Check typename on each one so you skip groups. Keep one history step per layer, because a single step for the whole batch makes a partial failure harder to undo.
To automate the gradient too, record the drag as an Action. Alternatively, use the ScriptListener plug-in to capture the gradient's Action Manager code and paste it into the script. Recorded code is long and tied to fixed coordinates, so it rarely beats drawing the gradient yourself. Gradient length is also a decision the scene should make.
Scripting suits shadows less well. The cast-shadow shape depends on the light direction and the ground perspective, and both are judgment calls from earlier chapters. A script can create the Multiply layer and set its opacity and blur (the second code example in the plan does this). It cannot decide where the shadow falls.
Exercise
Your script duplicates a smart object layer and then stops with "General Photoshop error occurred" at the blur line. Why does this happen, and how do you change the script so it keeps working on both smart objects and normal layers?
Show answer
applyGaussianBlur() is not allowed on smart objects (or text layers), because they are not plain pixel layers. The fix is to guard the call. The script already does this with if (cfg.blur > 0 && refl.kind === LayerKind.NORMAL), so a smart object still gets flipped, faded and masked, just without the blur. To blur it anyway, rasterize the copy first, or apply a Gaussian Blur smart filter by hand.
Exercise
You run the script twice by accident, then realize you want to undo it. Why does one undo step remove a whole reflection, and which line of the script is responsible?
Show answer
The doc.suspendHistory("Add reflection", "addReflection(...)") call. It collapses every change made inside addReflection (duplicate, flip, move, opacity, blur, mask) into a single History entry, so one undo reverses all of them. Without it, you would need an undo for each step.
Exercise
The reflection of a bottle appears 30 pixels below where its base meets the table, leaving a visible gap. What are two possible causes, and what should you check first?
Show answer
One cause is stray, nearly transparent pixels below the bottle's base. Layer bounds include them, so the flip anchors lower than the visible edge. The other is CONFIG.gap set to a nonzero value. Check CONFIG.gap first, since it is a one-line fix. If it is 0, inspect the layer mask edge for faint pixels below the base and clean them up.