9 Replies Latest reply on Jan 4, 2010 5:14 PM by Rick Gerard

    Simple increment with expressions

    DC B. 09 Level 1

      Hi:

       

       

      I'm still struggling with expressions. How do you remember a previous state of the expression, say for a simple increment?

       

      i = i + 1;

        • 1. Re: Simple increment with expressions
          Mylenium Most Valuable Participant

          Such stuff needs to be incorporated into loops.

           

          n=0; //init counter
          i=0; //init value
          
          val=1;
          
          while (n<200)
            { i = i
              i += val
              n++}
          

           

          Mylenium

          • 2. Re: Simple increment with expressions
            DC B. 09 Level 1

            I'm sorry. I still don't get it. Say, just to test it I created a text layer and applied your expression as-it-is to the Source Text property. And as I would expect it the result shows 199 for each frame. My idea was to add 1 for each consecutive frame. What am I doing wrong?

            • 3. Re: Simple increment with expressions
              Todd_Kopriva Level 8
              > Say, just to test it I created a text layer and applied your expression as-it-is to the Source Text property. And as I would expect it the result shows 199 for each frame. My idea was to add 1 for each consecutive frame. What am I doing wrong?

               

              One thing that it's important to keep in mind is that expressions don't remember anything from one frame to the next. Each expression is evaluated fully for each frame.

               

              If you want to have a value go up by 1 for each frame, then you can do this easily by referencing the frame number directly.

               

              Here are some sections about expressions and time:

              "Expression time"

              "Time conversion methods"

              1 person found this helpful
              • 4. Re: Simple increment with expressions
                DC B. 09 Level 1

                Thanks. Yes, timeToFrames() will give me a frame number that can be used as I wanted. Just FYI, it'd be really nice if the next version of AE had some means to incorporate user-defined global variables that can be used throughout the comp.

                • 5. Re: Simple increment with expressions
                  Mylenium Most Valuable Participant

                  My idea was to add 1 for each consecutive frame. What am I doing wrong?

                   

                  For that you don'tr need custom loops at all. As Todd said, simply use timeToFrames(). Of course you can define your own termination criteria for a while() loop, e.g. while (n > timeToFrames(thisComp.duration)). Just a matter of combining the correct functions and methods.

                   

                  Mylenium

                  • 6. Re: Simple increment with expressions
                    Rick Gerard Adobe Community Professional & MVP

                    Using the timeToFrames() function works just fine for generating text but it does not work properly when you're using it for transformations. An expression to move a layer quickly from left to right in the x direction may be written like this:

                     

                    m = timeToFrames();
                    x = value [0] + m * 10;
                    [x, value[1]]
                    
                    

                     

                    Movement is created by multiplying the current X position by 10 times the frame number (m) . The movement is created but when you apply motion blur you get something that looks like this:

                    timeToFrames.png

                    Change the expression to this:

                     

                    m = time/thisComp.frameDuration;
                    x = value [0] + m * 10;
                    [x, value[1]]
                    
                    

                     

                     

                    The result is more like this:

                    timeBlur.png

                    You could also use just  m = time * 29.97 to produce the same result. I haven't figured out whether this is a feature of the timeToFrames() function or a bug, but I thought you should know. I wasted about an hour once trying to get the motion blur to work right before I discovered the problem.

                    • 7. Re: Simple increment with expressions
                      DC B. 09 Level 1

                      Mylenium wrote:

                       

                      My idea was to add 1 for each consecutive frame. What am I doing wrong?

                       

                      For that you don'tr need custom loops at all. As Todd said, simply use timeToFrames(). Of course you can define your own termination criteria for a while() loop, e.g. while (n > timeToFrames(thisComp.duration)). Just a matter of combining the correct functions and methods.

                       

                      Mylenium

                      I got it now. With the loops (while, for, do while) you basically recreate all the frames until the current one. Wow. What a waste without any way to remember the previous state of variables. I sincerely hope that developers of the AE implement it in the next versions. It will be so helpful.

                      • 8. Re: Simple increment with expressions
                        DC B. 09 Level 1

                        You could also use just  m = time * 29.97 to produce the same result. I haven't figured out whether this is a feature of the timeToFrames() function or a bug, but I thought you should know. I wasted about an hour once trying to get the motion blur to work right before I discovered the problem.

                        Rick, thanks for sharing. But isn't it because timeToFrames() returns an integer, i.e. 0, 1, 2, 3, etc. when the time function returns a floating-point number. That is why your "halo" had visible gradations.

                        • 9. Re: Simple increment with expressions
                          Rick Gerard Adobe Community Professional & MVP

                          But isn't it because timeToFrames() returns an integer, i.e. 0, 1, 2, 3, etc. when the time function returns a floating-point number. That is why your "halo" had visible gradations.

                          That is probably the root of the problem. If you round time by using m = Math.round(time) / thisComp.frameDuration motion blur just won't work properly. If , on the other hand you round the x position by using x = Math.round(value [0] + m * 10) then motion blur works as expected. Mathematically the result is the same, both versions of the expression move the layer exactly 10 pixels per frame, but the blur treats that movement completely differently when you round time.

                           

                          The only place I've seen this behavior is when you round time. Use any other mathematical formula with or without rounded numbers and motion blur works as expected. As I said before, I don't know whether to call this a bug or a feature.