Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions How to set global variables and/or Functions in composition

  • How to set global variables and/or Functions in composition

    Posted by Evan Jenkins on March 29, 2020 at 6:40 pm

    I have a massive piece of code that sets variables by referencing sliders in a control layer.

    I have 30 layers where I apply this same piece of code. If I need to adjust the code, I have to delete 29 layers, and then re-do them all haha. It’s crazy.

    Is there some way to make my variables global, so those variables apply to all layers? Can I get a function to apply to all layers?

    myID = effect("myID")("Slider");

    firstPhotoInPoint = thisComp.layer("Control Sliders").effect("firstPhotoInPoint")("Slider");

    checkSections = thisComp.layer("^sections").text.sourceText;

    sectionOneStart = thisComp.layer("Control Sliders").effect("sectionOneStart")("Slider");
    sectionOnePhtNum = thisComp.layer("Control Sliders").effect("sectionOnePhtNum")("Slider");
    sectionOneDur = thisComp.layer("Control Sliders").effect("sectionOneDur")("Slider");
    sectionOnePhtTime = sectionOneDur / sectionOnePhtNum;

    sectionTwoStart = thisComp.layer("Control Sliders").effect("sectionTwoStart")("Slider");
    sectionTwoPhtNum = thisComp.layer("Control Sliders").effect("sectionTwoPhtNum")("Slider");
    myIDSectionTwo = myID - sectionOnePhtNum;
    sectionTwoDur = thisComp.layer("Control Sliders").effect("sectionTwoDur")("Slider");
    sectionTwoPhtTime = sectionTwoDur / sectionTwoPhtNum;

    sectionThreeStart = thisComp.layer("Control Sliders").effect("sectionThreeStart")("Slider");
    sectionThreePhtNum = thisComp.layer("Control Sliders").effect("sectionThreePhtNum")("Slider");
    myIDSectionThree = myID - sectionOnePhtNum - sectionTwoPhtNum;
    sectionThreeDur = thisComp.layer("Control Sliders").effect("sectionThreeDur")("Slider");
    sectionThreePhtTime = sectionThreeDur / sectionThreePhtNum;

    sectionFourStart = thisComp.layer("Control Sliders").effect("sectionFourStart")("Slider");
    sectionFourPhtNum = thisComp.layer("Control Sliders").effect("sectionFourPhtNum")("Slider");
    myIDSectionFour = myID - sectionOnePhtNum - sectionTwoPhtNum - sectionThreePhtNum;
    sectionFourDur = thisComp.layer("Control Sliders").effect("sectionFourDur")("Slider");
    sectionFourPhtTime = sectionFourDur / sectionFourPhtNum;

    sectionFiveStart = thisComp.layer("Control Sliders").effect("sectionFiveStart")("Slider");
    sectionFivePhtNum = thisComp.layer("Control Sliders").effect("sectionFivePhtNum")("Slider");
    myIDSectionFive = myID - sectionOnePhtNum - sectionTwoPhtNum - sectionThreePhtNum - sectionFourPhtNum;
    sectionFiveDur = thisComp.layer("Control Sliders").effect("sectionFiveDur")("Slider");
    sectionFivePhtTime = sectionFiveDur / sectionFivePhtNum;

    if (checkSections == "sections" && myID <= sectionOnePhtNum) {
    timeElapsed = sectionOnePhtTime * (effect("myID")("Slider") - 1);
    timeStart = firstPhotoInPoint + timeElapsed;

    } else if (checkSections == "sections" && myID <= (sectionOnePhtNum + sectionTwoPhtNum)) {
    timeElapsed = firstPhotoInPoint + sectionOneDur + (sectionTwoPhtTime * (myIDSectionTwo - 1));
    timeStart = timeElapsed;

    } else if (checkSections == "sections" && myID <= (sectionOnePhtNum + sectionTwoPhtNum + sectionThreePhtNum)) {
    timeElapsed = sectionThreeStart + (sectionThreePhtTime * (myIDSectionThree - 1));
    timeStart = timeElapsed;

    } else if (checkSections == "sections" && myID <= (sectionOnePhtNum + sectionTwoPhtNum + sectionThreePhtNum + sectionFourPhtNum)) {
    timeElapsed = sectionFourStart + (sectionFourPhtTime * (myIDSectionFour - 1));
    timeStart = timeElapsed;

    } else if (checkSections == "sections" && myID <= (sectionOnePhtNum + sectionTwoPhtNum + sectionThreePhtNum + sectionFourPhtNum + sectionFivePhtNum)) {
    timeElapsed = sectionFiveStart + (sectionFivePhtTime * (myIDSectionFive - 1));
    timeStart = timeElapsed;

    } else {

    photoTime = thisComp.layer("Control Sliders").effect("photoTime")("Slider");
    timeElapsed = photoTime * (effect("myID")("Slider") - 1);
    timeStart = firstPhotoInPoint + timeElapsed;

    }

    valueAtTime(time - timeStart);

    Evan Jenkins
    Aspen Films Inc.
    aspenfilms.ca

    Evan Jenkins replied 6 years, 5 months ago 2 Members · 5 Replies
  • 5 Replies
  • Andrei Popa

    March 29, 2020 at 7:10 pm

    Create a text layer. Put all this code in the text value. And on all your layers put this as expression:

    eval(thisComp.layer("Text Layer").text.sourceText)

    I think you’ll need to change the name of the layer. You can make it invisible so it does not mess with your design.

    Andrei
    My Envato portfolio.

  • Evan Jenkins

    March 29, 2020 at 7:35 pm

    Thanks I copied and pasted my code into the text layer and then added the eval, but I got an error, it says Object of type TextProperty found where a Number, Array, or Property is needed

    Evan Jenkins
    Aspen Films Inc.
    aspenfilms.ca

  • Andrei Popa

    March 29, 2020 at 7:44 pm

    Maybe add .value at the end. Some of this small things changed with the expression engine.

    eval(thisComp.layer("Text Layer").text.sourceText.value)

    Andrei
    My Envato portfolio.

  • Evan Jenkins

    March 29, 2020 at 8:01 pm

    I think I’m making progress, now it says “Syntax Error”

    The .value seemed to work.

    Also I saw an old post on the forum where you can add “””Code Here””” (triple quotes) in the text layer to make sure you’re converting everything into a single character string.

    I’m still working at it trying to resolve the error.

    Evan Jenkins
    Aspen Films Inc.
    aspenfilms.ca

  • Evan Jenkins

    March 29, 2020 at 8:19 pm

    I gave up out of trying to get the code out of a text layer, there was something wrong with what I was doing but I couldn’t figure it out.

    I actually had more success with getting the code out of the comments of a layer marker, which also has the advantage of letting me scroll through a little easier to access the code.

    I got the idea from this video:

    https://www.youtube.com/watch?v=MX4dUF0J5x0

    eval(thisComp.layer("Set Global Variables").marker.key(1).comment);

    Evan Jenkins
    Aspen Films Inc.
    aspenfilms.ca

    Some contents or functionalities here are not available due to your cookie preferences!

    This happens because the functionality/content marked as “Google Youtube” uses cookies that you choosed to keep disabled. In order to view this content or use this functionality, please enable cookies: click here to open your cookie preferences.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy