Search code examples
javascriptloopsadobeextendscriptafter-effects

For loop in Adobe ExtendScript


my for-loop in the "setEase" function won't increase "i"

function storeKeyframes(){
            var properties      = app.project.activeItem.selectedProperties;
            var activeProperty  = null;
            var keySelection    = null;
            var curKey          = null;
            var curKeyTime      = null;
            var curKeyIndex     = null;

            var theEase         = new KeyframeEase(0 , slider_1_slider.value);

            for (var i = 0; i < properties.length; i++){
                activeProperty = properties[i];
                setEase();
            }

            function setEase(){
                for (var i = 0; i < activeProperty.selectedKeys.length ; i++){
                    keySelection    = activeProperty.selectedKeys;
                    curKey          = keySelection[i];
                    curKeyTime      = activeProperty.keyTime(curKey);
                    curKeyIndex     = activeProperty.nearestKeyIndex(curKeyTime);

                    activeProperty.setInterpolationTypeAtKey(curKeyIndex, KeyframeInterpolationType.BEZIER, KeyframeInterpolationType.BEZIER);
                    activeProperty.setTemporalEaseAtKey(curKeyIndex,theEase, theEase);
                }
            }
        }

I just can't figure out why. Am I missing something?


Solution

  • I tried your code and indeed "i" is not increasing, but for me the reason was that there was an error.

    The main reason for error is that the 2nd and 3rd arguments to setTemporalEaseAtKey() should be arrays of KeyframeEase, not just KeyframeEase (see the scripting guide). Another reason is that activeProperty needs not be an actual Property, hence querying activeProperty.selectedKeys.length will throw an error.

    On a side note, what you call curKeyIndex is actually the same as curKey, so you dont need the nearestKeyIndex stuff. The following code works for me:

    function storeKeyframes(){
        var comp = app.project.activeItem;
        if (!comp || comp.typeName !== "Composition") return;
        var properties = comp.selectedProperties;
        var i, I=properties.length;
        var ease1 = new KeyframeEase(0,100);
    
        for (i=0; i<I; i++){
            if (properties[i] instanceof Property) setEase(properties[i], ease1);
            };
        };
    function setEase(property, ease1){
        var ease = property.propertyValueType===PropertyValueType.Two_D ? [ease1, ease1] : (property.propertyValueType===PropertyValueType.Three_D ? [ease1, ease1, ease1] : [ease1]);
        var keySelection = property.selectedKeys;
        var i, I=keySelection.length;
        for (i=0; i<I; i++){
            property.setInterpolationTypeAtKey(keySelection[i], KeyframeInterpolationType.BEZIER, KeyframeInterpolationType.BEZIER);
            property.setTemporalEaseAtKey(keySelection[i], ease, ease);
            };
        };
    storeKeyframes();