/*
BPL_morpherFunctions
Written By: Jason Hayes (jhayes@ballisticpixel.com)
Date: 02/28/2002

Description: These are a set of functions that allow you to do several 
			 things with the morpher modifier.
			 
Usage:
---------------------------------------------------------------------------
getMorphOBJ <object>
	Gets the current selected object with a morpher modifier.
	
extractMorphTargets <modifier> <object> <distance to copy> <num channels>
	Extracts morph channels to seperate objects.
	
resetBaseMorphChannels <modifier> <num channels>
	Resets the current selected objects morph channels back to 0.0
	
addMorphKeys <modifier> <channel(s)>
	Creates keyframes on all used channels.
	
exportMorphData <output file> <num channels>
	Exports current keyframe information to file.
	
importMorphData <input file>
	Imports animation data created by the exportMorphData function.
---------------------------------------------------------------------------
*/

global morphOBJ
global morphMOD
global ctrls = #()



fn getMorphObj obj = 
	(
	morphOBJ = obj 
	morphMOD = morphOBJ.modifiers["Morpher"]
	)


fn extractMorphTargets mod obj dis numChan =
	(
	progressStart "Extracting Morph Targets"
	morphPOS_x = morphOBJ.pos.x
	morphPOS_y = morphOBJ.pos.y
	morphPOS_z = morphOBJ.pos.z
	
	for i in 1 to numChan do
		(
		progressUpdate (i as float/numChan*100)
		mod[i].value = 100
		tempOBJ = copy morphOBJ
		mod[i].value = 0
		tempNAME = mod[i] as string
		filterNAME = substring tempNAME 13 100
		tempOBJ.name = filterNAME
		tempOBJ.pos.x += morphOBJ.pos.x += dis
		collapseStack tempOBJ
		)
	morphOBJ.pos = [morphPOS_x,morphPOS_y,morphPOS_z]
	progressEnd()
	)
	

fn resetBaseMorphChannels mod numChan =
	(
	progressStart "Reseting Base Morph Targets to 0.0"
	for i in 1 to numChan do
		(
		progressUpdate (i as float/numChan*100)
		mod[i].value = 0
		)
	progressEnd()
	)


fn addMorphKeys mod chan =
	(
	morphCTRL = mod[chan].controller
	addNewKey morphCTRL currentTime
	)


fn exportMorphData outFile numChan =
	(
	/* This function is based off of Claes Wikdahl code. */
	progressStart "Exporting Morph Animation"

	format "%\n" numChannels to:outFile
	for i in 1 to numChan do
		(
		progressUpdate (i as float/numChannels*100)
		ctrls[i] = morphOBJ[i].controller
		format "%\n" ctrls[i].keys.count to:outFile
	
		for k in ctrls[i].keys do
			(
			format "% " ((k.time as float)/(1s as float)) to:outfile
			format "%\n" k.value to:outfile
			format "%\n" k.inTangentType to:outfile
			format "%\n" k.outTangentType to:outfile
			)
		)
		close outfile
		progressEnd()
	)


fn importMorphData inFile =
	(
	/* This function is based off of Claes Wikdahl code. */
	progressStart "Importing Morph Animation"
	getData = openfile inFile
	for i in 1 to (readValue getData) do
		(
		deleteKeys ctrls[i] #allkeys
		numOfKeys = readValue getData
	
		for n = 1 to numOfKeys do
			(
			progressUpdate (i as float/numOfKeys*100)
			kTime = readvalue getData
			kVal = readvalue getData
			newkey = addnewkey ctrls[i] (kTime*1s)
			newkey.value = kVal
			newkey.inTangentType = (readvalue getData)
			newkey.outTangentType = (readvalue getData)
			)
		)
	close getData
	progressEnd()
	)