How to find angle of rotation of photoshop layer

I found a way to get the current rotation angle for a text layer. More precisely, I adapted the method from ExtendScript to UXP.

function angleFromMatrix(yy, xy) {
    var toDegs = 180/Math.PI;
    return Math.atan2(yy, xy) * toDegs - 90;
}

function getActiveLayerRotation() {
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var desc = executeActionGet(ref).getObjectValue(stringIDToTypeID('textKey'));
    if (desc.hasKey(stringIDToTypeID('transform'))) {
        desc = desc.getObjectValue(stringIDToTypeID('transform'));
        var yy = desc.getDouble(stringIDToTypeID('yy'));
        var xy = desc.getDouble(stringIDToTypeID('xy'));
        return angleFromMatrix(yy, xy);
    }
    return 0;
}

This works in JSX.

async function getActiveLayerRotationAngle() {
    const app = require("photoshop").app;
    const activeDocument = app.activeDocument;
    const activeLayer = activeDocument.activeLayers[0];
    
    const layerId = activeLayer._id;
    const transform = await batchPlay([
        {
            "_obj": "get",
            "_target": [{ "_ref": "layer", "_id": layerId }],
            "_property": "transform"
        }
    ], {});
    const { textKey: { transform: { yy, xy } } } = transform[0];
    const toDegs = 180 / Math.PI;
    const angle = Math.atan2(yy, xy) * toDegs - 90;
    console.log("angle=" + angle);
    return angle;
}

This works in UXP.

I was specifically looking for a method for text layers.

1 Like