Skip to content

Instantly share code, notes, and snippets.

@NimaiMalle
Last active January 14, 2022 00:03
Show Gist options
  • Save NimaiMalle/4356e3912b36e1476e13204ca88bd747 to your computer and use it in GitHub Desktop.
Save NimaiMalle/4356e3912b36e1476e13204ca88bd747 to your computer and use it in GitHub Desktop.
Photoshop Script to interleave layers for use with Lenticular Lens Sheets.
#target Photoshop
function makeLayerMask(maskType) {
if( maskType == undefined) maskType = 'RvlS' ; //from selection
//requires a selection 'RvlS' complete mask 'RvlA' otherThanSelection 'HdSl'
var desc140 = new ActionDescriptor();
desc140.putClass( charIDToTypeID('Nw '), charIDToTypeID('Chnl') );
var ref51 = new ActionReference();
ref51.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Msk ') );
desc140.putReference( charIDToTypeID('At '), ref51 );
desc140.putEnumerated( charIDToTypeID('Usng'), charIDToTypeID('UsrM'), charIDToTypeID(maskType) );
executeAction( charIDToTypeID('Mk '), desc140, DialogModes.NO );
}
// two element array of numbers for x,y start of line,
// two element array of numbers for x,y endof line,
//number;line width in pixels
// uses foreground color
function drawLine( startXY, endXY, width ) {
var desc = new ActionDescriptor();
var lineDesc = new ActionDescriptor();
var startDesc = new ActionDescriptor();
startDesc.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), startXY[0] );
startDesc.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), startXY[1] );
lineDesc.putObject( charIDToTypeID('Strt'), charIDToTypeID('Pnt '), startDesc );
var endDesc = new ActionDescriptor();
endDesc.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), endXY[0] );
endDesc.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), endXY[1] );
lineDesc.putObject( charIDToTypeID('End '), charIDToTypeID('Pnt '), endDesc );
lineDesc.putUnitDouble( charIDToTypeID('Wdth'), charIDToTypeID('#Pxl'), width );
desc.putObject( charIDToTypeID('Shp '), charIDToTypeID('Ln '), lineDesc );
if (width != Math.floor(width)) desc.putBoolean( charIDToTypeID('AntA'), true );
executeAction( charIDToTypeID('Draw'), desc, DialogModes.NO );
};
function main() {
var doc = app.activeDocument;
var numLayers = doc.artLayers.length;
var visLayers = 0;
for (var i = 0; i < numLayers; i++) doc.artLayers[i].visible && visLayers++;
var res = doc.resolution;
var lensLPI = Number(prompt("Lenticular Sheet LPI?","40","Lines Per Inch Value"));
var lineThickness = res * (1.0/lensLPI) * (1.0/visLayers);
if (lineThickness != Math.floor(lineThickness)) {
lineThickness = Math.ceil(lineThickness);
var idealRes = 1.0 / ((1.0/lineThickness) * (1.0/lensLPI) * (1.0/visLayers));
app.activeDocument.resizeImage(undefined,undefined,idealRes,ResampleMethod.BICUBIC);
}
preferences.rulerUnits = Units.PIXELS;
var j = 0;
for (var i = 0; i < numLayers; i++) {
var layer = doc.artLayers[i];
if (layer.visible) {
app.activeDocument.activeLayer = layer;
$.writeln(app.activeDocument.activeLayer.name);
var bounds = layer.bounds;
activeDocument.selection.selectAll();
makeLayerMask('HdSl');
app.foregroundColor.rgb.red = 255;
app.foregroundColor.rgb.green = 255;
app.foregroundColor.rgb.blue = 255;
for (var y=j*lineThickness; y<bounds[3]; y+= visLayers*lineThickness) {
drawLine( [0,y], [bounds[2],y], lineThickness );
// break;
}
j++;
}
// break;
}
}
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.INCHES;
main();
preferences.rulerUnits = originalRulerUnits;
@cvansas
Copy link

cvansas commented Mar 21, 2021

Hi there,
Im getting an error in Photoshop CS 2020 saying that the make command is not available, line 11.
Any idea how this is caused?

image with the error

@cvansas
Copy link

cvansas commented Mar 21, 2021

update: I used a layer with points and comma's after renaming to Layer1, Layer2 .... it worked.

Thanks for this very usefull script!

@NimaiMalle
Copy link
Author

I'm glad you figured it out! I was just testing it out again, after not having used it in a while, and I didn't have any issues. I even tried renaming the layers to use commas and periods in the name, but I still didn't get an error. What was the exact layer name that caused the issue?

@cvansas
Copy link

cvansas commented Mar 23, 2021 via email

@NimaiMalle
Copy link
Author

Running this script could be one part of a larger workflow, including using Photoshop Actions to record and play back any additional steps.

@mindfu23
Copy link

Love it. Beautiful. Thank you so much!
Just getting into Lenticular photography, looking forward to testing this out with some photos and some plastic sheets I just ordered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment