Highlighted

Expression: Snapshot of current value

Participant ,
Jun 04, 2017

Copy link to clipboard

Copied

Hi, how would you guys solve the following problem using expressions?:

I have custom Slider Control "xPos" , it is keyframed and its value changes constantly;

I have a custom Checkbox Control "Check". It is keyframed too. When Check is checked, i want to store xPos's Current Value in a Variable "a".

While xPos is changing further down the timeline, i want to be able to access "a", which should always hold the Value xPos had, when Check last changed to value 1.

When i uncheck Check, and check it again, i want a to update to the new current value of xPos.

If you want to see, what i tried so far, check this thread of mine : Expression: I am stuck

Problem is, that when i set a = xPos, it updates constantly, instead of just taking the current value and holding it...

Thanks and kind regards

Mutable global variables in AE (ie, variables whose value can change) are tricky because there are no global variables in AE and you're not allowed to change the value of another expression control (that you could use as a global variable) from within an expression control. But I came up with a hack for you. Create a slider named "a" (for your "global" variable 'a') and use this expression in it:

if (effect("Check")("Checkbox") == 1) {

    // walk backwards in time to find last time checkbox was Off

    var prevTime = time;

    while (prevTime > 0 && effect("Check")("Checkbox").valueAtTime(prevTime) == 1)

        prevTime -= 0.0001;

    // set value of this slider equal to the value that xPos slider had at point this checkbox transition to On

    effect("xPos")("Slider").valueAtTime(prevTime);

} else

    // checkbox off - set current value to -1 (or anything else you want it to be)

    -1;

Note that this expression will noticeably slow down AE because it has to iterate a lot of values for each frame.

TOPICS
Expressions

Views

1.1K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Expression: Snapshot of current value

Participant ,
Jun 04, 2017

Copy link to clipboard

Copied

Hi, how would you guys solve the following problem using expressions?:

I have custom Slider Control "xPos" , it is keyframed and its value changes constantly;

I have a custom Checkbox Control "Check". It is keyframed too. When Check is checked, i want to store xPos's Current Value in a Variable "a".

While xPos is changing further down the timeline, i want to be able to access "a", which should always hold the Value xPos had, when Check last changed to value 1.

When i uncheck Check, and check it again, i want a to update to the new current value of xPos.

If you want to see, what i tried so far, check this thread of mine : Expression: I am stuck

Problem is, that when i set a = xPos, it updates constantly, instead of just taking the current value and holding it...

Thanks and kind regards

Mutable global variables in AE (ie, variables whose value can change) are tricky because there are no global variables in AE and you're not allowed to change the value of another expression control (that you could use as a global variable) from within an expression control. But I came up with a hack for you. Create a slider named "a" (for your "global" variable 'a') and use this expression in it:

if (effect("Check")("Checkbox") == 1) {

    // walk backwards in time to find last time checkbox was Off

    var prevTime = time;

    while (prevTime > 0 && effect("Check")("Checkbox").valueAtTime(prevTime) == 1)

        prevTime -= 0.0001;

    // set value of this slider equal to the value that xPos slider had at point this checkbox transition to On

    effect("xPos")("Slider").valueAtTime(prevTime);

} else

    // checkbox off - set current value to -1 (or anything else you want it to be)

    -1;

Note that this expression will noticeably slow down AE because it has to iterate a lot of values for each frame.

TOPICS
Expressions

Views

1.1K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 04, 2017 1
Enthusiast ,
Jun 04, 2017

Copy link to clipboard

Copied

Mutable global variables in AE (ie, variables whose value can change) are tricky because there are no global variables in AE and you're not allowed to change the value of another expression control (that you could use as a global variable) from within an expression control. But I came up with a hack for you. Create a slider named "a" (for your "global" variable 'a') and use this expression in it:

if (effect("Check")("Checkbox") == 1) {

    // walk backwards in time to find last time checkbox was Off

    var prevTime = time;

    while (prevTime > 0 && effect("Check")("Checkbox").valueAtTime(prevTime) == 1)

        prevTime -= 0.0001;

    // set value of this slider equal to the value that xPos slider had at point this checkbox transition to On

    effect("xPos")("Slider").valueAtTime(prevTime);

} else

    // checkbox off - set current value to -1 (or anything else you want it to be)

    -1;

Note that this expression will noticeably slow down AE because it has to iterate a lot of values for each frame.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 04, 2017 2
Participant ,
Jun 04, 2017

Copy link to clipboard

Copied

Hey, thank you so much, that seems to work. You are right though, it does slow playback down quite drastically, once the checkbox is checked... Nonetheless - Great work!!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 04, 2017 0
Enthusiast ,
Jun 04, 2017

Copy link to clipboard

Copied

Here's a refined version that doesn't slow down AE (at least not enough to be noticeable for most comps):

if (effect("Check")("Checkbox") == 1) {

    // walk backwards in time to find last time checkbox was Off

    var minTimeIncrement = thisComp.frameDuration;

    var prevTime = time - minTimeIncrement;

    while (prevTime >= 0 && effect("Check")("Checkbox").valueAtTime(prevTime) == 1)

        prevTime -= minTimeIncrement;

    // set value of this slider equal to the value that xPos slider had at point this checkbox transition to On  

    effect("xPos")("Slider").valueAtTime(prevTime >= 0 ? prevTime+minTimeIncrement : 0);

} else

    // checkbox off - set current value to -1 (or anything else you want it to be)

  -1;

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 04, 2017 2
Advocate ,
Jun 07, 2017

Copy link to clipboard

Copied

If the checkbox value is driven by keyframes (i.e. not by an expression) you can use nearestKeyIndex, it should be much faster (no need to scan back in time frame by frame).

Xavier

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 07, 2017 2
Participant ,
Jun 13, 2017

Copy link to clipboard

Copied

Just saw your replies, guys, thanks so much for contributing!! I love AE Expressions!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 13, 2017 0
New Here ,
May 23, 2020

Copy link to clipboard

Copied

Thanks Horshack, your code really helped me out! I adjusted it to check by frames instead of by .0001 of time, which decreases the amount of calculations it has to run. Worked a treat for me, hope it does for someone else out there. Here's my adjusted code, which assumes a constantly changing slider called 'pos', and a checkbox called 'status':

if (effect("status")("Checkbox") == 1) {
// walk backwards in time to find last time checkbox was Off
var prevTime = timeToFrames(time);
while (prevTime > 0 && effect("status")("Checkbox").valueAtTime(framesToTime(prevTime)) == 1)
prevTime --;

// set value of this slider equal to the value that pos slider had at point this checkbox transitions to On
effect("pos")("Slider").valueAtTime(framesToTime(prevTime));

} else
// set value of this slider to 0, or whatever you want
0;

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 23, 2020 0