Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Selecting layer with value

  • Selecting layer with value

    Posted by William Allum on November 13, 2015 at 5:14 pm

    Hi guys, hope you can help me.

    How can I convert a value to text so that I can use it in an expression, I want to reference a layer based on increasing values, for example from a slider.

    I’m trying to set the X position of a layer to the X position of another layer based on the current number of a slider, the value of the slider matching the title of the layer to focus on. For example:

    TITLE_01
    TITLE_02
    TITLE_03

    Slider = 2

    Set ball’s X position to TITLE_02

    This is what I have been using so far.

    Test = 02;
    X = thisComp.layer(“TITLE_”+Test).effect(“X”)(“Slider”);

    [X,0,0]

    Many thanks.

    William Allum replied 9 years, 2 months ago 3 Members · 5 Replies
  • 5 Replies
  • Dan Ebberts

    November 13, 2015 at 5:44 pm

    I’m not sure this is exactly what you’re looking for, but it should give you some ideas:


    s = "" + Math.round(effect("Slider Control")("Slider"));
    if (s.length < 2) s = "0" + s;
    try{
    x = thisComp.layer("TITLE_"+s).transform.position[0];
    }catch (e){
    x = 0;
    }
    [x,0,0]

    the try/catch is there in case your slider goes out of range.

    Dan

  • Kalleheikki Kannisto

    November 13, 2015 at 5:58 pm

    With your code, if you change the line

    Test = 02;
    to
    Test = "02";

    you will have the layer that you have applied the expression to move to the X position value set by the Slider on layer TITLE_02, with position Y and Z values of 0.

    Now, I assume you want the (ball) layer to move to the same x position as your selected layer. If so, I’d put a Layer Control on your ball layer and use the following code for ball position:

    pos = transform.position;
    layer_to_match = effect("Layer Control")("Layer");
    x=layer_to_match.transform.position[0];
    new_position = [x,pos[1],pos[2]]

  • William Allum

    November 13, 2015 at 6:05 pm

    Thanks for the replies Dan and Kalle,

    Kalle, you basically summed up my problem with the below:

    Test = 02;

    to
    Test = “02”;

    Without the “” I get an error, but with them the expression works fine. The problem is that where the value 02 is I want to add a link to a slider, 02 is a bad example in this case. But say if the slider was set to any number between 1 and 10, the expression would be looking at any layer from TITLE_1 to TTILE_10 based on this value.

  • Kalleheikki Kannisto

    November 13, 2015 at 6:27 pm

    Sorry I didn’t think it through: You can’t keyframe a layer control. So it would be a combination of Dan’s method of turning the slider value to a selector plus getting the x position of said layer.

    You can simplify the matter by leaving out the leading zero so that the layer name is TITLE_1, TITLE_2, etc.

    Thus, the code for ball position based on a slider would be:

    pos = transform.position;
    num_layers = 3; // highest layer number
    layer_num = Math.floor(effect("Slider Control")("Slider"));
    target_layer= "TITLE_"+layer_num;
    if (layer_num>0 & layer_num<=num_layers) {
    x=thisComp.layer(target_layer).transform.position[0];
    new_position = [x,pos[1],pos[2]]
    } else {
    pos}

    Dan would write it much neater, though 🙂

  • William Allum

    November 13, 2015 at 6:44 pm

    Ah awesome guys… got it to work, thanks so much! Made my day. 🙂

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