Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity › Forums › Adobe After Effects Expressions › What is wrong with my approach, and how does “my” solution actually works?

  • What is wrong with my approach, and how does “my” solution actually works?

    Posted by Ramon Peppelenbos on May 31, 2014 at 7:14 am

    Hey all,

    I needed to make a calendar in Ae, and I needed a loop of numbers going from 1 to 31. I thought I had enough knowledge of expressions to pull this off, but unfortunately I hadn’t.

    This was my approach:

    I made a new text layer and dropped a slider control on that.
    in the source text of the layer, I create this expression:

    n = effect("Slider Control")("Slider");

    Math.round(n)

    Now I simply made 2 keyframes om the slider from 1 to 31, and used the loopOut(“cycle”) expression to make it loop. I was now able to adjust the speed with the keyframes, and everything worked fine.

    But I wanted to add one thing to it, and that took a lot more work than I thought. Basically I wanted to add a zero in front of the numer when the number was smaller than 10. So I thought I could make that with this expression:

    n = effect("Slider Control")("Slider");

    Math.round(n)

    if (n < 10){
    n = "0" + n;
    }else{
    n = n;
    }

    But this doesnt works. it doesnt add a 0 in front of the 1 digit numbers, but it also seems to mess up the Math.round expression.

    After some googling I found some expressions to make this work:

    s = effect("Slider Control")("Slider");
    v = Math.round(time*s);
    t = (v%31)+1;
    padLength = 2;
    t = t.toString();
    while (t.length < padLength) {
    t = "0" + t;
    }

    The only things I changed was the added slider for the speed (I believe the expression was v = Math.round(time/thiscompduration); of something like that.

    It does exactly what I want, but I don’t understand exactly how. also, when I make v the slider value, it also gives me errors.

    So my question is: What did do wrong in my approach, and how does the expression that works actually works? I’m really curious what toString actually does, what “(c%31)+1” does and why they used “while” instead of “if/else”

    Regards,

    Ramon Peppelenbos

    Ramon Peppelenbos replied 12 years, 4 months ago 2 Members · 4 Replies
  • 4 Replies
  • Declan Smith

    May 31, 2014 at 8:11 am

    You were so frustratingly close in your solution.

    The only thing wrong was that you didn’t assign the value derived by Math.round(n) to anything. Math.round(n) is a pass by value function not a pass by reference, so you need to assign the result to something.

    Also, for brevity, you don’t need the else clause because if n>=10 it won’t be changed by your if clause.

    Modified code below.

    Hope this helps.

    n = effect("Slider Control")("Slider");
    n=Math.round(n)
    if (n < 10){ n = "0" + n; }

    Declan Smith
    https://www.madpanic.tv
    After Effects CS6/ FCS3 / Canon XLH1 / Canon 7D / Reason / Cubase

    "it's either binary or it's not"

  • Ramon Peppelenbos

    May 31, 2014 at 8:29 am

    Thanks a lot! never heard about “pass by value function” or “pass by reference” before, but I kinda get an idea on what it means. I do understand now what I did wrong, and why I should assign Math.round to a variable. Good to hear I was close though!

    Regards,

    Ramon Peppelenbos

  • Declan Smith

    May 31, 2014 at 8:53 am

    To answer the second part of your question:
    This expression enables you to have a slider value that can be almost any value, i.e. keyframe it from 1 to 200.
    v = Math.round(time*s); This makes the value v related to time so you don’t have to set keyframes for your slider value. By multiplying it by time, it will effectively auto increment.

    t = (v%31)+1;

    The % operator is modulo. So this will take whatever value v is, divide it by 31 and give you the remainder. So if v was 64, t would be 3, if v was 31, t would be 0, hence adding 1 will ensure you get 1 – 31 displayed.

    t.toString();

    As t is an integer, the toString() method returns a string representation of the number, the equivalent of “” + t

    I would guess that the while loop is there because this expression was used for some other purpose originally, where perhaps the number range was larger and therefore, a consistent number of padded 0’s was needed. If you increase the padLength to 4, you will see the effect. Your number will be padded with up to 4 0’s.

    Declan Smith
    https://www.madpanic.tv
    After Effects CS6/ FCS3 / Canon XLH1 / Canon 7D / Reason / Cubase

    “it’s either binary or it’s not”

  • Ramon Peppelenbos

    May 31, 2014 at 10:48 am

    Thanks a lot for the additional information!

    Regards,

    Ramon Peppelenbos

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