Skip to main content
Participating Frequently
Nov 9, 2023 7:57 AM UTC
Question

Key Frame Stretching Script only affect 1 layer

Good moring guys, I'm trying to script that would stretch out the distance between my selected keyframes proportionately, similar to alt click and dragging but faster.
Here's the feature I want for the script:
1. It would stretch out from the point of the last keyframe

2. It would reselect the keyframes, so that you could stretch it again if you needed
3. I want it to affect every keyframe I selected from different layers.  

I got 1 and 2 to work but can't find a solution for 3

var comp = app.project.activeItem;
if (comp != null && (comp instanceof CompItem)) {
    var layer = comp.selectedLayers[0];
    if (layer != null) {
        var keys = layer.selectedProperties;
        if (keys.length > 0) {
            app.beginUndoGroup("Stretch Keyframes");
            var stretchFactor = 0.8; // Change this to your desired stretch factor
            var keyData = [];
            for (var i = 0; i < keys.length; i++) {
                for (var j = 1; j <= keys[i].numKeys; j++) {
                    var keyTime = keys[i].keyTime(j);
                    var keyValue = keys[i].keyValue(j);
                    var inTemporalEase = keys[i].keyInTemporalEase(j);
                    var outTemporalEase = keys[i].keyOutTemporalEase(j);
                    var inInterpolationType = keys[i].keyInInterpolationType(j);
                    var outInterpolationType = keys[i].keyOutInterpolationType(j);
                    keyData.push({property: keys[i], keyTime: keyTime, keyValue: keyValue, inTemporalEase: inTemporalEase, outTemporalEase: outTemporalEase, inInterpolationType: inInterpolationType, outInterpolationType: outInterpolationType});
                }
                while (keys[i].numKeys > 0) {
                    keys[i].removeKey(1);
                }
            }
            for (var i = 0; i < keyData.length; i++) {
                var lastKeyTime = keyData[keyData.length - 1].keyTime;
                var newKeyTime = lastKeyTime + (keyData[i].keyTime - lastKeyTime) * stretchFactor;
                var newKeyIndex = keyData[i].property.addKey(newKeyTime);
                keyData[i].property.setValueAtTime(newKeyTime, keyData[i].keyValue);
                keyData[i].property.setTemporalEaseAtKey(newKeyIndex, keyData[i].inTemporalEase, keyData[i].outTemporalEase);
                keyData[i].property.setInterpolationTypeAtKey(newKeyIndex, keyData[i].inInterpolationType, keyData[i].outInterpolationType);
                keyData[i].property.setSelectedAtKey(newKeyIndex, true);
            }
            app.endUndoGroup();
        } else {
            alert("No properties are selected.");
        }
    } else {
        alert("No layer is selected.");
    }
} else {
    alert("No composition is selected.");
}

Is there's a way for it to loop around all the layers selected?
Thank you! 

This topic is closed to new replies. Start a new post to keep the conversation going.

3 replies

Meng Zhiqun
Inspiring
Nov 12, 2023 1:34 PM UTC

You don't need to find selected properties based on selected layers. There's a syntax for selected properties so you can jump straight in.

var proj = app.project;
var projComp = proj.activeItem;
var selProp = projComp.selectedProperties;

app.beginUndoGroup("Stretch Keyframes");
    var stretchFactor = 0.8; // Change this to your desired stretch factor
    var keyData = [];
    for (var i = 0; i < selProp.length; i++) {
        for (var j = 1; j <= selProp[i].numKeys; j++) {
            var keyTime = selProp[i].keyTime(j);
            var keyValue = selProp[i].keyValue(j);
            var inTemporalEase = selProp[i].keyInTemporalEase(j);
            var outTemporalEase = selProp[i].keyOutTemporalEase(j);
            var inInterpolationType = selProp[i].keyInInterpolationType(j);
            var outInterpolationType = selProp[i].keyOutInterpolationType(j);
            keyData.push({property: selProp[i], keyTime: keyTime, keyValue: keyValue, inTemporalEase: inTemporalEase, outTemporalEase: outTemporalEase, inInterpolationType: inInterpolationType, outInterpolationType: outInterpolationType});
        }
        while (selProp[i].numKeys > 0) {
            selProp[i].removeKey(1);
        }
    }
    for (var i = 0; i < keyData.length; i++) {
        var lastKeyTime = keyData[keyData.length - 1].keyTime;
        var newKeyTime = lastKeyTime + (keyData[i].keyTime - lastKeyTime) * stretchFactor;
        var newKeyIndex = keyData[i].property.addKey(newKeyTime);
        keyData[i].property.setValueAtTime(newKeyTime, keyData[i].keyValue);
        keyData[i].property.setTemporalEaseAtKey(newKeyIndex, keyData[i].inTemporalEase, keyData[i].outTemporalEase);
        keyData[i].property.setInterpolationTypeAtKey(newKeyIndex, keyData[i].inInterpolationType, keyData[i].outInterpolationType);
        keyData[i].property.setSelectedAtKey(newKeyIndex, true);
    }
app.endUndoGroup();
Translate
Mathias Moehl
Community Expert
Community Expert
Nov 9, 2023 10:11 AM UTC

Currently you process the first selected layer:

var layer = comp.selectedLayers[0];

Have you tried to loop over all layers in the comp.selectedLayers array instead?

 

Translate
Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Participating Frequently
Nov 9, 2023 1:19 PM UTC

Good suggestion, but once the script has run through the first layer I think it releases the keyframes from the layers bellow and tell me "no properties are selected". Is there a way for it to go through every layer before losing the selection?

Translate