Page 1 of 1

layer Position to .txt (extract coordinates)

Posted: May 22nd, 2007, 10:38 am
by berniebernie
not tested on mac
extractcoordinates.jsx

This scripts allows you to export the current layer's positions through time to a .txt format (to export to a 3d app for instance)
It doesn't have a 'z' position nor does it allow you to select anchor points or rotation values - but I'll add it if needs be.

If no position keyframes are selected then all the position values during the comp length will be written to the text file


Image

Code: Select all

// script created by mlk: mlkdesign@gmail.com Jan 2007 
// 
// The script will write to a text file the x & y values of the layer position for every frame comprised in
// a selection of keyframes, or for the whole comp duration if no keyframes are selected

function timeToFrameNum(myTime){
	return Math.floor(myTime) * app.project.activeItem.frameRate + (myTime - Math.floor(myTime)) / (1/app.project.activeItem.frameRate);
}
function framesToTime(myTime){
	return myTime/app.project.activeItem.frameRate;
}
function timeToTimeCode(myTime){
	var framesN = myTime * app.project.activeItem.frameRate;
	fr = addZero(Math.round((myTime - Math.floor(myTime))/(1/app.project.activeItem.frameRate)));
	ho = addZero(Math.floor(myTime/3600));
	mi = addZero(Math.floor(myTime/60)-ho*60);
	se = addZero(Math.floor(myTime)-mi*60-ho*3600);
	return ho+":"+mi+":"+se+":"+fr;
}
function addZero(val){
	if(val<10){
		val = "0"+val;
	}
	return val;
}
var myDisp = app.project.timecodeDisplayType;
app.project.timecodeDisplayType = TimecodeDisplayType.TIMECODE;
var pText = "Choose an output format using:\n%f (framenumber),%i (index, starts at 0) %t (SMTPE timecode), %x (x value), %y (y value), %l (linebreak) and any other character. Default output looks like '16: 230;22'";

if(app.project.activeItem != "null" && app.project.activeItem != null && app.project.activeItem != 0){
	if(app.project.activeItem.selectedLayers.length != 0){
		if(app.preferences.getPrefAsLong("Main Pref Section","Pref_SCRIPTING_FILE_NETWORK_SECURITY")){
			curLayer = app.project.activeItem.selectedLayers[0];
			var textName = "AEcoordinates.txt";
			var myTextFile = filePutDialog("Select a location to save your .txt file", textName, "TEXT txt");
			if(myTextFile == null){
				alert("You must choose a place to save the file");
			}else{
				var formatString = prompt(pText,"%i: %x;%y%l");
				myTextFile.open("w","TEXT","????");
				myKeys = curLayer.property("position").selectedKeys;
				if(myKeys.length != 0){
					strTime = curLayer.property("position").keyTime(myKeys[0]);
					endTime = curLayer.property("position").keyTime(myKeys[myKeys.length-1]);
				}else{
					strTime = app.project.activeItem.workAreaStart;
					endTime = app.project.activeItem.workAreaStart + app.project.activeItem.workAreaDuration;
				}
				var startLoop = timeToFrameNum(strTime);
				var endLoop = timeToFrameNum(endTime);
				for(i=startLoop;i<=endLoop;i++){
					var curTime = framesToTime(i);
					out = formatString.replace('%x',curLayer.property("position").valueAtTime(curTime,true)[0]);
					out = out.replace('%y',curLayer.property("position").valueAtTime(curTime,true)[1]);
					out = out.replace('%f',i);timeToTimeCode
					out = out.replace('%t',timeToTimeCode(curTime));
					out = out.replace('%i',i-startLoop);
					out = out.replace('%l','\n');
					myTextFile.write(out);
					clearOutput();
					write(Math.round((i-startLoop)/(endLoop-startLoop)*100,0)+"% done...");
				}
			clearOutput();
			writeLn(endLoop-startLoop+" positions saved to file");
			writeLn("script written by mlk =)");
			myTextFile.close();
			}
		} else {
			alert ("This script requires the scripting security preference to be set.\n" +
			"Go to the \"General\" panel of your application preferences,\n" +
			"and make sure that \"Allow Scripts to Write Files and Access Network\" is checked.");
		}

	}else{
		alert("Select a layer with 'position' keyframes");
	}
}else{
	alert("Select a composition and a layer with 'position' keyframes");
}
app.project.timecodeDisplayType = myDisp;

Posted: May 30th, 2007, 11:42 am
by lya98
Very usefull! Thanks!

Posted: January 12th, 2008, 6:43 am
by DecWest
AWESOME! 8)

Posted: January 14th, 2008, 6:40 am
by Atomic
How is this useful?

What 3D app is going to read that information?

Posted: January 14th, 2008, 6:55 am
by DecWest
Atomic wrote:How is this useful?

What 3D app is going to read that information?
I need information like this for an 'in house' GUI tool. I am planning to create the animation (layer by layer) in AFX, export TRS information as a document and import this information to our toolkit (which is still under development)

MASSIVE time saver - especially when it comes to iterative reworking

Re: layer Position to .txt (extract coordinates)

Posted: February 11th, 2008, 4:18 am
by Darkmoon_UK
Tasty!!! :D Nice touch on the animating GIF too.

Re: layer Position to .txt (extract coordinates)

Posted: April 11th, 2008, 8:22 am
by berniebernie
By extremely popular request (errr I mean the one mail I got) I'll try to add a much friendlier user interface, I need to code a bit to relax.

matt

Re: layer Position to .txt (extract coordinates)

Posted: May 24th, 2010, 7:08 pm
by kilopop
Hi, is there any way this script could be adjusted to output rotation values as well?
Much appreciated.

Re: layer Position to .txt (extract coordinates)

Posted: February 1st, 2012, 1:56 am
by pecijackson
i'm using aftereffect since in college but they never tell me that there's a script so handy like this. thanks sir. :D