Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Scripting: rotated layer anchor point

  • Scripting: rotated layer anchor point

    Posted by Otávio Nascimento on February 14, 2024 at 1:52 am

    I want to move the anchor point in a rotated layer and maintain the visual position via script. I tried this code below but I dont know how to make it work:

    var layer = app.project.activeItem.selectedLayers[0];
    var position = layer.property("ADBE Transform Group").property("ADBE Position");
    var anchorPoint = layer.property("ADBE Transform Group").property("ADBE Anchor Point");
    var comp = app.project.activeItem;
    var rotation = layer.property("ADBE Transform Group").property("ADBE Rotate Z");
    var compwidth = comp.width / 2;
    var compheight = comp.height / 2;
    var positionOffsetX = compwidth - pos.value[0];
    var positionOffsetY = compheight - pos.value[1];
    // Rotate the position offsets based on the layer's rotation
    var rotPoint = rotatePoint(positionOffsetX, positionOffsetY, rotation);
    var anchorX = anchorPoint.value[0] + rotPoint[0];
    var anchorY = anchorPoint.value[1] + rotPoint[1];
    anchorPoint.setValue([anchorX, anchorY, anchorPoint.value[2]]);
    position.setValue([compwidth, compheight, position.value[2]]);
    function rotatePoint(x, y, angle) {
    var radians = toRadians(angle);
    var newX = x * Math.cos(radians) - y * Math.sin(radians);
    var newY = x * Math.sin(radians) + y * Math.cos(radians);
    return [newX, newY];
    }
    function toRadians(degrees) {
    return (degrees.valueAtTime(app.project.activeItem.time, false) * Math.PI) / 180;
    }

    Brie Clayton
    replied 2 years, 3 months ago
    3 Members · 5 Replies
  • 5 Replies
  • Filip Vandueren

    February 14, 2024 at 2:29 pm

    It’s not clear where you want the script to move the layer to ? Or put differently, where should teh anchorPoint be when the script is finished ? In the center of the Comp ?

  • Otávio Nascimento

    February 14, 2024 at 2:56 pm

    Yes, in the center of the composition in this example. Later I want to modify the code so I can move the anchor point to the corners of the composition as well.

  • Filip Vandueren

    February 14, 2024 at 4:13 pm

    I think it’s easiest to use an intermediate expression for this:

    via scripting add a Point Control effect to the layer, give its point this expression:

    fromCompToSurface([thisComp.width/2,thisComp.height/2,0]);

    then read the result of that expression and set it as the new anchorpoint value,

    then set the position’s value to [comp.width/2, comp.height/2, 0].

    You can then also remove the point control effect

  • Otávio Nascimento

    February 14, 2024 at 10:18 pm

    Thank you Filip Vandueren, that worked!

  • Brie Clayton

    February 14, 2024 at 11:09 pm

    Thanks for solving this, Filip!

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy