Copy link to clipboard
Copied
Let's say I selected a bunch of keyframes from multiple layers.
How can I deselect random key-frames and keep the rest selected? Via scripting of course.
Thank you!
Oops, I forgot to actually access the selectedKeys to get the right key indices. It was only working as expected because I'd selected all the keys when I tested it. Keyframe indices start from an index of 1 while arrays (as selectedProperties is) start from 0 so that's why I also had to change the for line.
var myProp = app.project.activeItem.selectedProperties[0];
for (var x = myProp.selectedKeys.length-1; x >= 0 ; x--) {
myProp.setSelectedAtKey(myProp.selectedKeys[x], Math.round(Math.random
...
Copy link to clipboard
Copied
If you check here you'll see there is a "Property.selectedKeys" which returns an array of indices of selected keyframes for that property:
https://ae-scripting.docsforadobe.dev/properties/property/
Property.selectedKeys
app.project.item(index).layer(index).propertySpec.selectedKeys
Description
The indices of all the selected keyframes in the named property. If no keyframes are selected, or if the property has no keyframes, returns an empty array.
Type
Array of integers; read-only.
You can also check if a specific keyframe is selected but you probably won't need that.
Property.keySelected()
app.project.item(index).layer(index).propertySpec.keySelected(keyIndex)
Description
Returns true if the specified keyframe is selected.
Parameters
keyIndex | The index for the keyframe. An integer in the range [1..numKeys], as returned by the addKey or nearestKeyIndex. |
Returns
Boolean.
But you will need Property.selectedAtKey() to unselect the keys you randomly select:
Property.setSelectedAtKey()
app.project.item(index).layer(index).propertySpec.setSelectedAtKey(keyIndex, onOff)
Description
Selects or deselects the specified keyframe.
Parameters
keyIndex | The index for the keyframe. An integer in the range [1..numKeys], as returned by the addKey or nearestKeyIndex. |
onOff | True to select the keyframe, false to deselect it. |
Returns
Nothing.
Here's some random javascript stuff:
https://www.w3schools.com/js/js_random.asp
And that's exactly the research I did to come up with this:
var myProp = app.project.activeItem.selectedProperties[0];
for (var x = myProp.selectedKeys.length; x >0 ; x--) {
myProp.setSelectedAtKey(x, Math.round(Math.random()));
}
I took advantage of the fact 0 and 1 are somewhat interchangeable for true and false in javascript and used a rounded random number (which defaults as values between 0 and 1) to loop through the selectedKeys and use that to set them on (well they're already on anyway) or off.
I looped through backwards because selectedKeys will gradually get shorter as you deselect them which screws things right up if you're using it to loop through forwards!
You could always skew it in one direction or other through various means of manipulating the round/random combo. One obvious way would be to multiply the result of Math.round by say 1.4 or 0.7 so the range across 0 > 0.5 > 1 is skewed when it's then rounded (while still resulting in a 0 or 1).
Copy link to clipboard
Copied
Oops, I forgot to actually access the selectedKeys to get the right key indices. It was only working as expected because I'd selected all the keys when I tested it. Keyframe indices start from an index of 1 while arrays (as selectedProperties is) start from 0 so that's why I also had to change the for line.
var myProp = app.project.activeItem.selectedProperties[0];
for (var x = myProp.selectedKeys.length-1; x >= 0 ; x--) {
myProp.setSelectedAtKey(myProp.selectedKeys[x], Math.round(Math.random()));
}
Copy link to clipboard
Copied
Thank you very much!