Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Help with a script to modify keyframes

  • Help with a script to modify keyframes

    Posted by David Cabestany on March 15, 2023 at 10:28 pm

    I was playing with Chat GPT earlier and got a script that grabs the value of the last keyframe, assigns it to the previous keyframe and then deletes the last. It works great when you have to align things early on your timeline but the camera hasn’t arrived to the final POV yet, with this you can adjust the final position of your object and then just push the keyframe back to the current time.

    The script works but it has two problems:

    • It opens two separate panels, an empty one and one with only the button in it

    • It only works with one layer at a time, in order to be truly useful it needs to be able to do that with multiple layers selected at once.

    I already tried tweaking it myself (only managed to break it) and asking the ai repeatedly in different ways to fix these issues, mostly the multiple layers one to no avail.

    I’m wondering if anyone here could maybe take a look and fix it? Here’s the code.

    Thanks in advance.

    (function () {
    // Create a new window docked to the main After Effects interface
    var moveKeyframeWindow = new Window('palette', 'Move Last Keyframe', undefined);
    // Add a button to the window
    var moveKeyframeButton = moveKeyframeWindow.add('button', undefined, 'Jump');
    // When the button is clicked
    moveKeyframeButton.onClick = function () {
    // Get the current composition and selected properties
    var comp = app.project.activeItem;
    var selectedProps = comp.selectedProperties;
    // If no property is selected, show an alert and return
    if (selectedProps.length === 0) {
    alert("Please select at least one property.");
    return;
    }
    // Loop through each selected property
    for (var i = 0; i < selectedProps.length; i++) {
    var selectedProperty = selectedProps[i];
    // Get the last keyframe index and the keyframe value
    var keyframeIndex = selectedProperty.selectedKeys[selectedProperty.selectedKeys.length - 1];
    var keyframeValue = selectedProperty.keyValue(keyframeIndex);
    // If there's only one keyframe, show an alert and continue to the next property
    if (selectedProperty.selectedKeys.length < 2) {
    alert("There are not enough keyframes to move for property '" + selectedProperty.name + "'.");
    continue;
    }
    // Get the previous keyframe index and value
    var previousKeyframeIndex = selectedProperty.selectedKeys[selectedProperty.selectedKeys.length - 2];
    var previousKeyframeValue = selectedProperty.keyValue(previousKeyframeIndex);
    // Move the last keyframe to the position of the previous keyframe
    selectedProperty.removeKey(keyframeIndex);
    selectedProperty.setValueAtKey(previousKeyframeIndex, keyframeValue);
    }
    }
    // Show the window
    moveKeyframeWindow.show();
    })();
    David Cabestany replied 11 months, 2 weeks ago 1 Member · 0 Replies
  • 0 Replies

Sorry, there were no replies found.

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