ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Constraint a Null Object to the nearest Layer

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Peter Chicken
Constraint a Null Object to the nearest Layer
on Jul 1, 2017 at 12:57:45 pm

Hey guys,

is it possible to constraint a null object automatically to its nearest layer in the composition?

Lets say i have two small solids and a null. Solid 1 is on the left side of the composition and solid 2 on the right side. The null is in the middle of the two solids. When i move the null object towards solid 1 it should constraint automatically to the position data of solid 1. Same should happen when i move the null towards the solid 2.

Thank you! ☺

Peter


Return to posts index

Dan Ebberts
Re: Constraint a Null Object to the nearest Layer
on Jul 1, 2017 at 1:08:08 pm

Like this maybe:

p1 = thisComp.layer("Solid 1").position;
p2 = thisComp.layer("Solid 2").position;
d1 = length(p1,position);
d2 = length(p2,position);
if (d1 > d2) p2 else p1


Dan



Return to posts index

Peter Chicken
Re: Constraint a Null Object to the nearest Layer
on Jul 1, 2017 at 2:00:30 pm

Hey Dan,

thank you for your fast reply!
Your solution works great with my example of two solids and one null.
But how would you tackle a composition with maybe 10 solids and 10 null objects?


Return to posts index


Dan Ebberts
Re: Constraint a Null Object to the nearest Layer
on Jul 1, 2017 at 2:20:25 pm

Maybe like this:


minD = 999999;
closest = null;
for (i = 1; i <= thisComp.numLayers; i++){
if ( i != index && thisComp.layer(i).name.indexOf("Solid") > -1){
p = thisComp.layer(i).position;
d = length(position,thisComp.layer(i).position);
if (d < minD){
minD = d;
closest = p;
}
}
}
if (closest != null) closest else value;


The expression only considers layers with "Solid" in their name, so you may have to adjust the criteria.

Dan



Return to posts index

Peter Chicken
Re: Constraint a Null Object to the nearest Layer
on Jul 1, 2017 at 2:53:58 pm

Thank you for your help Dan! ☺
I will try this!


Return to posts index

Peter Chicken
Re: Constraint a Null Object to the nearest Layer
on Jul 1, 2017 at 11:09:10 pm

Is there a way to get the absolute position of the constrained solid? I used the following code in the hierarchy which you can see at the image below.

if (thisComp.layer("Checkbox").effect("Einstellungen für Kontrollkäst.")("Kontrollkästchen") == 1) {minD = 999999;
closest = null;
for (i = 1; i <= thisComp.numLayers; i++){
if ( i != index && thisComp.layer(i).name.indexOf("Horizontal") > -1){
p = thisComp.layer(i).position;
d = length(position,thisComp.layer(i).position);
if (d < minD){
minD = d;
closest = p;
}
}
}
if (closest != null) closest else value;} else
{minD = 999999;
closest = null;
for (i = 1; i <= thisComp.numLayers; i++){
if ( i != index && thisComp.layer(i).name.indexOf("Vertical") > -1){
p = thisComp.layer(i).position;
d = length(position,thisComp.layer(i).position);
if (d < minD){
minD = d;
closest = p;
}
}
}
if (closest != null) closest else value; }


"Kontrollkästchen" means Checkbox Control. Its german haha ☺

I need to get the position that is created by the null object that controlled the position of the solid, not the default position of the solid. Any ideas?

Thanks a lot ☺



Return to posts index


Dan Ebberts
Re: Constraint a Null Object to the nearest Layer
on Jul 1, 2017 at 11:24:43 pm

Are you saying that the solids have parents? In that case, you could do it like this:


minD = 999999;
closest = null;
for (i = 1; i <= thisComp.numLayers; i++){
if ( i != index && thisComp.layer(i).name.indexOf("Solid") > -1){
L = thisComp.layer(i);
p = L.toComp(L.anchorPoint);
d = length(position,p);
if (d < minD){
minD = d;
closest = p;
}
}
}
if (closest != null) closest else value;



Dan



Return to posts index

Peter Chicken
Re: Constraint a Null Object to the nearest Layer
on Jul 1, 2017 at 11:31:22 pm

Yes, the null objects called "horizontal" and "vertical" are the parents for the solids. The position of the null objects are also controlled by a slider. And the expression is used in the position of the solids.


Return to posts index

Peter Chicken
Re: Constraint a Null Object to the nearest Layer
on Jul 1, 2017 at 11:48:53 pm

Unfortunately, your new code doesnt work in that case. For better understanding what i am trying to achive:

I have a checkbox that controlled what type of nulls are parent the solids. If the checkbox is on, every null with the name "horizontal" should parent the nearest solid. If the checkbox is off, every null with the name "Vertical" should parent the nearest solid.


Return to posts index


Peter Chicken
Re: Constraint a Null Object to the nearest Layer
on Jul 3, 2017 at 2:22:40 pm

I think the problem is that the position of the solids are relative positions because they are controlled by the expression from Dan. The comp is searching for the closest null based on its absolute position. Thats the problem.

The anchorpoint change in the expression from Dan doesn't fix the problem.
Is there a way to make the comp search the nearest null from its relative position?


Return to posts index

Peter Chicken
Re: Constraint a Null Object to the nearest Layer
on Jul 3, 2017 at 2:31:53 pm

EDIT: i mean solid not comp

absolute position = original position without the expression


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]