ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

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

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Andrew Marston
Make switch/case expression choose any option except the current one?
on Apr 24, 2020 at 5:45:42 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 &lt; 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);


Return to posts index

Dan Ebberts
Re: Make switch/case expression choose any option except the current one?
on Apr 24, 2020 at 6:25:32 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



Return to posts index

Andrew Marston
Re: Make switch/case expression choose any option except the current one?
on Apr 30, 2020 at 8:12:05 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.


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2020 CreativeCOW.net All Rights Reserved
[TOP]