Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Is it possible randomly rotate duplicates of the same layer?

Community Beginner ,
Nov 01, 2024 Nov 01, 2024

I'm making a map of historical sites. I've marked each site with a light ball png that has light rays spreading out. I'm trying to make them not look quite so uniform so I'd ideally like to be able to select all 400 layers at once and have them all rotate to a random orientation between -180 and 180 degrees. Is there a way to do this? Thank you

TOPICS
Actions and scripting , macOS
927
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 01, 2024 Nov 01, 2024

Try this script, it assumes that you have selected all the layers to rotate randomly beforehand:

 

/*
Randomly rotate selected layers.jsx
Stephen Marsh
v1.0 - 2nd November 2024
Features: Single undo step, selected layers and visibility are retained, facilitating multiple script runs
Special thanks jazz-y for the code to work with multiple selected layers
*/

#target photoshop

// Single history stage undo
activeDocument.suspendHistory("Randomly rotate selected layers", "main()");


function main
...
Translate
Adobe
Community Expert ,
Nov 01, 2024 Nov 01, 2024

Yes, this is possible with scripting.

 

Have you tested this manually? The resampling may lead to less than ideal results.

 

Please post a screenshot of your layer panel so that the layer structure is identifiable (such as are you using groups/nested groups or artboards).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 01, 2024 Nov 01, 2024

 

 

 

Thanks for the reply. So my very bottom layer is at 0% transparency, it was my reference map to place all the markers but I want to keep it there incase I need to change something. The one above that is the artistic, stylised map with endless markers above it. I have tried the method manually to a few using a random number generator and typing the values in, it looks better than not doing it. What do you think the less than ideal results might be? Do you have a better suggestion?

 

 

Screenshot 2024-11-01 at 12.14.55.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2024 Nov 01, 2024

OK, that layer structure doesn't look like it holds any nasty surprises.

 

Can you show a screenshot of the original marker graphic, and one rotated at an arbitrary angle for comparison at the same 100% zoom level?

 

As I said, the resampling/interpolation for the rotation may not lead to the best results, but it depends on the image content, size, resampling algorythm etc.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 01, 2024 Nov 01, 2024

Here are the first ones I did, a couple are randomly rotated, one is in its original position. I've included the full map for reference too.

 

Screenshot 2024-11-01 at 13.00.25.pngScreenshot 2024-11-01 at 12.57.38.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2024 Nov 01, 2024

OK, I can now see what type of content is being rotated. 

Will the anchor point be the middle centre?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 01, 2024 Nov 01, 2024

Correct

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2024 Nov 01, 2024

Try this script, it assumes that you have selected all the layers to rotate randomly beforehand:

 

/*
Randomly rotate selected layers.jsx
Stephen Marsh
v1.0 - 2nd November 2024
Features: Single undo step, selected layers and visibility are retained, facilitating multiple script runs
Special thanks jazz-y for the code to work with multiple selected layers
*/

#target photoshop

// Single history stage undo
activeDocument.suspendHistory("Randomly rotate selected layers", "main()");


function main() {

    // Capture the initial layer visibility and layer selection
    var currentLayersState = getLayersVisiblity();

    // Get the selected layers: courtesy of jazz-y
    var s2t = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var lrs = executeActionGet(r).getList(p),
        sel = new ActionReference();

    // Loop over the selected layers: courtesy of jazz-y
    for (var l = 0; l < lrs.count; l++) {
        sel.putIdentifier(s2t('layer'), p = lrs.getReference(l).getIdentifier(s2t('layerID')));
        (r = new ActionReference()).putIdentifier(s2t('layer'), p);
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        //d.putBoolean(s2t("makeVisible"), false);
        executeAction(s2t('select'), d, DialogModes.NO);

        // Generate a random integer degree value between -180 and 180
        var randomDegree = Math.floor(Math.random() * 360 - 180);
        //alert(randomDegree);

        // Set the rotation angle
        var idTrnf = charIDToTypeID("Trnf");
        var desc = new ActionDescriptor();
        var idnull = charIDToTypeID("null");
        var ref = new ActionReference();
        var idLyr = charIDToTypeID("Lyr ");
        var idOrdn = charIDToTypeID("Ordn");
        var idTrgt = charIDToTypeID("Trgt");
        ref.putEnumerated(idLyr, idOrdn, idTrgt);
        desc.putReference(idnull, ref);
        var idOfst = charIDToTypeID("Ofst");
        var offsetDesc = new ActionDescriptor();
        offsetDesc.putUnitDouble(charIDToTypeID("Hrzn"), charIDToTypeID("#Pxl"), 0);
        offsetDesc.putUnitDouble(charIDToTypeID("Vrtc"), charIDToTypeID("#Pxl"), 0);
        desc.putObject(idOfst, idOfst, offsetDesc);
        var idAngl = charIDToTypeID("Angl");
        desc.putUnitDouble(idAngl, charIDToTypeID("#Ang"), randomDegree);
        executeAction(idTrnf, desc, DialogModes.NO);
    }

    // Restore the initial layer visibility and selection
    setLayersVisiblity(currentLayersState);
}


///// Functions /////

function getLayersVisiblity() {
    // by jazz-y
    var s2t = stringIDToTypeID,
        t2s = typeIDToStringID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var targetLayers = executeActionGet(r).getList(p),
        seletion = [],
        visiblity = {};

    for (var i = 0; i < targetLayers.count; i++) seletion.push(targetLayers.getReference(i).getIdentifier());
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var len = executeActionGet(r).getInteger(p);

    for (var j = 1; j <= len; j++) {
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerSection'));
        r.putIndex(s2t('layer'), j);
        if (t2s(executeActionGet(r).getEnumerationValue(p)) == 'layerSectionEnd') continue;
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerID'));
        r.putIndex(s2t('layer'), j);
        var id = executeActionGet(r).getInteger(p);
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('visible'));
        r.putIndex(s2t('layer'), j);
        var visible = executeActionGet(r).getBoolean(p);
        visiblity[id] = visible;
    }
    return {
        selection: seletion,
        visiblity: visiblity
    };
}

function setLayersVisiblity(layersStateObject) {
    // by jazz-y
    var s2t = stringIDToTypeID;
    for (var a in layersStateObject.visiblity) {
        makeVisible = layersStateObject.visiblity[a] ? "show" : "hide";
        (r = new ActionReference()).putIdentifier(s2t('layer'), a);
        (d = new ActionDescriptor()).putReference(s2t('target'), r);
        executeAction(s2t(makeVisible), d, DialogModes.NO);
    }
    if (layersStateObject.selection.length) {
        var r = new ActionReference();
        for (var i = 0; i < layersStateObject.selection.length; i++)
            r.putIdentifier(s2t("layer"), layersStateObject.selection[i]);
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        d.putBoolean(s2t("makeVisible"), false);
        executeAction(s2t("select"), d, DialogModes.NO);
    } else {
        (r = new ActionReference()).putEnumerated(s2t("layer"), s2t('ordinal'), s2t('targetEnum'));
        (d = new ActionDescriptor()).putReference(s2t('target'), r);
        executeAction(s2t('selectNoLayers'), d, DialogModes.NO);
    }
}

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 03, 2024 Nov 03, 2024

Thank you very much for this! 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 03, 2024 Nov 03, 2024
LATEST
quote

Thank you very much for this! 


By @Michael25967188dyl9

 

You're welcome. If you have any questions or additional requests, please let me know, otherwise, please mark the script provided as a correct answer, thanks.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines