Delete Selected Keyframes via script

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
Sebastian Moreno
Posts: 5
Joined: November 4th, 2017, 9:36 am
Location: Germany

Hi everyone, 
I am a still beginner at Extendscript. How could I store selected keyframes so I can delete them afterwards. I have tried arrays but I am missing something. This is the simplified version:

var keySelection = property.selectedKeys; 

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

I am writing a script that will move keys to a specific time in milliseconds,secs or frames.. the button is aware of the comp frame-rate and you would be able to move to the selected keys to a typed specific time

Thanks,
Sebastian
jordanwade33
Posts: 16
Joined: December 8th, 2014, 11:11 pm

I think what you were going for is this:

Code: Select all

var keySelection = property.selectedKeys; 
for (i = 0; i < keySelection.length; i++) {
    property.removeKey(keySelection[i]);
}
The problem with that is that as you're removing keyframes, the current indexes of the keyframes reduce by one, but your original keySelection indexes stay the same. The way you can get around that is to subtract the number of times you've already removed a keyframe:

Code: Select all

var keySelection = property.selectedKeys; 
for (i = 0; i < keySelection.length; i++) {
    property.removeKey(keySelection[i]-i);
}
Sebastian Moreno
Posts: 5
Joined: November 4th, 2017, 9:36 am
Location: Germany

Hi Jordan
That did the trick.So good to know this, no idea that the indexes stay the same, because apparently  ae deselects after deleting a keyframe. 
Thanks a lot.
Sebastian Moreno
Posts: 5
Joined: November 4th, 2017, 9:36 am
Location: Germany

I am struggling now with the layers. So it will delete all keyframes from all selected properties from selected layers.
AE deletes the selected keyframes form the first selected property from the first selected Layer--good until then :D. 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 :shock:.

Code: Select all

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);   }
     }
}
}
beginUndoGroup
Posts: 81
Joined: November 27th, 2012, 6:41 am

property is not defined in your snippet.

(You need define : var property = property[k])

Xavier
Post Reply