Copy link to clipboard
Copied
Hello,
I'm encountering issues with a script I've been working on for After Effects, which is designed to offset a layer's anchor point based on its rotation and position relative to a guide layer. The script works as expected when the layer has rotation only on one axis, but it fails to produce accurate results when the layer has rotations on two or three axes.
Here’s the code I'm using:
var layerPosition = layer.position.value;
var currentAnchor = layer.anchorPoint.value;
var guidePosition = guideLayer.position.value;
var guideX = guidePosition[0];
var guideY = guidePosition[1];
var guideZ = guidePosition[2];
var offsetX = guideX - layerPosition[0];
var offsetY = guideY - layerPosition[1];
var offsetZ = guideZ - layerPosition[2];
var layerOrientation = layer.orientation.value;
var layerRotationX = layerOrientation[0];
var layerRotationY = layerOrientation[1];
var layerRotationZ = layerOrientation[2];
var thetaX = layerRotationX * Math.PI / 180;
var thetaY = layerRotationY * Math.PI / 180;
var thetaZ = layerRotationZ * Math.PI / 180;
var cosThetaX = Math.cos(thetaX);
var sinThetaX = Math.sin(thetaX);
var cosThetaY = Math.cos(thetaY);
var sinThetaY = Math.sin(thetaY);
var cosThetaZ = Math.cos(thetaZ);
var sinThetaZ = Math.sin(thetaZ);
var rotatedOffsetX = offsetX * cosThetaZ + offsetY * sinThetaZ;
var rotatedOffsetY = -offsetX * sinThetaZ + offsetY * cosThetaZ;
var rotatedOffsetZ = offsetZ;
var tempX = rotatedOffsetX * cosThetaY - rotatedOffsetZ * sinThetaY;
var tempZ = rotatedOffsetX * sinThetaY + rotatedOffsetZ * cosThetaY;
rotatedOffsetX = tempX;
rotatedOffsetZ = tempZ;
var tempY = rotatedOffsetY * cosThetaX + rotatedOffsetZ * sinThetaX;
tempZ = -rotatedOffsetY * sinThetaX + rotatedOffsetZ * cosThetaX;
rotatedOffsetY = tempY;
rotatedOffsetZ = tempZ;
var newAnchorX = currentAnchor[0] + rotatedOffsetX;
var newAnchorY = currentAnchor[1] + rotatedOffsetY;
var newAnchorZ = currentAnchor[2] + rotatedOffsetZ;
I would appreciate it if someone could hel.
Thanks very much
Copy link to clipboard
Copied
Unless you properly convert the orientation to actual consistent angles or use matrices this of course won't work. The old Quarternions vs. Euler thing.
Mylenium