Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Out of range error with layer index

  • Out of range error with layer index

    Posted by Eva Lepik on May 15, 2017 at 8:24 am

    Hey,

    I am trying to link camera’s focus distance to a chosen layer.
    The layer can be chosen (and keyframed) with a slider.

    I have also added a Math.round expression to controller slider.
    But the expression below gives Out of range error, and I can’t understand why.

    Could anybody show me where the mistake is?
    Thanks!

    Eva

    n = thisComp.layer("CONTROLS").effect("Focus on layer")("Slider"); // Math.round expression is added to slider
    Z = thisComp.layer(n).transform.position[2];

    Z-(thisComp.layer("Cam control").transform.position[2])

    Eva Lepik replied 9 years ago 2 Members · 4 Replies
  • 4 Replies
  • Steve Sierra

    May 15, 2017 at 9:20 am

    Hi,

    You can’t have n = 0, since it is used as the layer index in Z (indexes start at 1).
    What happens when your (“Focus on Layer”) slider hits 0 ?

    I think you should do an if/else statement to set a default value to your focal distance when the slider is at 0.

    Hope this helps ????

    Cheers !

  • Eva Lepik

    May 15, 2017 at 9:42 am

    Hi Steve,

    Thanks for the reply.
    I put this expression to slider to avoid it hits 0, and this seems to work ok (below).

    That’s why I don’t understand the reason it gives me ‘out of range’ error.
    Right now the slider value is 6, and still the error is there.

    Regards,

    Math.round(clamp(value, min=5, max=thisComp.numLayers));

  • Steve Sierra

    May 15, 2017 at 10:53 am

    Hi again,

    Try putting your clamp in your n variable…

    n = clamp(thisComp.layer(“CONTROLS”).effect(“Focus on layer”)(“Slider”), 5, thisComp.numLayers);
    Z = thisComp.layer(n).transform.position[2];

    Z-(thisComp.layer(“Cam control”).transform.position[2])

    Cheers !

  • Eva Lepik

    May 16, 2017 at 7:37 am

    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) {
    }

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy