ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Toggle variable with time

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Mike Foran
Toggle variable with time
on May 22, 2018 at 9:29:19 pm
Last Edited By Mike Foran on May 22, 2018 at 9:59:06 pm

I'm trying to build an expression that toggles a variable at 0 or 500 at time intervals. So for 3 seconds it's 0, then for 7 seconds it's 500, repeat...

The reason for doing this is to try to get Particular to emit a dotted line from an emitter traveling at a constant speed. My plan is to toggle the particles/sec on and off (0 or 500) as the emitter moves, thus creating a dotted line. My time estimate of 3 and 7 seconds is just a guess and will certainly need to be modified. I';d appreciate any help on this kind of expression.

EDIT: I thought I found the answer in another post here: https://forums.creativecow.net/readpost/227/29494 but I can't seem to get the get the value to toggle at specific time intervals- it's more based on percentages and I am getting unexpected results.


Return to posts index

Steve Bentley
Re: Toggle variable with time
on May 24, 2018 at 6:04:11 am

Wouldn't it be easier to keyframe the emission value of particular? Won't the particles per sec need to be tied to the speed of the emission point, so you get dotted lines with a specific period.

Otherwise Dan's code should do the trick with a few mods for your situation.
Assuming 30fps:

pps = 500; // particles per second
onFrames = 90; // 3 seconds at 30fps
offFrames=270; // you could tie these two to a slider so you woudn't have to open the expression again when you change the time span/
f = timeToFrames(time-inPoint)%(onFrames+offFrames); //AE works in seconds so you have to convert to get frames
if (f >onFrames && f


Return to posts index

Mike Foran
Re: Toggle variable with time
on May 24, 2018 at 12:54:41 pm

I messed around with keyframing first, but it was cumbersome. The emitter path is a really long path and I wanted an easy way to control the line with a couple sliders that determine the rate at which the emitter would turn on and off. I actually solved it myself, although the results weren't what I wanted. Despite the path being long the emitter wasn't traveling slow enough to give acceptable results, and the line suffered from interference patterns between the frames and the emitter rate. Subframe sampling doesn't work in Particular for an emitter expression, only path shapes. This created uneven dotting.

I'm not terribly competent with expressions so I am sure someone could create a more elegant bit of script, but this is the essence of what I ended up with:

range = 8
it = Math.floor(time / range)
if (time > range) {v = time - ( it * range)} else {v = time};

v


This doesn't include the additional sliders for specific on/off controls. But it uses time to increase a variable that loops from 1 to 8.


Return to posts index


Mike Foran
Re: Toggle variable with time
on May 24, 2018 at 1:05:16 pm

Sorry I just re-read my original post and the script I provided didn't completely solve it. Here's a more detailed script that references two sliders named 'On and 'Off' that are on a controller null.

on = thisComp.layer("Controller").effect("On")("Slider");
off = thisComp.layer("Controller").effect("Off")("Slider");
range = on+off
it = Math.floor(time / range)
if (time > range) {v = time - ( it * range)} else {v = time};
if (v > on) {e = 0} else {e=500};

e


This script sets the emitter to 500 for however many seconds the 'On' slider indicates, and sets it to 0 for however many seconds the 'Off' slider indicates. But like I said, accuracy is based on your frame rate and the speed of the emitter, which created overlapping patterns in the emitter. It wasn't working for this usage. I'm sure there's a more elegant way to write it.


Return to posts index

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