Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Delete Selected Keyframes via script

  • Delete Selected Keyframes via script

    Posted by Sebastian Moreno on December 20, 2017 at 6:31 pm

    Hi all
    I am writing a script to remove keyframes from selected properties and layers.

    with this code (below) AE deletes the selected keyframes form the first selected property from the first selected Layer good until then :D.

    var keySelection = property.selectedKeys;
    for (i = 0; i < keySelection.length; i++) {
    property.removeKey(keySelection[i]-i);}

    As i said, it deletes the selected keys from the first property after that, looks like the selection goes to hell and is like nothing was selected in the fist place, so It can’t move to the next selected property to continue deleting frames.

    here is the complete code I have:

    var layers = comp.selectedLayers;
    var properties = comp.selectedProperties;

    for (var j = 0; j < layers.length; j++) {
    for (var k = 0; k < properties.length; k++) {

    if (properties[k] instanceof Property){

    var keySelection = property.selectedKeys;

    for (var i = 0; i < keySelection.length; i++) {
    property.removeKey(keySelection[i]-i); }
    }
    }
    }

    any help, suggestion or advice is welcome

    Sebastian Moreno replied 8 years, 3 months ago 3 Members · 7 Replies
  • 7 Replies
  • Walter Soyka

    December 20, 2017 at 7:10 pm

    When deleting items from an array, start at the end and work backwards to the beginning; otherwise, the indices change as you go!

    Walter Soyka
    Designer & Mad Scientist at Keen Live [link]
    Motion Graphics, Widescreen Events, Presentation Design, and Consulting
    @keenlive   |   RenderBreak [blog]   |   Profile [LinkedIn]

  • Mark Whitney

    December 20, 2017 at 10:19 pm

    Just so you aren’t re-inventing the wheel, you might want to check out the suite of free scripts at the Redefinery, this one in particular:

    // rd_RemoveKeys.jsx
    // Copyright (c) 2006-2013 redefinery (Jeffrey R. Almasol). All rights reserved.
    // check it: https://www.redefinery.com
    //
    // Name: rd_RemoveKeys
    // Version: 3.0
    //
    // Description:
    // This script displays a palette with controls for removing keyframes from
    // the selected layers.

    Details of the usage on some of these:

    https://www.youtube.com/watch?v=DFHtmmuEpHA&t=52s

    Some contents or functionalities here are not available due to your cookie preferences!

    This happens because the functionality/content marked as “Google Youtube” uses cookies that you choosed to keep disabled. In order to view this content or use this functionality, please enable cookies: click here to open your cookie preferences.

  • Sebastian Moreno

    December 21, 2017 at 1:54 pm

    Thank you Mark
    I checked the script, I love how he approaches the issue. But there is no selected keys option. That is why I am doing it.

    I will try what Walter said, hopefully it will work for all selected keyframes no matter how many properties and layers are selected.

    Thanks!!

  • Mark Whitney

    December 21, 2017 at 6:32 pm

    Ah dang. Ya, the rd_ script works only on the selected Layers but no option to select just a particular property(s).

    Sounds like ya got a very useful script in the works.

  • Sebastian Moreno

    February 1, 2018 at 11:04 am

    Hi Walter
    I am stuck.

    I tried looping backwards for everything, one by one (all first, or comp and the property and vice-versa) nothing seemed to work. Just forwards and as you said the index changes.
    To see If i am doing something wrong I tried removing layers something super basic:

    for ( var i = selectedLayers.length ; i != 0 ; i–) {
    selectedLayers[i].remove();
    }

    and also

    for ( var i = selectedLayers.length ; i > 0 ; i–) {
    selectedLayers[i].remove();
    }

    This does not work. Is after having a bug ? IT WORKS when operand i =1 and of course it wont remove one of the selected.

    any ideas why is this happening when looping backwards.

  • Walter Soyka

    February 1, 2018 at 11:23 am

    selectedLayers is an array with an origin of zero. If selectedLayers.length = 3, then the only valid indices are selectedLayers[0], selectedLayers[1] and selectedLayers[2]. That means the starting value and the test in your loops are both off by one.

    Try this:

    app.beginUndoGroup("Remove selected layers");

    selectedLayers = app.project.activeItem.selectedLayers;
    for (var i = selectedLayers.length - 1 ; i >= 0 ; i--) {
    selectedLayers[i].remove();
    }

    app.endUndoGroup();

    Walter Soyka
    Designer & Mad Scientist at Keen Live [link]
    Motion Graphics, Widescreen Events, Presentation Design, and Consulting
    @keenlive   |   RenderBreak [blog]   |   Profile [LinkedIn]

  • Sebastian Moreno

    February 5, 2018 at 8:41 pm

    Hi Walter
    Thank you so much. That worked wonderfully, Indexes are now doing good. Still after I delete the key-frames of a property the selection is lost.

    for (var x = selectedLayers.length – 1; x >= 0; x–) {

    var currentLayer = selectedLayers[x];
    var SelectedProp = currentLayer.selectedProperties;

    for (var i = SelectedProp.length – 1; i >= 0; i–) {

    var currentProp = SelectedProp[i];
    var keySelection = currentProp.selectedKeys;

    for (var k = keySelection.length – 1; k >= 0; k–) {

    currentProp.removeKey(keySelection[k]);
    }
    }
    }

    Any ideas what may be the issue

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