Activity › Forums › Adobe After Effects Expressions › Saving the result of a script as a global variable
-
Saving the result of a script as a global variable
Posted by Detlef Maerz on December 11, 2021 at 2:57 pmIs there a way in After Effects to save the result of an expression script as a global variable?
Simple example: I calculate with souceRectAtTime a numeric value . How could I access this value in a different place (different composition, different layer) without running this script again?Filip Vandueren replied 4 years, 4 months ago 4 Members · 3 Replies -
3 Replies
-
Andrei Popa
December 11, 2021 at 7:11 pmAre we talking about scripts or expressions?
Scripts are different files, pieces of code that you have to run. You can use extendscript for developing them
Expressions are the pieces of code that go inside the properties in AE (when you alt+click the stopwatch).
If you run a script and assign a value to a variable in a global scope, that variable is available until you close AE.
If we are talking about expressions, the best way I can think of that you can share a numeric value is adding the expression to a slider and access that slider in other parts of your project (with the pick-whip).
-
Constantin Maier
December 13, 2021 at 5:20 pmWell, when we’re talking about expressions, there is a way to declare a global variable in expressions and have them be accessible by other expressions. To do that you need to use the dollar sign to declare a variable, like this:
$.myVar = “Yolo”;
More about this here: https://blob.pureandapplied.com.au/the-global-object-in-after-effects/
However, as far as I’ve experimented with this, I found it quite unstable as I don’t know whether it’s exactly clear when the variable gets declared and sometimes the expressions will break for a while because AE didn’t recognize the variable declaration yet on the other expression. In general, I think it’s best to do it like Andrei Popa suggested, to save it as a numeric value on a slider or something like that and access that from the other expression.
-
Filip Vandueren
December 14, 2021 at 7:54 amIf there are multiple values that need to be shared, or if it is a string, or an array, etc. it would be cumbersome to add mulitple sliders.
I often use (hidden) text-layers for this. The sourceText expression would have some calculations and yield a JSON object, other expressions will parse that JSON object fromt he sourceText and get the properties they need.
for example, sourceText of layer “globaltxt”:
globals = {};
globals.a= Math.sqrt(time);
globals.pos = [Math.random(100,100), globals.a*5];
globals.description = time < 2 ? "hello" : "goodbye";
JSON.stringify(globals);
in other expressions:
globals = JSON.parse(thisComp.layer("globaltxt").text.sourceText.value);
globals.description + " " + globals.a;
Reply to this Discussion! Login or Sign Up