skip to Main Content

I’m trying to create a pattern script in Photoshop that duplicates an image horizontally and vertically over the whole canvas. But the issue is that across the x-axis it doubles its value every loop. If I remove the “j” loop, it works fine.

This pic will show you the issue I’m referring to https://imgur.com/a/0x9HhCS

        var offset = parseInt(prompt("Type in the offset (spacing between pics) value here.nDefault is 0px.", "0"));
        for (var i = 0; i < width / (layerWidth + offset); i++) {
            for (var j = 0; j < 3; j++) {
                app.activeDocument.layers[i, j].duplicate()
                app.activeDocument.layers[i, j].translate(i * (layerWidth + offset), j * (layerHeight + offset));
            }
        }

2

Answers


  1. the issue is related in how do you use offset:
    Translate refers to the bounding rect of the layer.
    If you have a 50px width image translated by 50px, the resulting layer width will be 100px.
    Try to use only the offset, each iteration.

    Login or Signup to reply.
  2. As volcanic mentioned, layers[i, j] isn’t a valid way of accessing your layers. I’m not even sure why this works. You’re supposed to select you original layer, make a copy and translate it. Something like this:

    var width = activeDocument.width.as("px");
    var height = activeDocument.height.as("px");
    var layer = app.activeDocument.activeLayer;
    var layerWidth = layer.bounds[2] - layer.bounds[0];
    var layerHeight = layer.bounds[3] - layer.bounds[1];
    var copy, i, j;
    
    var offset = parseInt(prompt("Type in the offset (spacing between pics) value here.nDefault is 0px.", "0"));   
    
    for (i = 0; i < width / (layerWidth + offset); i++)
    {
        for (j = 0; j < height / (layerHeight + offset); j++)
        {
            // in the each loop we select the original layer, make a copy and offset it to calculated values
            app.activeDocument.activeLayer = layer;
            copy = layer.duplicate();
            copy.translate(i * (layerWidth + offset), j * (layerHeight + offset));
        }
    }
    
    layer.remove(); // remove the original layer
    

    Result:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search