Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Error on script, function selectedProperty.key is undefined

  • Error on script, function selectedProperty.key is undefined

    Posted by David Cabestany on February 10, 2023 at 11:10 pm

    Wondering if anyone can help me understand what is wrong with this script?

    var activeComp = app.project.activeItem;
    if (activeComp == null || !(activeComp instanceof CompItem)) {
    alert("Please select a composition first.");
    } else {
    var selectedLayer = activeComp.selectedLayers[0];
    if (selectedLayer == null) {
    alert("Please select a layer first.");
    } else {
    var selectedProperty = selectedLayer.selectedProperties[0];
    if (selectedProperty == null) {
    alert("Please select a property first.");
    } else if (selectedProperty.numKeys < 2) {
    alert("Property must have at least 2 keyframes.");
    } else {
    var lastKeyframeIndex = selectedProperty.numKeys.lastIndex;
    var secondToLastKeyframeIndex = selectedProperty.numKeys - 1;
    // Store the last keyframe value and temporal ease
    var lastKeyframe = selectedProperty.key(lastKeyframeIndex);
    var lastValue = lastKeyframe.value;
    var lastTemporalEase = lastKeyframe.temporalEase;
    // Set the second-to-last keyframe value and temporal ease to the last keyframe value and temporal ease
    var secondToLastKeyframe = selectedProperty.key(secondToLastKeyframeIndex);
    secondToLastKeyframe.setValue(lastValue);
    secondToLastKeyframe.setTemporalEaseAtKey(1, lastTemporalEase);
    // Remove the last keyframe
    selectedProperty.removeKey(lastKeyframeIndex);
    }
    }
    }

    when I run it I get the following error:
    Unable to execute script at line 20. Function selectedProperty.key is undefined.

    Thanks in advance.

    David Cabestany replied 1 year, 12 months ago 2 Members · 2 Replies
  • 2 Replies
  • Andrei Popa

    February 11, 2023 at 3:46 pm

    I think you want to give last value and easing to previous key and delete the last key. I made some changes, but did not test them.

    var activeComp = app.project.activeItem;
    if (activeComp == null || !(activeComp instanceof CompItem)) {
    alert("Please select a composition first.");
    } else {
    var selectedLayer = activeComp.selectedLayers[0];
    if (selectedLayer == null) {
    alert("Please select a layer first.");
    } else {
    var selectedProperty = selectedLayer.selectedProperties[0];
    if (selectedProperty == null) {
    alert("Please select a property first.");
    } else if (selectedProperty.numKeys < 2) {
    alert("Property must have at least 2 keyframes.");
    } else {
    var lastKeyframeIndex = selectedProperty.numKeys;
    var secondToLastKeyframeIndex = selectedProperty.numKeys - 1;
    // Store the last keyframe value and temporal ease
    var lastValue = selectedProperty.keyValue(lastKeyframeIndex);
    var lastInEase = selectedProperty.keyInTemporalEase(lastKeyframeIndex);
    var lastOutEase = selectedProperty.keyOutTemporalEase(lastKeyframeIndex);
    //set last value to previous key
    selectedProperty.setValueAtKey(secondToLastKeyframeIndex, lastValue);
    //set last ease to previous key
    secondToLastKeyframe.setTemporalEaseAtKey(secondToLastKeyframeIndex, lastInEase, lastOutEase);
    // Remove the last keyframe
    selectedProperty.removeKey(lastKeyframeIndex);
    }
    }
    }

  • David Cabestany

    February 13, 2023 at 3:14 pm

    Hi Andrei, thanks for looking into this.

    I got a similar error but on a different property:

    secondToLastKeyframe is undefined

    It’s happening at line 24. It’s very confusing because I see that the variable is clearly defined above, on line 16.

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