Skip to main content
jeffc54426914
Participant
February 7, 2018
Question

Auto layer dupping

  • February 7, 2018
  • 5 replies
  • 707 views

I'm trying to find a way to duplicate an image in one layer and duplicate it multiple times randomly rotating, sizing and repositioning each one.

Does anyone know of anything like this or even close?

This topic has been closed for replies.

5 replies

c.pfaffenbichler
Community Expert
Community Expert
February 21, 2018

Have you tested the edited Script?

Kukurykus
Legend
March 16, 2018

I wonder why some people ask for something they might get answer for, but are not anymore interested if that happened!

c.pfaffenbichler
Community Expert
Community Expert
February 7, 2018

(edited)

// duplicate and transform layer;

// 2018, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

var docHeight = myDocument.height.as('px');

var docWidth = myDocument.width.as('px');

var theID = getLayerId();

var ref = new ActionReference();

ref.putProperty(charIDToTypeID( "Prpr" ), stringIDToTypeID("bounds"));

ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var layerDesc = executeActionGet(ref);

var bounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));

var left = bounds.getUnitDoubleValue(stringIDToTypeID("left"));

var top = bounds.getUnitDoubleValue(stringIDToTypeID("top"));

var right = bounds.getUnitDoubleValue(stringIDToTypeID("right"));

var bottom = bounds.getUnitDoubleValue(stringIDToTypeID("bottom"));

var layerX = left + (right - left) / 2;

var layerY = top + (bottom - top) / 2;

// duplicate;

for (var m = 0; m < 50; m++) {

var centerX = Math.random()*docWidth;

var centerY = Math.random()*docHeight;

var theScale = Math.random()*60+40;

layerDuplicateScaleRotate(theID, centerX - layerX, centerY - layerY, theScale, theScale, Math.random()*360);

}

};

////// duplicate layer (id, xOffset, yOffset, theXScale, theYScale, theAngle) //////

function layerDuplicateScaleRotate (theID, xOffset, yOffset, theXScale, theYScale, theAngle) {

// based on code by mike hale, via paul riggott;

var ref = new ActionReference();

    ref.putIdentifier(charIDToTypeID("Lyr "), theID);

    var desc = new ActionDescriptor();

    desc.putReference(charIDToTypeID("null"), ref );

    desc.putBoolean( charIDToTypeID( "MkVs" ), false );

   try{

    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );

}catch(e){

alert(e.message);

};

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

    var desc23 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref2 = new ActionReference();

        ref2.putIdentifier ( charIDToTypeID( "Lyr " ), theID );

//        ref2.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

    desc23.putReference( idnull, ref2 );

    desc23.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ) );

    var idOfst = charIDToTypeID( "Ofst" );

        var desc24 = new ActionDescriptor();

        var idPxl = charIDToTypeID( "#Pxl" );

        desc24.putUnitDouble( charIDToTypeID( "Hrzn" ), idPxl, xOffset );

        desc24.putUnitDouble( charIDToTypeID( "Vrtc" ), idPxl, yOffset );

    var idOfst = charIDToTypeID( "Ofst" );

    desc23.putObject( idOfst, idOfst, desc24 );

    var idPrc = charIDToTypeID( "#Prc" );

    desc23.putUnitDouble( charIDToTypeID( "Wdth" ), idPrc, theXScale );

    desc23.putUnitDouble( charIDToTypeID( "Hght" ), idPrc, theXScale );

    desc23.putUnitDouble( charIDToTypeID( "Angl" ), charIDToTypeID( "#Ang" ), theAngle );

    desc23.putEnumerated( charIDToTypeID( "Intr" ), charIDToTypeID( "Intp" ), stringIDToTypeID( "bicubicAutomatic" ) );

    desc23.putBoolean( charIDToTypeID( "Cpy " ), true );

executeAction( charIDToTypeID( "Trnf" ), desc23, DialogModes.NO );

};

// by mike hale, via paul riggott;

// http://forums.adobe.com/message/1944754#1944754

function getLayerId(){

//Assumes activeDocument and activeLayer

var ref = new ActionReference();

ref.putProperty(charIDToTypeID( "Prpr" ), charIDToTypeID('LyrI'));

ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));

d = executeActionGet(ref);

return d.getInteger(charIDToTypeID('LyrI'));

};

Tom Winkelmann
Inspiring
February 7, 2018

What about the "random fill"-script as starting point?

c.pfaffenbichler
Community Expert
Community Expert
February 7, 2018

Good idea.

But depending on what the OP is after exactly (should individual Layers be kept editable, should Smart Objects be used, …) it may not suffice.

c.pfaffenbichler
Community Expert
Community Expert
February 7, 2018

Another point: When people speak of »random« they often seem to mean a limited randomness, so one should give some thought to how one intends to limit the randomness within any variant beforehand.

Chuck Uebele
Community Expert
Community Expert
February 7, 2018

Its just a matter of writing some functions on how you want to manipulate the layer - normally by using scriptListener, then creating a loop to loop though how many times you want to repeat something and adding a random calculation into the rotation, size, etc. For example in this image, I wanted to create tiles in a somewhat close order to the original image, but I wanted each tile offset a bit. So I create some code that made a selection, but I added a random element to it to move it around, then I moved it back into order, so the tiles were lined up.

Basically to add some randomness to a number you use something like this:

var myRandomNum = Math.random()*theRange + theOffset

You times the Math.random by how much of a spread you want, the you either add or subtract the offset.

JJMack
Community Expert
Community Expert
March 16, 2018

Chuck what did he do to you to make you treat him the way you did? Man dices to pieces

JJMack
Chuck Uebele
Community Expert
Community Expert
March 16, 2018

My father. Sometime I really felt like I really didn't know him.