Quantcast
Channel: Adobe Community : Popular Discussions - Photoshop Scripting
Viewing all articles
Browse latest Browse all 12244

Script I made: Instancer

$
0
0

Hey, I had some downtime at work so I decided to write this script I would have liked to have a few times in the past, and I thought I'd share it if anyone might find it useful or have any suggestions.

 

What it does is take a group of layers and places a copy of the layer on each count item null you've placed in the document.

I'm thinking of adding some companion scripts later for stuff like various ways to distribute the count items on the document, like based on existing layers, randomly, in a grid, so it will be a sort of primitive particle system of sorts.

 

Explained.png

 

Code

#target photoshop

 

 

var scriptName = "NinjaScripts Instancer";

var scriptVersion = "0002";

 

 

//Suspends history while running script to undo results in one step.

activeDocument.suspendHistory("NS_Instancer", "main()")

 

 

function main()

{

    if (activeDocument.countItems.length == 0) {alert ("Please use the Count Tool to designate instance positions"); return;}

    if (activeDocument.layerSets.length == 0) {alert ("Error, document doesn't contain any layer groups"); return;}

    OpenMainWindow();

}

 

 

//Iterates over count items list and places a random layer from the srcLayerset at its location

function runInstancer(srcLayerset, destLayerset)

{

    for (var i = 0; i < activeDocument.countItems.length; i++)

    {

        var randomIndex = getRandomInt(0, srcLayerset.artLayers.length-1)

        var newLayer = srcLayerset.artLayers[randomIndex].duplicate (destLayerset, ElementPlacement.INSIDE)

        translateLayerTo(newLayer,activeDocument.countItems[i].position[0],activeDocument.countIt ems[i].position[1]);

    }

}

 

 

//Generates a random integer between min and max

function getRandomInt(min, max)

{

    return Math.floor(Math.random() * (max - min + 1)) + min;

}

 

 

// returns the center of a layer's bounding box

function getCenter(inLayer)

{

    var Pivot = new Array();

    Pivot[0] = (inLayer.bounds[2] + inLayer.bounds[0])/2;

    Pivot[1] = (inLayer.bounds[3] + inLayer.bounds[1])/2;

    return Pivot

}

 

 

//translates a layer to a specific point

function translateLayerTo (inLayer, inX, inY)

{

    var layerPivot = getCenter(inLayer)

    inLayer.translate(-(layerPivot[0] - inX), -(layerPivot[1] -inY));

}

 

 

//Main interface wi

function OpenMainWindow()

{

    var MainWindow = new Window("dialog", scriptName + " v" + scriptVersion);

  

    MainWindow.orientation = 'column';

    MainWindow.alignChildren = 'center';

  

    var InputGroup = MainWindow.add("group");

    InputGroup.orientation = 'row';

  

        InputGroup.add("statictext", undefined, "Source sprite group:");

        var LayersetsDropdown = InputGroup.add("dropdownlist");

        LayersetsDropdown.preferredSize.width = 200;

        for (var i = 0; i < activeDocument.layerSets.length; i++)

        {

            LayersetsDropdown.add("item",activeDocument.layerSets[i].name);

        }

        LayersetsDropdown.selection = 0;

 

    ConfirmGroup = MainWindow.add("group");

    ConfirmGroup.orientation = 'row';

        OkButton = ConfirmGroup.add("button", undefined, "Ok");

        OkButton.onClick = function()

        {

                var srcLayerset = activeDocument.layerSets[LayersetsDropdown.selection.index];

                if (srcLayerset.artLayers.length == 0) {alert ("Error, group contains no layers"); return;}

                var newLayerset = activeDocument.layerSets.add();

                newLayerset.name = "NSInstancer Layers";

                runInstancer(srcLayerset, newLayerset);

                MainWindow.close();

        }

 

 

        CancelButton = ConfirmGroup.add("button", undefined, "Cancel");

      

    MainWindow.center();

  var result = MainWindow.show();

 

 

}


Viewing all articles
Browse latest Browse all 12244

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>