Copy link to clipboard
Copied
Hello,
I'm struggling with the toComp/toWorld and fromComp/fromWorld expressions. I kind of understand the basics, but I missing something somewhere to make it work.
So I have the following structure :
Parent 1 (parented as well, not not relevant here)
- Layer 1 (child of Parent 1, 3D, with a Z1 depth)
- Layer 2 (child of Parent 1, 3D, with a Z2 depth different than Z1, with Z2 fixed, like Z2 = Z1 + 400)
So basically, what I want to do is control Layer 2 position, that it's placed, visually, and the same X at the Layer 1 (despite a different Z).
If I use in position of Layer 2 :
L1 = thisComp.layer("Layer 1");
L1.toComp([thisComp.width/2,thisComp.height/2,0])[0];
I get the two layers linked on the X in the comp, despite the Z, but with an offset, because of the Z, if Z1 = Z2, I get what I want.
But how can I compensate for the Z difference.
Thank you in advance!
Best regards,
Christophe.
It's tricky. If your comp doesn't have a camera, it would be something like this:
zOffset = 400;
L = thisComp.layer("Layer 1");
p = L.toWorld(L.anchorPoint);
w = thisComp.width * thisComp.pixelAspect;
z = (w/2)/Math.tan(degreesToRadians(19.799));
c = [thisComp.width/2,thisComp.height/2,-z];
v = normalize(p - c);
if(hasParent){
parent.fromWorld(p + v*(zOffset/v[2]));
}else{
p + v*(zOffset/v[2]);
}
If it does have a camera, it would be like this:
zOffset = 400;
L = thisComp.layer("Layer 1");
p = L.toWorld(L.a
...Copy link to clipboard
Copied
What you want is way more complex than a simple toComp expression. toComp has nothing to do with the camera and what you want to do is to set Layer 2 to "Position of Layer 1 + some vector pointing from the camera to Position of Layer 1", i.e. going exactly backwards when looking from the current camera position.
For an easy -no manual coding at all - solution you could probably use the Auto Scale iExpression for this
Auto Scale Expression | mamoworld
set the position of layer 2 to the position of layer 1 and then apply the iExpression to it (and choose an offset of 400 in the parameters of the iExpression). If you apply the expression to only the position, it will move the layer as desired - if you also apply it to the scale, it will also scale it such that it seems to have the same size although it is now 400 pixels more far away.
Copy link to clipboard
Copied
It's tricky. If your comp doesn't have a camera, it would be something like this:
zOffset = 400;
L = thisComp.layer("Layer 1");
p = L.toWorld(L.anchorPoint);
w = thisComp.width * thisComp.pixelAspect;
z = (w/2)/Math.tan(degreesToRadians(19.799));
c = [thisComp.width/2,thisComp.height/2,-z];
v = normalize(p - c);
if(hasParent){
parent.fromWorld(p + v*(zOffset/v[2]));
}else{
p + v*(zOffset/v[2]);
}
If it does have a camera, it would be like this:
zOffset = 400;
L = thisComp.layer("Layer 1");
p = L.toWorld(L.anchorPoint);
c = thisComp.layer("Camera 1").toWorld([0,0,0]);;
v = normalize(p - c);
if(hasParent){
parent.fromWorld(p + v*(zOffset/v[2]));
}else{
p + v*(zOffset/v[2]);
}
Dan
Copy link to clipboard
Copied
Thank you Mathias for your suggestion! After reading your answer I tried to search further thanks to your mention about vector pointing to the cam. I reached the point where I was calculating the angle between the vector Layer1-Cam and the vector Layer2-Cam. Because if the angle is 0, it's aligned to the Cam point of view. But I wasn't taking care of the Z problem and the parent problem of layer 2.
So thank you very much Dan for the solution!! Every time I struggle with expressions and especially space transform ones I find your posts here and there giving answers or ideas where to go. So you've already helped me thousand of time
About the expression, I understand until the normalization of the vector, but after, the p + v*(zOffset/v[2]) part is more tricky. I think I see what you're doing. Basically you add the vector to the Layer 1 position (in world coordinates), but you don't use the vector from Layer 1 to Camera as it, but you compensate the zOffset and calculating what would the vector between the Layer 1 to Layer 2. Am I right?
Thank you!!
Christophe.
Copy link to clipboard
Copied
The normalized vector is a unit vector, so if you multiply that vector by 400/v[2] you end up with a vector where the z value is 400 and the x and y values are scaled proportionally.
Dan.
Copy link to clipboard
Copied
Ok! Thank you for the explanation!