macroScript KuperCamIn category:"Kuper Camera"
(
-----------------------------------------
--KuperCameraImport 
--Version 0.25
--Started: 2/11/2000
--Edited : 9/13/2000
--by Borislav Petrov
--http://gfxcentral.com/bobo/
-----------------------------------------
--Rewritten exclusively for Mitch Gates
-----------------------------------------

global kuper_float
rollout kuper_roll "Kuper Camera Import" 
(
	button import_camera "IMPORT KUPER CAMERA" width:160 height:30 align:#center
	progressbar progress color:(color 0 0 160) height:10 width:160 align:#center
	
	group "Camera Lens"
	(
	spinner camera_lens "Camera Lens (mm):" range:[9.857,100000.0,35.0] fieldwidth:50
	button camera15 "15mm" width:48 align:#left across:3
	button camera20 "20mm" width:48 align:#center 
	button camera24 "24mm" width:48 align:#right 
	button camera28 "28mm" width:48 align:#left across:3
	button camera35 "35mm" width:48 align:#center 
	button camera50 "50mm" width:48 align:#right 
	button camera85 "85mm" width:48 align:#left across:3
	button camera135 "135mm" width:48 align:#center 
	button camera200 "200mm" width:48 align:#right 
	)
	
	on camera15 pressed do camera_lens.value = 15
	on camera20 pressed do camera_lens.value = 20
	on camera24 pressed do camera_lens.value = 24
	on camera28 pressed do camera_lens.value = 28
	on camera35 pressed do camera_lens.value = 35
	on camera50 pressed do camera_lens.value = 50
	on camera85 pressed do camera_lens.value = 85
	on camera135 pressed do camera_lens.value = 135
	on camera200 pressed do camera_lens.value = 200


	group "Shutter Timing"
	(
	dropdownlist blur_mode items:#("Center On Blur","Center on Close")
	spinner offset_frames "Frame Offset" range:[0,10000,0] type:#integer fieldwidth:45
	spinner offset_ticks "Ticks Offset" range:[0,4800,0] type:#integer fieldwidth:45
	spinner FPS_settings "Base FPS" range:[1,4800,24] type:#integer fieldwidth:45
	button FPS24 "24 fps" width:48 align:#left across:3
	button FPS25 "25 fps" width:48 align:#center 
	button FPS30 "30 fps" width:48 align:#right 
	)
	on FPS24 pressed do FPS_settings.value = 24
	on FPS25 pressed do FPS_settings.value = 25
	on FPS30 pressed do FPS_settings.value = 30

	group "Camera Options"
	(
	
	checkbutton delete_old_cam "Delete Old Cameras&Dummies" checked:true highlightcolor:(color 255 200 200) width:150 align:#center
	
	checkbutton enable_in_vp "Activate in Current Viewport" checked:true highlightcolor:(color 200 255 200) width:150 align:#center
	checkbutton show_trajectory "Display Camera Trajectory" checked:true highlightcolor:(color 200 255 200) width:150 align:#center
	checkbutton flip_axes "Flip Camera Tilt Axis" checked:false highlightcolor:(color 200 200 255) width:150 align:#center
	checkbutton create_camera_dummy "Create Camera Dummy" checked:false highlightcolor:(color 255 255 200) width:150 align:#center
	
	)
	
--Function to calculate current time plus offsets
fn calculate_time t =
(
	current_time = ((t as time)+(offset_frames.value as time)) as float 
	current_time += offset_ticks.value 
	if blur_mode.selection == 2 then
		(
		current_time -= (TicksPerFrame/2.0)
		)
	
	current_time = (current_time / TicksPerFrame)
	current_time 
)--fn
	
	on import_camera pressed do
	(
	
	FrameRate = FPS_settings.value
	
		get_file = getOpenFileName caption:"Select KuperCamera ASCII File" \
		types:"ASCII Files (*.ASC)|*.ASC|All Files (*.*)|*.*|"
		
		if get_file != undefined then						--check if user selected a file or pressed Cancel
		(
		in_file = openFile get_file							--open specified file
		test_string = readchars in_file 7						--read first value from file
		
		if findstring test_string "Axes" == undefined then	--in case the first line does NOT contain the word "Axes"...
		(
		messagebox "The file you selected is not a valid Kuper ASCII File!" title:"KuperCamera Import:"	--error message!
		)
		else		--file is a valid KuperCamera ASCII File, so let's go on...
		(

		focus_yes = false			--flag for focus data availability
		channel_array = #()			--init array to store channel names

		for i = 1 to 8 do			--loop through 8 possible channel names
		(
		test_string = readdelimitedstring in_file " "	--read values between spaces
		
		--check for substring in channel name, store name in array


		if findstring test_string "lensroll" != undefined then append channel_array "lensroll"		
		if findstring test_string "Table" != undefined then append channel_array "Table"		
		
		if findstring test_string "VTrack" != undefined then append channel_array "Track"		
		if findstring test_string "VEW" != undefined then append channel_array "EW"
		if findstring test_string "VNS" != undefined then append channel_array "NS"
		if findstring test_string "VPan" != undefined then append channel_array "Pan"
		if findstring test_string "VTilt" != undefined then append channel_array "Tilt"
		if findstring test_string "VRoll" != undefined then append channel_array "Roll"
		if findstring test_string "Focus" != undefined then 
			(
			append channel_array "Focus"
			focus_yes = true
			)
		if findstring test_string "Editbits" != undefined then append channel_array "Editbits"
		)
		
		--print channel_array
		
		if delete_old_cam.checked then delete $KuperCam*		--delete all KuperCams if requested
		
		cam = freecamera name:(uniquename "KuperCam")			--create new free camera with unique name
		if create_camera_dummy.checked then 
			(
			cam_dummy = dummy boxsize:[50,50,50] name:(uniquename "KuperCamDummy")
			cam.parent = cam_dummy
			)
		
		if show_trajectory.checked then cam.showtrajectory = true	--enable trajectory display if requested
		if enable_in_vp.checked then viewport.setcamera cam			--activate camera in current viewport if requested
		
		cam.fov = 2*atan(0.5*GetRendApertureWidth()/camera_lens.value)

		if create_camera_dummy.checked then 
		(
		cam_dummy.rotation.controller = Euler_XYZ()					--set Euler Rotation Controller
		)
		else
		(
		cam.rotation.controller = Euler_XYZ()					--set Euler Rotation Controller
		)
		
		if focus_yes then 
			(
			focus_dummy = Dummy boxsize:[30,30,30] name:(uniquename "KuperCamFocus")	--create Focus Dummy if needed
			focus_dummy.parent = cam
			)
		
		t = 0														--set time counter to 0
		lines_count = 0												--init a new counter variable
		while not eof in_file do 									--loop through all lines of the file until the end
			(
			readline in_file										--read each line
			lines_count += 1										--increase counter
			)
		seek in_file 1												--go back to start of file
		counter = 0													--init a new lines counter for the progressbar
		readline in_file
--		try															--activate error trap for any case...
		(
	
		while not eof in_file do									--loop through the file until the end again... 
		(
		counter += 1												--increase line counter 
		progress.value = (100.0 * counter / lines_count)			--set progressbar to a percentage of lines read so far
		
		
		pos_x = 0
		pos_y = 0
		pos_z = 0 
		rot_x = 0 
		rot_y = 0
		rot_z = 0
		focus_val = 100
		
		--read channel values from file:

		for i = 1 to channel_array.count do	--loop through the number of channels to read
		(
		in_value = readvalue in_file		--read a single value, 
											--then check in array which channel it belongs to and assign to variable
											
		if channel_array[i] == "Track" then pos_y = in_value		
		if channel_array[i] == "EW" then pos_x = in_value
		if channel_array[i] == "NS" then pos_z = in_value
		if channel_array[i] == "Pan" then rot_y = in_value
		if channel_array[i] == "Tilt" then rot_x = in_value
		if channel_array[i] == "Roll" then rot_z = in_value
		if channel_array[i] == "Focus" then focus_val = in_value
		)

		--start animating
		
			animate on
			(
			current_time = calculate_time t	--ger current time incl. offsets
			
			at time current_time			--set time context to current time counter variable
			(				

			if create_camera_dummy.checked then 
				(
				cam_dummy.pos = [0,0,0]			--move camera to world origin
				--apply rotations in world space about world origin, transform coord sys to MAX coord space.
				in coordsys world about [0,0,0] cam_dummy.rotation.x_rotation = rot_x+90
				in coordsys world about [0,0,0] cam_dummy.rotation.y_rotation = -rot_z 
				in coordsys world about [0,0,0] cam_dummy.rotation.z_rotation = -rot_y
				cam_dummy.pos = [pos_x,-pos_y,pos_z]	--move camera to new position
				)
				else
				(
				cam.pos = [0,0,0]			--move camera to world origin
				--apply rotations in world space about world origin, transform coord sys to MAX coord space.
				
				if flip_axes.checked then
				(
				in coordsys world about [0,0,0] cam.rotation.x_rotation = -rot_x+90
				in coordsys world about [0,0,0] cam.rotation.y_rotation = -rot_z 
				in coordsys world about [0,0,0] cam.rotation.z_rotation = -rot_y
				cam.pos = [pos_x,-pos_y,pos_z]	--move camera to new position
				)
				else
				(
				in coordsys world about [0,0,0] cam.rotation.x_rotation = rot_x+90
				in coordsys world about [0,0,0] cam.rotation.y_rotation = -rot_z 
				in coordsys world about [0,0,0] cam.rotation.z_rotation = -rot_y
				cam.pos = [pos_x,-pos_y,pos_z]	--move camera to new position
				)--end flip
				)
				
				if focus_yes then in coordsys cam focus_dummy.pos = cam.pos + [0,0,focus_val] 	--if focus dummy required, set position
																								--in camera coord space along Z.
				
			)--end time
			)--end anim
		t += 1	--increase frame counter
			
		)--end while
		)
--		catch
		(
--		messagebox "There was an error while reading the ASCII File!" title:"KuperCamera Import:"
		)
		if t > 1 then t -= 2
		current_time = calculate_time t					--calculate last frame time
		animationRange = interval 0f current_time		--set scene range to new time
		
		--delete key on frame 0 in case there is offset involved
		
		if offset_frames.value > 0 or offset_ticks.value > 0 or blur_mode.selection == 2 then 
			(
			selectKeys cam.position.controller 0f
			selectKeys cam.rotation.controller 0f
	
			deleteKeys cam.position.controller #selection
			deleteKeys cam.rotation.controller #selection
			)
		progress.value = 0		--reset progress display
		select cam				--select camera
		max modify mode			--display camera in Modify panel
		)--end if test
		close in_file
		)--end if file
	)--end on
)--end roll

rollout about_roll "About..."
(
label about01 "Kuper Camera Import"
label about02 "Version 0.25 Beta"
label about03 "02/18/2000"
label about04 "by Borislav Petrov"
label about05 "http://gfxcentral.com/bobo/"

)

try(closerolloutfloater kuper_float)catch()
kuper_float = newrolloutfloater "KuperCameraIn" 200 530 100 100
addrollout kuper_roll kuper_float	
addrollout about_roll kuper_float rolledup:true

)