Page 1 of 1

Delete Selected Keyframes via script

Posted: November 4th, 2017, 10:04 am
by Sebastian Moreno
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

Re: Delete Selected Keyframes via script

Posted: November 6th, 2017, 4:22 pm
by jordanwade33
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);
}

Re: Delete Selected Keyframes via script

Posted: November 9th, 2017, 6:45 am
by Sebastian Moreno
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.

Re: Delete Selected Keyframes via script

Posted: December 20th, 2017, 7:19 am
by Sebastian Moreno
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);   }
     }
}
}

Re: Delete Selected Keyframes via script

Posted: December 21st, 2017, 2:17 am
by beginUndoGroup
property is not defined in your snippet.

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

Xavier