Skip to main content
Participant
November 30, 2022
Question

After Effects Extend Script for Expressions Question

  • November 30, 2022
  • 1 reply
  • 488 views

I am making an expression injector that has a drop down menu for the expressions and transform properties. The UI works but the button click doesn't work. What am I doing wrong?

 

// AEEXPRESSIONPANEL
// =================
var expressionsArray = [];
var expressionObj = {
       "Auto Fade": "transition = 20;if (marker.numKeys<2){tSecs = transition / ( 1 / thisComp.frameDuration);linear(time, inPoint, inPoint + tSecs, 0, 100) - linear(time, outPoint - tSecs, outPoint, 0, 100)}else{linear(time, inPoint, marker.key(1).time, 0, 100) - linear(time, marker.key(2).time, outPoint, 0, 100)};",
   
        "Inertial Bounce": "amp = .1; freq = 2.0; decay = 2.0; n = 0; time_max = 4; if (numKeys > 0){n = nearestKey(time).index;if (key(n).time > time){n--;}}if (n == 0){ t = 0;}else{t = time - key(n).time;}if (n > 0 && t < time_max){v = velocityAtTime(key(n).time - thisComp.frameDuration/10);value + v*amp*Math.sin(freq*t*2*Math.PI)/Math.exp(decay*t);}else{value}",
   
        "Random Blinking": "blinkRate=.1*gaussRandom(2)n=Math.sin(time*blinkRate);if(n<0) 0;else 100;",
   
        "Sine Wave": "Math.sin(time*2*Math.PI*1/3)*500;",
    };
   
    var AEEXPRESSIONPANEL = new Window("palette", undefined, undefined, {maximizeButton: true, minimizeButton: true});
        AEEXPRESSIONPANEL.text = "AE EXPRESSIONS";
        AEEXPRESSIONPANEL.preferredSize.width = 200;
        AEEXPRESSIONPANEL.preferredSize.height = 75;
        AEEXPRESSIONPANEL.orientation = "column";
        AEEXPRESSIONPANEL.alignChildren = ["center","center"];
        AEEXPRESSIONPANEL.spacing = 10;
        AEEXPRESSIONPANEL.margins = 16;
   
    // GROUP1
    // ======
    var group1 = AEEXPRESSIONPANEL.add("group", undefined, {name: "group1"});
        group1.orientation = "row";
        group1.alignChildren = ["left","center"];
        group1.spacing = 10;
        group1.margins = 0;
   
    var dropdown1_array = ["Position","-","Scale","-","Rotation","-","Opacity"];
    var dropdown1 = group1.add("dropdownlist", undefined, undefined, {name: "dropdown1", items: dropdown1_array});
        dropdown1.selection = 0;
        dropdown1.alignment = ["left","center"];
   
    var dropdown2_array = group1.add("dropdownlist", undefined, expressionsArray);
        dropdown2_array.selection = 0;
        dropdown2_array.size = [220, 25];
    var displayText = group1.add("statictext", undefined, expressionObj[expressionsArray[0]], {multiline: true});
        displayText.size = [300, 100];
   
    var applyButton = group1.add("button", undefined, undefined, {name: "button1"});
        applyButton.text = "Apply";
   
    AEEXPRESSIONPANEL.show();

    dropdown2_array.onChange = function() {
        displayText.text = expressionObj[expressionsArray[dropdown2_array.selection.index]];
        }

var newName;
   
    applyButton.onClick = function() {
        if(app.project.activeItem.selectedLayers.length < 1) {
            alert("Please select a layer to apply expressions to", "");
            return false;
            }
        else {
            app.beginUndoGroup("Expression Injection");
                injectExpression(app.project.activeItem.selectedLayers);
                app.endUndoGroup();
            }
            for(var q = 0; q < app.project.activeItem.selectedLayers[0].selectedProperties.length; q++) {
                app.project.activeItem.selectedLayers[0].selectedProperties[q].expression = expressionObj[dropdown2_array.selection.index];
                }
    }

function injectExpression(layerArray) {
    for(var i = 0; i < layerArray.length; i++) {
        layerArray[i].property(getPropertyName1()).expression = expressionList;
        }
    alert("Expressions applied successfully!", "");
    }

   
    function expressionList() {
        switch(dropdown2.selection.index) {
            case 0:
                return "Auto Fade";
            break;
            case 1:
                return "Inertial Bounce";
            break;
            case 2:
                return "Random Blinking";
            break;
            case 3:
                return "Sine Wave";
            break;
        }
    }
   
   
    function getPropertyName1() {
        switch(dropdown1.selection.index) {
            case 0:
                return "Position";
            break;
            case 1:
                return "Scale";
            break;
            case 2:
                return "Rotation";
            break;
           
            case 3:
                return "Opacity";
            break;
            }
        }
   
   
This topic has been closed for replies.

1 reply

Dan Ebberts
Community Expert
Community Expert
November 30, 2022

I tried your code. It appears that the second dropdown is not populating correctly. The button Apply appears to respond, and tries to apply an expression to the Position property, but the expression appears as "Function". Also, when I switch the first dropdown to Scale, it applies the Function expression to Rotation, so your code may not be accounting for the "-" spacers in the dropdown. So I think there are a number of issues to clean up, but it looks like you're close.

Participant
November 30, 2022

Thanks for taking the time to look at this Dan 🙂

I'm pretty new to scripting so I feel a bit lost here. Are there any resources you could recommend to help put me on the right path? I'm basically doing guesswork at the moment. I feel so close to getting this to work!

My main struggle is understanding how to "call" an expression from an array. Is everything set up correctly here to do that? Can you offer any potential solutions to my code errors?

Many thanks!