Skip to main content
xpertpse
Participant
September 14, 2012
Question

How can I determine the rotate angle of an artLayer?

  • September 14, 2012
  • 2 replies
  • 3488 views

I have a document with multiple layers. I can rotate a layer via the script. However, if the layer is already rotated via some angle, how can i determine that via the script?

This topic has been closed for replies.

2 replies

Legend
September 16, 2012

You can create a linked and disabled vector mask with only two ancor points and determine the rotation of the layer via calculations points coordinates.

c.pfaffenbichler
Community Expert
Community Expert
September 14, 2012

What are you talking about?

Smart Objects or regular Layers (of the various kinds)?

xpertpse
xpertpseAuthor
Participant
September 14, 2012

I am talking about the regular layers.

Inspiring
May 8, 2023

You can't get the angle directly from the layer's transparency. You can load the transparency as a selection. Make a path from that selection. Then examine the pathPoints to find an angle in the path. With a simple rectanglar shaped layer you should be able to get close to the correct angle if it was only rotated once and less than +- 89 degrees.

function loadTransparency(){

    var desc = new ActionDescriptor();

        var ref = new ActionReference();

        ref.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) );

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

        var ref1 = new ActionReference();

        ref1.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Trsp" ) );

    desc.putReference( charIDToTypeID( "T   " ), ref1 );

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

};

function makePathFromSelection() {

    var desc = new ActionDescriptor();

        var ref = new ActionReference();

        ref.putClass( charIDToTypeID('Path') );

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

        var channelRef = new ActionReference();

        channelRef.putProperty( charIDToTypeID('csel'), charIDToTypeID('fsel') );

    desc.putReference( charIDToTypeID('From'), channelRef );

    desc.putUnitDouble( charIDToTypeID('Tlrn'), charIDToTypeID('#Pxl'), 2.000000 );

    executeAction( charIDToTypeID('Mk  '), desc, DialogModes.NO );

};

function activePathIndex(){

    try{

        var ref = new ActionReference();

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

        var desc = executeActionGet( ref );

        return  desc.getInteger(charIDToTypeID("TrgP" ));

    }catch(e){}

};

function measureAngle(path){

   var res = new Array;

   var lineStart = path.subPathItems[0].pathPoints[0].anchor;

   var lineEnd = path.subPathItems[0].pathPoints[1].anchor;

   var a = Math.max(lineStart[0],lineEnd[0])-Math.min(lineStart[0],lineEnd[0]);

   var o = Math.max(lineStart[1],lineEnd[1])-Math.min(lineStart[1],lineEnd[1]);

   var c = Math.round(Math.sqrt((a*a)+(o*o)));

   res.push(c);

   var ang = (180/Math.PI) * Math.atan2(o,a);

   if(lineStart[1] < lineEnd[1]){//negative angle

      ang = -ang;

   };

   res.push(ang);

   return res;

};

loadTransparency();

makePathFromSelection();

var tempPath = app.activeDocument.pathItems[activePathIndex()];

var angle = measureAngle( tempPath )[1];

tempPath.remove();

alert( Math.abs(angle) );


This is the best code for measuring the layer angles! Many thanks!