Skip to main content
Participant
October 16, 2023
Answered

Extracting keyframe data

  • October 16, 2023
  • 2 replies
  • 465 views

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 has been closed for replies.
Correct answer Vladin M. Mitov

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 ) );

})();

 



2 replies

Vladin M. Mitov
Vladin M. MitovCorrect answer
Inspiring
October 16, 2023

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
Participant
October 19, 2023

That's perfect - thanks very much!

kglad
Community Expert
Community Expert
October 16, 2023