FORUMS: list search recent posts

# If statement only executes once - Expression problem

FAQ   •   VIEW ALL
 If statement only executes once - Expression problem on Jul 10, 2012 at 5:47:19 pm

So I am trying to make an expression have an on and off duration, all my math is good as far as I can tell but one of the if conditions only runs once. If I duplicate the if it will work as many times as I copy it, any help is really appreciated.

t = time*24;
period=10;
holdTime=2;
loop=period+holdTime;
if(t>=loop) t=t-loop;
if(t/period>=1) 100 else 0;

The if that doesn't repeat is "if(t>=loop) t=t-loop;" The weird part is the second if will always execute.

 Re: If statement only executes once - Expression problemon Jul 10, 2012 at 7:29:58 pm

Expressions have no memory; they are evaluated anew on each frame. The value of t is not remembered from one frame to the next, so this expression will not constrain the value of t to a single cycle from 0 to 12.

If you want the value to cycle, you can use the modolo (%) operator and so-called "clock arithmetic."

More simply, you could just set some keyframes and add the expression `loopOut("cycle")` to have them repeat over and over.

What exactly is it you are trying to do?

Walter Soyka
Principal & Designer at Keen Live
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
RenderBreak Blog - What I'm thinking when my workstation's thinking
Creative Cow Forum Host: Live & Stage Events

 Re: If statement only executes once - Expression problemon Jul 10, 2012 at 7:42:09 pm

I am just trying to make a preset that is off for 10 frames than on for 2 frames. So if you apply this to opacity it will have a value of 0 from frames 0-9 and a value of 100 for frames 10 & 11, then to have that repeat.

I don't want to use keyframes because this is a constant issue I face and if I can just update the numbers and have it update rather than add new keyframes and loop it would save me a huge amount of time.

 Re: If statement only executes once - Expression problemon Jul 10, 2012 at 8:00:02 pm

[Jeo Greko] "I don't want to use keyframes because this is a constant issue I face and if I can just update the numbers and have it update rather than add new keyframes and loop it would save me a huge amount of time."

Right -- the idea is to make a couple hold keyframes and add an expression.

Create a keyframe at frame 0 at 100 opacity; right-click it and Toggle Hold Keyframe.

Advance to frame 10 and add a keyframe with the value 0.

Advance to frame 12 and add a keyframe with the value 100.

Alt-click the opacity stopwatch and enter the following expression:
`loopOut("cycle");`

To modify the timing of the loop, you need only modify the timing of the three keyframes; loopOut will work the looping magic for you.

Walter Soyka
Principal & Designer at Keen Live
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
RenderBreak Blog - What I'm thinking when my workstation's thinking
Creative Cow Forum Host: Live & Stage Events

 Re: If statement only executes once - Expression problemon Jul 10, 2012 at 8:05:27 pm

Or, if you really want an expression:

```myTimeInFrames = timeToFrames(time - inPoint); period = 10; hold = 2; myLoopTimeInFrames = myTimeInFrames % (period + hold); if (myLoopTimeInFrames < 10) 100 else 0;```

The modulo operator (%) returns the remainder after integer division. With this constants, the value cycles from 0 to 11, then repeats.

FYI, there is an AE Expressions forum [link] here.

Walter Soyka
Principal & Designer at Keen Live
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
RenderBreak Blog - What I'm thinking when my workstation's thinking
Creative Cow Forum Host: Live & Stage Events

 Re: If statement only executes once - Expression problemon Jul 10, 2012 at 8:21:20 pm

Hey wow,

this is exactly what I was aiming to do, thank you very much for your time.
I will make sure to post on the expression forum next time.

 Re: If statement only executes once - Expression problemon Jul 10, 2012 at 9:09:02 pm

Minor error in the above! I left a constant value in the last line. The expression SHOULD read:

```myTimeInFrames = timeToFrames(time - inPoint); period = 10; hold = 2; myLoopTimeInFrames = myTimeInFrames % (period + hold); if (myLoopTimeInFrames < period) 100 else 0;```