utility kuper "Kuper ASCII Import"
(
	button fileload "Load Kuper ASCII File" width:140 --enabled:false
	
	on fileload pressed do
	(
		local ValArray = #()
		local strStore = "" as string
		local InBuf = "" as string
		motionPath = (getOpenFileName "Choose ASCII Data File") as string
		files = openFile motionPath
		
		cam = freecamera name: "KuperCam"
		--KuperCam can not be a target camera
	
		t = 0

		InBuf = (readline files) as string	--throw away the "Axes = " line
		
		--We make the big assumption that the motion control operator saved
		--   the proper set of six virtual axes:
		--   VTrack, VEW, VNS, VPan, VTilt, and VRoll
		while (eof files == false) do
		(
			InBuf = (readline files) as string
			x = 1
			while (InBuf[x] == " ") do (x = x + 1)
			while (x < InBuf.count) do
			(
				o = InBuf[x] as string
				if o == " " do
				(
					append ValArray strStore
					strStore = ""
					o = ""
					while (InBuf[x + 1] == " ") do (x = x + 1)
				)
				x = x + 1
				strStore += o as string
			)
			append ValArray strStore
			--local t = (ValArray[1] as integer) as time
			local track = ValArray[1] as float
			local ew = ValArray[2] as float
			local ns = ValArray[3] as float
			local pan = ValArray[4] as float
			local tilt = ValArray[5] as float
			local roll = ValArray[6] as float
			
			--format "[%: % % % % % %]\n" t track ew ns pan tilt roll
			
			--eulerangles tilt roll pan
			--cam home position is: eulerangles 90 0 0; cam.pos = [0,0,0]
			
			animate on, at time (t) (				
				--first do tilt and pan
				cam.rotation = eulerangles (tilt + 90) 0 -pan
				--switch to local coords to make camera roll nest correctly
				cam coordsys local
				cam.rotation = quat -roll z_axis
				--add in x,y,z
				cam coordsys world
				cam.pos = [ew,-track,ns]
				t = t + 1
			)
			ValArray = #()
			strStore = "" as string
		)
		close files
		--Subtract one from t because if got an extra bump in the above loop.
		--Subtract yet another one because the Kuper file has been interpolated
		--  between frames to center the image of the photographed image blur.
		--The computer graphic file will always be one frame less than the original
		--  motion control file.
		if (t > 1) then t -= 2
		animationRange = (interval 0f t)
	)
)
		