Skip to main content
Participant
Oct 16, 2023 9:47 AM UTC
Answered

Extracting keyframe data

Hi, is there a way to extract a list of keyframe numbers (or better still, timestamps) for all keyframes on a given layer in an Animate file?  I want to be able to mark up specific points in the animation using blank keyframes on a dummy layer, then once the animation is finshed, extract the keyframe numbers or timestamps into a text file.  I'm a novice at coding, but from what I've seen in other discussions, it seems like it should be feasible.  Thanks!

This topic is closed to new replies. Start a new post to keep the conversation going.
Vladin M. Mitov
Vladin M. MitovCorrect answer
Inspiring
Oct 16, 2023 5:08 PM UTC

Hi,

The code below will produce an array that contains all keyframes of the currently selected layer, represented as timestamps (in string format), determined by the current document's FPS.

In the framesToHMS() function, you can skip the last parameter if you want to get the timestamps as objects.

 

(function(){

	function isKeyFrame( aLayer, frameNum ){
		return ( aLayer.frames[ frameNum ].startFrame === frameNum );
	}
	function framesToHMS( aframes, fps, asAstring ){
		
		var fps = fps || 25;
		
		// Calculate total seconds
		var totalSeconds = aframes / fps;

		// Calculate hours, minutes, and seconds
		var hours   = Math.floor( totalSeconds / 3600 );
		var minutes = Math.floor( ( totalSeconds % 3600 ) / 60 );
		var seconds = Math.floor( totalSeconds % 60 );

		// Calculate frames
		var frames = aframes % fps;
		
		// Timestamps as string ( Skip the "asAstring" parameter for JSON objects )
		return ( asAstring ) ?
			( hours + ":" + minutes + ":" + seconds + ":" + frames ) :
			{ hours:hours, minutes:minutes, seconds:seconds, frames:frames };
	}
	function keysToTimeStamps( aLayer, fps ){
		var out = [], i;
		for( i = 0; i < aLayer.frameCount; i++ ){
			if( isKeyFrame( aLayer, i ) ){
				out.push( framesToHMS( i, fps, true ) );
			}
		}
		return out;
	}

	// Example usage ( returns an array of H:M:S:F strings ):
	var doc = fl.getDocumentDOM();
	var tml = doc.getTimeline();
	var selectedLayer = tml.layers[ tml.currentLayer ];

	fl.trace( keysToTimeStamps( selectedLayer, doc.frameRate ) );

})();

 



- Vlad: UX and graphic design, Flash user since 1998Member of Flanimate Power Tools team - extensions for character animation

3 replies

Vladin M. Mitov
Vladin M. MitovCorrect answer
Inspiring
Oct 16, 2023 5:08 PM UTC

Hi,

The code below will produce an array that contains all keyframes of the currently selected layer, represented as timestamps (in string format), determined by the current document's FPS.

In the framesToHMS() function, you can skip the last parameter if you want to get the timestamps as objects.

 

(function(){

	function isKeyFrame( aLayer, frameNum ){
		return ( aLayer.frames[ frameNum ].startFrame === frameNum );
	}
	function framesToHMS( aframes, fps, asAstring ){
		
		var fps = fps || 25;
		
		// Calculate total seconds
		var totalSeconds = aframes / fps;

		// Calculate hours, minutes, and seconds
		var hours   = Math.floor( totalSeconds / 3600 );
		var minutes = Math.floor( ( totalSeconds % 3600 ) / 60 );
		var seconds = Math.floor( totalSeconds % 60 );

		// Calculate frames
		var frames = aframes % fps;
		
		// Timestamps as string ( Skip the "asAstring" parameter for JSON objects )
		return ( asAstring ) ?
			( hours + ":" + minutes + ":" + seconds + ":" + frames ) :
			{ hours:hours, minutes:minutes, seconds:seconds, frames:frames };
	}
	function keysToTimeStamps( aLayer, fps ){
		var out = [], i;
		for( i = 0; i < aLayer.frameCount; i++ ){
			if( isKeyFrame( aLayer, i ) ){
				out.push( framesToHMS( i, fps, true ) );
			}
		}
		return out;
	}

	// Example usage ( returns an array of H:M:S:F strings ):
	var doc = fl.getDocumentDOM();
	var tml = doc.getTimeline();
	var selectedLayer = tml.layers[ tml.currentLayer ];

	fl.trace( keysToTimeStamps( selectedLayer, doc.frameRate ) );

})();

 



Translate
- Vlad: UX and graphic design, Flash user since 1998Member of Flanimate Power Tools team - extensions for character animation
Participant
Oct 19, 2023 8:29 AM UTC

That's perfect - thanks very much!

Translate
kglad
Community Expert
Community Expert
Oct 16, 2023 12:23 PM UTC