FORUMS: list search recent posts

Only Set Variable When Condition is True, then Freeze Variable Value

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Bryce Poole
Only Set Variable When Condition is True, then Freeze Variable Value
on Oct 20, 2014 at 4:32:41 pm

Hello,

I'm wondering if something like this is possible with expressions in AE.

I'd like to declare a variable and while a condition is true set the variable's value, but then as soon the the condition is no longer true, stop setting that value and retain the frozen value in the variable for use elsewhere.

Something like whats in the code below.

Is this possible to write with expressions?

if (Counter > 500) {
pxl = str.length;
pxl;
} else {
pxl;
}


Return to posts index

Dan Ebberts
Re: Only Set Variable When Condition is True, then Freeze Variable Value
on Oct 20, 2014 at 5:56:58 pm

Expression variables don't persist from frame to frame, so depending on what you're trying to do, your expression may have to loop back in time to find out if the condition you're looking for has ever occurred.


Dan



Return to posts index

Bryce Poole
Re: Only Set Variable When Condition is True, then Freeze Variable Value
on Oct 20, 2014 at 6:38:03 pm

Hey Dan,

I'm trying to create a bit of code that will give me the length of a string (in characters) right up until it's pixel length crosses a certain threshold, or if it has already crossed that threshold how many characters are in the string up to that point.

I'm trying something like this, but its just returning undefined once the value is greater than 2000.

//define the variables
var str = thisComp.layer("Text Input").text.sourceText;
var pxCounter = thisComp.layer("pxCounter").text.sourceText;
var pxl;


//define counterTrigger function
function counterTrigger (lng) {
lng = str.length;
return lng;
}


// if statement to run function while string length is less than 2000 pixels, otherwise show last
// length

if (pxCounter&lt;2000) {
pxl = counterTrigger (pxl);
} else {
pxl;
}


Return to posts index


Dan Ebberts
Re: Only Set Variable When Condition is True, then Freeze Variable Value
on Oct 20, 2014 at 6:55:36 pm

If pxCounter is greater than 2000, pxl is not defined because variables do not survive from one frame to the next. You have to re-calculate them every frame.


Dan



Return to posts index

Dan Ebberts
Re: Only Set Variable When Condition is True, then Freeze Variable Value
on Oct 20, 2014 at 7:06:21 pm

I think what your expression needs to do, when it discovers that 2000 has been exceeded, it needs to go back in time, frame-by-frame, (using .valueAtTime()) to find the time when that event occurred, then retrieve the string length for that time.

Dan



Return to posts index

Bryce Poole
Re: Only Set Variable When Condition is True, then Freeze Variable Value
on Oct 20, 2014 at 7:59:42 pm

Hey Dan,

Great, thanks for the suggestion, I'll look into this! Will this still work if the value of pxCounter is going to always be the same at all frames and doesn't change? The pxCounter Width is based on a user input string, while that string my vary depending on what is entered, once its entered it will remain constant throughout the comp.

Can I use .valueAtTime() and just pick a frame to have it store that value for me? Say:

var pxCounter = thisComp.layer("var CharCounter4OverlayLength").text.sourceText.valueAtTime(1);


Return to posts index


Dan Ebberts
Re: Only Set Variable When Condition is True, then Freeze Variable Value
on Oct 20, 2014 at 8:06:02 pm

You wouldn't need to use .valueAtTime() for anything that doesn't change during the duration of the comp, just the dynamic elements.


Dan



Return to posts index

Bryce Poole
Re: Only Set Variable When Condition is True, then Freeze Variable Value
on Oct 20, 2014 at 8:27:04 pm

Hmmm, okay. So, while the value technically doesn't change over time it does change depending on what is entered by the user. I'm trying to find some way to catch the .length value of a string at the point the entered string became too long and store that value. The problem I'm having is the variable constantly updates as the string length grows. I tried placing it in a function which is returning the value, then only calling it while true. My though process being that since the function isn't being called again it's last value would still be remembered, but that doesn't appear to be the case. Any thoughts on what I could try in this case, where time isn't a factor?

//define the variables
var str = thisComp.layer("Text Input").text.sourceText;
var pxCounter = thisComp.layer("pxCounter").text.sourceText;
var pxl;


//define counterTrigger function
function counterTrigger (lng) {
lng = str.length;
return lng;
}


// if statement to run function while string length is less than 2000 pixels, otherwise show last
// length

if (pxCounter<2000) {
pxl = counterTrigger (pxl);
} else {
pxl;
}


Return to posts index

Dan Ebberts
Re: Only Set Variable When Condition is True, then Freeze Variable Value
on Oct 21, 2014 at 3:28:35 am

Clearly I don't understand what you're trying to do. Something is changing over the duration of the comp, correct? If so, that's what you need to analyze with .valueAtTime() to find when it reached the trigger condition. If not, I have no idea...


Dan



Return to posts index


Bryce Poole
Re: Only Set Variable When Condition is True, then Freeze Variable Value
on Oct 21, 2014 at 7:34:36 pm

Hey Dan,

I know what I'm trying to do is probably a bit unusual, thanks for bearing with me on this one.

I'm trying to setup an informational lower third that an editor can copy past some text into and it will auto resize itself based on what is entered. The lower third will also look at the text and add line breaks when it gets to long to help keep everything in the box. I have some code to add the line breaks, however it is inserting them at specific character positions in the string. Because the string may sometimes contain wider characters (the font is variable width) and the box size will be changing based on the string, I'd rather use the string pixel width to denote where in the main string a line break should be entered.

What I'm hoping to do is setup a trigger that looks for when the string pixel length crosses a certain threshold, then once it does, it grabs that strings current length and use it as the position to insert the line break.

While I have been able to grab the current string length once the trigger is crossed, it continues to update as the string grows, I'm trying to find someway to have After Effects look for when the threshold occurs, grab that value, retain it and then stop changing it.

I know I could tie the break point to an expression slider to have it be easily change able but the text will be entered by the editor inside Premiere Pro (Using 2014's ability to share AE text with Premiere) so the editor won't have access to the AE sliders.

Hopefully this helps explain what I'm trying to do a bit better, though so far I haven't been able to figure out how to retain the string length. Any thoughts on how I might accomplish this?

Thanks Dan!


Return to posts index

Dan Ebberts
Re: Only Set Variable When Condition is True, then Freeze Variable Value
on Oct 21, 2014 at 8:04:06 pm

Her'e a little test example. I'm using a simplified set of characters (a,b,c,d,e) and an array of fake pixel widths for those. The rest of the code determines at what character index in my test string, the total pixel width exceeds the max (100 in this case). When I run it here, the result is 18, which means the carriage return probably needs to go after the 18th character (index of 17) to avoid exceeding the maximum. Your setup would of course be much more complicated, because you have to deal with a much larger character width table. If you start your table with the space character, you would subtract 32 instead of 97. I hope this helps.


myTestString = "abcecdeeeadcdadacdbddeda";

// a b c d e
myCharWidths = [5.5,5.5,5.5,5.5,5.5];

myPixelMax = 100;
myPixelWidth = 0;
for (i = 0; i < myTestString.length; i++){
myPixelWidth += myCharWidths[myTestString.charCodeAt(i) - 97];
if (myPixelWidth > myPixelMax) break;
}
i



Dan



Return to posts index

Bryce Poole
Re: Only Set Variable When Condition is True, then Freeze Variable Value
on Oct 21, 2014 at 11:39:38 pm

Hey Dan,

That looks great, I think that will do exactly what I'm looking for. However I'm having an issue with inserting quote characters, I'm starting my table at 32 to allow spaces and some common symbols to be used.
However, when I get to spots 34 or 39 (either of the quote characters) the script breaks, is there a way I can selectively escape these out?


Return to posts index


Dan Ebberts
Re: Only Set Variable When Condition is True, then Freeze Variable Value
on Oct 22, 2014 at 12:04:14 am

I think you can just use a backslash for the one you're using to enclose the string. So for " it would be like this:

myStr = " !\"#$%&'()*+,-./";


Dan



Return to posts index

Bryce Poole
Re: Only Set Variable When Condition is True, then Freeze Variable Value
on Oct 22, 2014 at 4:08:33 pm

Dan, thank you very much this is brilliant! I think this will do exactly what I am looking for, thank you again for your help!


Return to posts index

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