Hi again Steve,
Thanks for the advice, that helped!
But as the resulted animation hold the focus distance until next keyframe (clamped, or maybe this is a wrong word), after trial and error I came up with the expression below:
This works, also if there are no keyframes on the layer index slider.
I’m pretty sure it could be shorter or written in a better form, but it seams to hold the focus on the layer on all cases ☺
Maybe that’s useful for someone else, too.
Regards,
Eva
x = thisComp.layer(2).effect("Focus on layer")("Slider");
n = 0;
try {
if (x.numKeys > 0) { // if there are keyframes
n = x.nearestKey(time).index;
if (x.key(n).time > time) n--;
}
if (n > 0 && x.numKeys >= n+1 ) { // if there is 1 more keyframe after current keyframe
t = time;
tMin = x.key(n).time;
tMax = x.key(n+1).time;
y1 = x.key(n).value;
y2 = x.key(n+1).value;
z1 = thisComp.layer(y1).position[2];
z2 = thisComp.layer(y2).position[2];
CamPos1 = thisComp.layer("Cam control").position.valueAtTime(tMin)[2];
CamPos2 = thisComp.layer("Cam control").position.valueAtTime(tMax)[2];
value1 = z1-(CamPos1); // Z position of layer 1 - Z position of cam
value2 = z2-(CamPos2); // Z position of layer 2 - Z position of cam
ease(t, tMin, tMax, value1, value2);
}
else if (n > 0 && x.numKeys = n ) { // if this is the last keyframe
y1 = x.key(n).value;
z1 = thisComp.layer(y1).position[2];
CamPos = thisComp.layer("Cam control").position[2];
cameraOption.focusDistance = z1 - CamPos;
}
else {
x = Math.round(clamp (thisComp.layer(2).effect("Focus on layer")("Slider"), 5, thisComp.numLayers));
L = thisComp.layer(x).position[2];
CamPos = thisComp.layer("Cam control").position[2];
L - CamPos
}}
catch (e) {
}