• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Snap anchor point in the middle of the composition

New Here ,
Jan 05, 2024 Jan 05, 2024

Copy link to clipboard

Copied

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.

TOPICS
Expressions

Views

130

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 05, 2024 Jan 05, 2024

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 06, 2024 Jan 06, 2024

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines