Skip to main content
Participant
December 12, 2022
Question

Conditionally increment value after some time

  • December 12, 2022
  • 2 replies
  • 319 views

I though it would be easy but I was heavily wrong. I'm searching for hours now but I couldn't find a solution. Maybe you can help me. I'll simplify the structure to make it easier to understand.

 

I have a "main" composition with 2 text layers (layer_1 and layer_2). The initial value of layer_1 is 0 and it's shown from the very beginning of the timeline. layer_2 is 5 seconds time-displaced and has a source text expression with an if/else statement.

 

Example:

var myVar = "test";

if(myVar == "test") {
  "Hello World";
}

 

Within the expression of layer_2 I'd like to increment the value from layer_1 after 5 seconds.

 

Example:

 

var myText = thisComp.layer("layer_1").text.sourceText;
var myVar = "test";

if(myVar == "test") {
  thisComp.layer("layer_1").text.sourceText.style.setText(myText + 1);
  "Hello World";
}

 

 

Unfortunately this isn't working. It seems I can not manipulate the source text from layer_1 within the expression of layer_2.

 

I checked many YouTube tutorials but I couldn't find the answer how to change the value of a text layer after a certain time depending on a condition.

 

I'm thankful for any kind of help.

This topic has been closed for replies.

2 replies

Mylenium
Legend
December 13, 2022

Your simple code will not get anything sorted in the truzest sense of the word. You have to run it in a loop and actually count the "hits". Anyway, none of that would actually even be necessary given your example. Start by re-formatting your Excel file. AE's data linking is column-centric, not row-based. Once that's done, you can easily also add a fourth row to fetch the cumulative results. Excel can do all the math for you and if you really only have this handful of entries, you could even animate it by hand. Seems to me like you are trying a simple thing very complicated.

 

Mylenium

Community Expert
December 13, 2022

If you want to change the Source Text based on time, try this:

 

t = time - inPoint;
sw = 2;

if (t < sw)
	"First Words"
else
	"Second Words"

 

If you want to change the opacity of a layer based on the text in a "Sample Text" layer, try this:

 

ref = thisComp.layer("Sample Text").text.sourceText;

if (ref == "First Words")
	100
else
	0

 

If you have a text layer names "Source" that has animated  and sayes First words for the first 2 seconds, then Second Words for the rest of the time, and you want the second text layer to change the characters in another text layer, try this:

 

ref = thisComp.layer("Sample Text").text.sourceText;
if (ref == "First Words")
	"Number 1"
else
	"Number 2"

 

Expressions only work on the property they are attached to. If you want one text layer to drive another, you have to look at that text layer with the expression.

 

You cannot fade between characters in a text layer with expressions. If you want to create a transition between text you have to use Text Animators and multiple layers. I'm not sure what you are trying to accomplish. Try explaining the design goal again. Maybe what you want when you say "the initial value of layer_01 is 0," and you want it to increment when the words in Layer_02 change is to increase the counter value layer_01, so you end up with a text layer that counts the number of changes in text layer 2.  If that is it, and the words in text layer 2 are keyframes, all you have to do is count the number of keys in layer 2. 

 

If you can clarify what you want to happen, we can probably come up with a solution. 

 

steefaanAuthor
Participant
December 13, 2022

Thanks for reply. Maybe I simplified my example too much so let me provide you more details.

 

I have a .csv file with 3 rows:

name,megapixels,megapixels_winner,weight,weight_winner
Canon EOS R5,45,1,658,1
Sony Alpha 7 IV,33,0,738,0

 

In After Effects I want to compare both cameras. Therefore I create text layers and assign them with the .csv data. Additionally I want to have a points counter for each camera. Each time when *_winner is 1 I want to increase the points counter by 1.

 

Therefore I created another text layer because I thought I can simply increase the value of the points counter. But as you said, expressions only work on the property they are attached to.

 

I couldn't find a solution to increase the points counter after each step of the comparison.

 

The points counter is another text layer with the initial value 0