-
Help with a script to modify keyframes
I was playing with Chat GPT earlier and got a script that grabs the value of the last keyframe, assigns it to the previous keyframe and then deletes the last. It works great when you have to align things early on your timeline but the camera hasn’t arrived to the final POV yet, with this you can adjust the final position of your object and then just push the keyframe back to the current time.
The script works but it has two problems:
• It opens two separate panels, an empty one and one with only the button in it
• It only works with one layer at a time, in order to be truly useful it needs to be able to do that with multiple layers selected at once.
I already tried tweaking it myself (only managed to break it) and asking the ai repeatedly in different ways to fix these issues, mostly the multiple layers one to no avail.
I’m wondering if anyone here could maybe take a look and fix it? Here’s the code.
Thanks in advance.
(function () {
// Create a new window docked to the main After Effects interface
var moveKeyframeWindow = new Window('palette', 'Move Last Keyframe', undefined);
// Add a button to the window
var moveKeyframeButton = moveKeyframeWindow.add('button', undefined, 'Jump');
// When the button is clicked
moveKeyframeButton.onClick = function () {
// Get the current composition and selected properties
var comp = app.project.activeItem;
var selectedProps = comp.selectedProperties;
// If no property is selected, show an alert and return
if (selectedProps.length === 0) {
alert("Please select at least one property.");
return;
}
// Loop through each selected property
for (var i = 0; i < selectedProps.length; i++) {
var selectedProperty = selectedProps[i];
// Get the last keyframe index and the keyframe value
var keyframeIndex = selectedProperty.selectedKeys[selectedProperty.selectedKeys.length - 1];
var keyframeValue = selectedProperty.keyValue(keyframeIndex);
// If there's only one keyframe, show an alert and continue to the next property
if (selectedProperty.selectedKeys.length < 2) {
alert("There are not enough keyframes to move for property '" + selectedProperty.name + "'.");
continue;
}
// Get the previous keyframe index and value
var previousKeyframeIndex = selectedProperty.selectedKeys[selectedProperty.selectedKeys.length - 2];
var previousKeyframeValue = selectedProperty.keyValue(previousKeyframeIndex);
// Move the last keyframe to the position of the previous keyframe
selectedProperty.removeKey(keyframeIndex);
selectedProperty.setValueAtKey(previousKeyframeIndex, keyframeValue);
}
}
// Show the window
moveKeyframeWindow.show();
})();
Sorry, there were no replies found.