-
Scripting: rotated layer anchor point
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;
}