Skip to main content
 Mirabelle Bazinet
Participant
January 5, 2024
Question

Snap anchor point in the middle of the composition

  • January 5, 2024
  • 2 replies
  • 228 views

I am trying to snap the anchor point of a layer in the center of the composition under all circumstances, whether the layer has one or multiple parents and whether those parents or the layer undergo transformations (position, anchor point, scale, rotation).

 

This is what I have so far:

anchor point:

offset = [0,0];
scaleFactor = thisLayer.scale[0]/100;
if(thisLayer.hasParent){
testLayer = thisLayer;
	while(testLayer.hasParent){
scaleFactor *= testLayer.parent.scale[0]/100;
offset -= testLayer.parent.toWorld(testLayer.parent.anchorPoint) - testLayer.parent.anchorPoint;
testLayer = testLayer.parent;
		}
}
[thisComp.width/2, thisComp.height/2] - position + value + offset * scaleFactor

 

position:

offset = [0,0];
scaleFactor = thisLayer.scale[0]/100;
if(thisLayer.hasParent){
testLayer = thisLayer;
	while(testLayer.hasParent){
scaleFactor *= testLayer.parent.scale[0]/100;
offset -= testLayer.parent.toWorld(testLayer.parent.anchorPoint) - testLayer.parent.anchorPoint;
testLayer = testLayer.parent;
		}
}
[thisComp.width/2, thisComp.height/2] + offset * scaleFactor

 

it works perfectly if the layer has no parent, when I parent the layer, layer anchor point isn't snapped anymore.

This topic has been closed for replies.

2 replies

 Mirabelle Bazinet
Participant
January 6, 2024

It seems, I don't need the anchor point expression for now and I don't need to apply scale factor.

Applying this expression solely to the position yields pretty good results:

offset = [0, 0];
// scaleFactor = scale[0] / 100;

if (thisLayer.hasParent) {
    testLayer = thisLayer;
    
    while (testLayer.hasParent) {
        offset += testLayer.parent.toWorld(testLayer.parent.anchorPoint) - testLayer.parent.anchorPoint;
        // scaleFactor *= testLayer.parent.scale[0] / 100;

        testLayer = testLayer.parent;
    }
}

[thisComp.width / 2, thisComp.height / 2] - offset;

If there is no parent, it works.

if there is a parent, it works and if the parent's position is in the middle of the comp, I can change the the parent's  scale without affecting the child's anchor point.

 

But when the parent's position isn't in the middle of the composition, in this case changing the parent's scale moves the child anchor point. I don't know if I can fix it. It seems to be impossible.

 

For a better understanding, imagine that the child layer is a world map and its anchor point is an airplane.

Community Expert
January 6, 2024

I haven't got the time to fix your expression right now, but you need to use toWorld() to retrieve the position of the child layer.