Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions Make switch/case expression choose any option except the current one?

  • Make switch/case expression choose any option except the current one?

  • Andrew Marston

    April 24, 2020 at 5:45 pm

    After doing the Workbench L-System tutorial, I’ve been trying to modify the script so that the path doesn’t double back on itself. My goal is create a script generated paths that sort of looks like growing circuitry. Below is the code from the tutorial. I believe this involves making the switch(r) function remember it’s current case and choose any case that isn’t that. However, after researching expressions and javascript switch/case functions, this is clearly above my current coding ability. Anyone know how to solve this, or point me in the right direction? I’m a bit stumped. I’ve also attached a screenshot with the bits where the path backtracks on itself I’m trying to get rid of.

    s = thisComp.layer("Controller").effect("Random Seed")("Slider");
    n = thisComp.layer("Controller").effect("Steps")("Slider");
    l = thisComp.layer("Controller").effect("Max Length")("Slider");
    seedRandom(s, true);
    var pts = [];
    var x = y = 0;
    pts.push([x,y]);
    for(i = 0; i < n; i++) {
    var d = Math.floor(random(l));
    var r = Math.floor(random(4));
    switch(r) {
    case 0:
    x += d;
    break;
    case 1:
    y += d;
    break;
    case 2:
    x -= d;
    break;
    case 3:
    y -= d;
    break;
    }
    pts.push([x,y]);
    }
    createPath(pts, [], [], false);

  • Dan Ebberts

    April 24, 2020 at 6:25 pm

    Try it this way:


    s = thisComp.layer("Controller").effect("Random Seed")("Slider");
    n = thisComp.layer("Controller").effect("Steps")("Slider");
    l = thisComp.layer("Controller").effect("Max Length")("Slider");
    seedRandom(s,true);
    var pts = [];
    var x = y = 0;
    pts.push([x,y]);
    rPrev = Math.floor(random(4));
    for(i = 0; i < n; i++) {
    var d = Math.floor(random(l));
    var r = (rPrev + 3 + Math.floor(random(3)))%4;
    switch(r) {
    case 0:
    x += d;
    break;
    case 1:
    y += d;
    break;
    case 2:
    x -= d;
    break;
    case 3:
    y -= d;
    break;
    }
    pts.push([x,y]);
    rPrev = r;
    }
    createPath(pts, [], [], false);

    Dan

  • Andrew Marston

    April 30, 2020 at 8:12 pm

    Thanks Dan! This works beautifully when there are 4 options. However when I try to adapt it to 3 or 8 options for example, I can’t figure out how to make it work.

Viewing 1 - 3 of 3 posts

Log in to reply.

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