3.02 Applying Random Constraints to AE

I moved into AE ready to translate processing to JavaScript only to find that OH SHIT its not just syntax that is different but the entire principle of how the code is looped through.

I managed to find a blog post from 2012 that addresses this issue [link]. Basically, for and while loops are not all that common in after effects expressions because AE tries to run through the entire for loop every time there is a new frame. In his blog post Lovatt finds a workaround and even provides the AE projects he made with working For, While and Do While loops.

I downloaded them and poked around inside the projects trying to figure out how they work.

Screen Shot 2017-01-21 at 16.48.38.png

It looked as if what Zack had created was a while loop that constrained the cycles of the loop to one per frame of the composition. Within that, he just wrote an if statement that acted like a switch, and was dependent on the position of the star being over a certain y value (the star moved randomly with a wiggle expression). If it was then hit count would increase for each frame this was true and be displayed as the small number in the corner of the composition.


To begin with I tried copying the code over from processing and then working through it, debugging as I went along.


I almost got the code to work (I suspect) using a new integer curFrame to count frames and ensure that each new frame on the composition was a cycle of the main for loop. The only (visible) issue is the line while(used[thisran]!==0] that checks whether the number has been picked before.

I also have a suspicion that I will need to restructure the code, because ideally, the colours would all be assigned in the first frame of the composition. I’m going to build this code up again line by line and see where it falls apart…

Firstly, I managed to get the expressions to pick a random number and call it’s assignment from within the array.

Next I got it to call thisran’s assignment from within both the stuff and the used arrays. Now the number changes randomly and the used array counts how many times they have been used.

You’ll notice that the array has 7 spaces but never calls value 7. After Effects throws an error if I reduce the array to 6 so i’m just keeping it like this for now. Further down the line, however I would like to introduce the option of increasing and decreasing the number of colours that can be used in my script.


Ok so here I am still getting the same issue I found when copying the code across from processing. AE really doesn’t like my while loop so i’m going to have to replace it with 6 nestled if statements.


Even with 6 if statements, some duplicates come through. I now realise that this is because of the random picker, it choses a new number at random every time. So it could take more than 6 attempts to get a number that hasn’t been chosen before, especially towards the end of the 6 value sequence.

All in all, I haven’t been able to figure out a way to make this code work inside After Effects, however this is not a huge issue since I only need to make it work in my script, which will be constructed in Script Editor.


Author: Pip Williamson

Pip // Animator & Motion Designer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s