Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Key Frame Stretching Script only affect 1 layer

Community Beginner ,
Nov 08, 2023 Nov 08, 2023

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! 

TOPICS
How to , Scripting
253
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 09, 2023 Nov 09, 2023

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?

 

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 09, 2023 Nov 09, 2023

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 12, 2023 Nov 12, 2023
LATEST

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines