Page 1 of 1

Delete Duplicate Keyframes

Posted: January 11th, 2013, 6:17 pm
by Iaenic
Hello! I'm wondering if you can help me write up a very quick script. It's similar to the aescripts jdSmartBaker, but much simpler.

I need a script that looks at the first keyframe and then continually deletes each keyframe after it that has the same value, until it hits a keyframe with a new value. Then, it repeats the same thing from that key frame. This would allow me to bake a stair-stepped animation, and then delete the unnecessary in-between keyframes to have it effectively "tween" the animation between the steps.

For example, an animation with 15 keyframes like this:
1, 1, 1, 1, 1, 1.5, 1.5, 1.5, 1.5, 2, 2, 2, 2, 2, 3,

Would become an animation with just 4 keyframes like this:
1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.625, 1.75, 1.875, 2, 2.2, 2.4, 2.6, 2.8, 3

Any help is much appreciated!

Re: Delete Duplicate Keyframes

Posted: January 15th, 2013, 4:23 pm
by Paul Tuersley
Try this:

Code: Select all

var activeItem = app.project.activeItem;

if (activeItem != null && activeItem instanceof CompItem) {
	var selectedProps = activeItem.selectedProperties;
	var y;
	
	app.beginUndoGroup("Remove Duplicate Keys");
	for (var x = 0; x < selectedProps.length; x++) {

		if (selectedProps[x].numKeys > 1) {		
			y = 1;
			while (y < selectedProps[x].numKeys) {
				if (selectedProps[x].keyValue(y).toString() == selectedProps[x].keyValue(y+1).toString()) {
					selectedProps[x].removeKey(y+1);
				} else {		
					y ++;
				}
			}
		}
	}
	app.endUndoGroup();
}

Re: Delete Duplicate Keyframes

Posted: January 15th, 2013, 5:33 pm
by Iaenic
Absolutely perfect!

This solves my immediate problem, and I should also be able to learn a lot from how you wrote it. Thank you!