Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Reference layer by index, defined by slider control

  • Reference layer by index, defined by slider control

    Posted by Noel Powell on November 3, 2014 at 5:26 pm

    I was wondering if anyone can think of a workaround for this apparent limitation in referencing layers. I am trying to reference a layer by its index, rather than its name. That’s easy but the error comes because I need the index layer to be defined by the user (via slider control). I have something like this:

    layerIndex = effect(“Slider Control Name”)(“Slider”);
    comp(“Comp Name”).layer(layerIndex).transform.opacity

    the error says “index out of range”. Any ideas? Thank you.

    After Effects templates – lens flares, light leaks, old film effects, digital glitches, VHS glitches, and more: https://CreationEffects.com

    Noel Powell replied 11 years, 10 months ago 2 Members · 3 Replies
  • 3 Replies
  • Noel Powell

    November 3, 2014 at 5:33 pm

    NVM, I just figured it out. I should have thought of this before. For anyone else who might want to do this – you can have the user define the layer using the Layer Control, rather than a Slider Control. Then in your expression, just pick whip the Layer Control.

    After Effects templates – lens flares, light leaks, old film effects, digital glitches, VHS glitches, and more: https://CreationEffects.com

  • Kevin Camp

    November 3, 2014 at 9:02 pm

    a layer control is definitely a better way to go, but you could use a slider to drive index, you’d just need to add a little more code to avoid the error.

    the first error you had was likely due to a number (like 0) that did not equate to a valid layer index. you could use an if statement like this to avoid the error:

    if (layerIndex > 0 && layerIndex <= comp("Comp Name").numLayers)

    you would also want to limit layerIndex to integers/whole numbers with a fuction like Math.round() or Math.floor(), etc…

    Kevin Camp
    Art Director
    KCPQ, KZJO & KRCW

  • Noel Powell

    November 3, 2014 at 9:13 pm

    Thanks for the response, Kevin. I actually had the slider control set to 9, and there was indeed a layer # 9. I would try the expression with the actual number in there, and it would work fine. Then I’d highlight the “9” and pick-whip the slider control, which was set to “9”, and it would return the error. So there must have been something else going on.

    Anyway, the Layer Control works great.

    After Effects templates – lens flares, light leaks, old film effects, digital glitches, VHS glitches, and more: https://CreationEffects.com

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