macroScript DFAniTranslator category:"Grants" buttonText:"DFAniTranslator" toolTip:"" ( -- ghra@optushome.com.au -- various functions local getFileSizer, dupeThenModifyBezFloat, delDupe, determineInputType, loadAllPoints, correctLength, saveAllPoints, saveKeyPoints, saveSamplePoints, getSelTracks fn getFileSizer input = ( seek input 0 local cnt = -1 while not eof input do ( cnt +=1 skipToNextLine input ) seek input 0 return cnt ) fn dupeThenModifyBezFloat selTracksVale typer = ( --globalTracks[1].available.track = copy selTracksVal copyTrack = copy selTracksVale for i in 1 to 5 do ( try( globaltracks[i].available.controller = selTracksVale a = globaltracks[i][globaltracks[i].numsubs - 1] )catch() ) ---AddNewKey a.keys ( a.keys[a.keys.count].time +1f) --DeleteKey a.keys a.keys.count for i in copyTrack.keys do -- this to set all keys to custom if not already. ( i.intangenttype = typer i.outtangenttype = typer ) return copyTrack ) fn delDupe = ( a = globalTracks[1].numsubs - 1 if a != 0 do globalTracks[1].delete a ) fn determineInputType input = ( if (readline input) != "DFSP" do return false if eof input do exit skipToNextLine input lineTest = readLine input testArray = filterstring lineTest " " return testArray.count ) fn loadAllPoints ctrlr = ( TheFilePath = getopenFileName caption:"Load DF spl File" types:"DFSpline(*.spl)|*.spl|All|*.*|" if TheFilePath == undefined do return() try (testVal = (classof ctrlr.keys[1].value))catch() if testVal == point3 do (messagebox "Can only load into a bezier_float" title:"Nope"; return() ) deletekeys ctrlr -- clear out existing fg = openfile TheFilePath -- disablesceneredraw() -- ctrlr = $sphere01.scale.controller.x_scale.controller if fg != undefined then ( inputType = determineInputType fg if inputType == false do (messagebox "Not a dfsp" title:"nope"; return()) --return() seek fg 0 -- back to start case inputType of ( 6: ( ff = ((readdelimitedstring fg "") as stringstream) skiptonextline ff ------- the first key, as it is structured differently in the DF spl file. if eof ff do exit -- exit if end of file keyTime = readvalue ff keyVal = readvalue ff if eof ff do exit -- exit if end of file outx = readvalue ff outy = readvalue ff addnewkey ctrlr keyTime c = ctrlr.keys[1] if c == undefined do (messagebox "Can only load into a bezier_float" title:"Nope"; deletekeys ctrlr; return() ) testVal = (classof c.value) if testVal == point3 do (messagebox "Can only load into a bezier_float" title:"Nope"; deletekeys ctrlr; return() ) c.value = keyVal c.intangenttype = #custom c.outtangenttype = #custom c.freehandle = on c.outtangent = (outy-keyVal) * 0.1875/(outx-keytime) -- this is my universal constant:) c.outtangentlength = (outx - keytime) lineCount = (getfilesizer ff) seek ff 0 skipToNextLine ff for i in 1 to 4 do readvalue ff for i in 2 to lineCount do -- then the rest, replace 10000 with a user defined value ( inx = readvalue ff iny = readvalue ff keyTime = readvalue ff keyVal = readvalue ff if eof ff do ( addnewkey ctrlr keyTime c = ctrlr.keys[i] c.value = keyVal c.intangenttype = #custom c.outtangenttype = #custom c.freehandle = on c.intangent = -(keyVal-iny) * 0.1875/(keyTime-inx) c.intangentlength = (keyTime-inx) exit -- exit if end of file ) outx = readvalue ff outy = readvalue ff addnewkey ctrlr keyTime c = ctrlr.keys[i] c.value = keyVal c.intangenttype = #custom c.outtangenttype = #custom c.freehandle = on c.intangent = -(keyVal-iny) * 0.1875/(keyTime-inx) c.intangentlength = (keyTime-inx) c.outtangent = (outy-keyVal) * 0.1875/(outx-keytime) c.outtangentlength = (outx - keytime) --if not eof ff do skiptonextline ff -- remove to do subframe samples ) ) 2:( ff = ((readdelimitedstring fg "") as stringstream) skiptonextline ff if eof ff do exit -- exit if end of file lineCount = (getfilesizer ff) seek ff 0 skipToNextLine ff for i in 1 to lineCount do -- then the rest, replace 10000 with a user defined value ( if eof ff do exit keyTime = readvalue ff keyVal = readvalue ff addnewkey ctrlr keyTime c = ctrlr.keys[i] c.value = keyVal c.intangenttype = #linear c.outtangenttype = #linear --if not eof ff do skiptonextline ff -- remove to do subframe samples ) ) ) close fg ) -- enablesceneredraw() -- redrawViews() ) function returnPoint1 input = ( if classof input == point3 do input = input[DFaniloadsave.rdo2.state] return input ) fn correctLength switch index controller = ( local output --switch: 0 == intangentlength, 1 == outtangentlength if controller.keys[index].freehandle == true do return ( if switch == 1 then (returnPoint1 controller.keys[index].outtangentlength) else (returnPoint1 controller.keys[index].intangentlength) ) -- this updates the track correctly AddNewKey controller.keys ( controller.keys[controller.keys.count].time +1f) DeleteKey controller.keys controller.keys.count case switch of ( 0:( output = controller.keys[index].intangentlength * (controller.keys[index].time.frame - controller.keys[index - 1].time.frame) ) 1:( output = controller.keys[index].outtangentlength * (controller.keys[index+1].time.frame - controller.keys[index].time.frame) ) ) if classof output == point3 do output = output[DFaniloadsave.rdo2.state] return (output) ) fn saveAllPoints ctrlr outputText = ( outputtext = replace outputtext 1 1 "" if outputtext[1] == "." do outputtext = replace outputtext 1 1 "" outputtext += ".spl" TheFilePath = getSaveFileName caption:"Save DF spl File" types:"DFSpline(*.spl)|*.spl|All|*.*|" filename:outputtext if TheFilePath == undefined do return() fg = createfile TheFilePath if fg != undefined then ( ff = "" as stringstream format "DFSP\n" to:ff ------- the first key, as it is structured differently in the DF spl file. a = ctrlr.keys[1] b = ((a.time.frame as float) as string) + " " format b to:ff b = (returnPoint1 a.value) as string format b to:ff format "\n" to:ff -- RETURN tmpLength = correctLength 1 1 ctrlr -- turning freehandle on corrupts, hence this b = (tmpLength - a.time.frame as float) as string + " " format b to:ff b = (((returnPoint1 a.outtangent) * (tmpLength - a.time.frame as float))/0.1875 + (returnPoint1 a.value)) as string + " " format b to:ff for i in 2 to ctrlr.keys.count do ( a = ctrlr.keys[i] if a == undefined do exit tmpLength = correctLength 0 i ctrlr b = (a.time.frame as float - tmpLength) as string + " " format b to:ff b = (((returnPoint1 a.intangent) * tmpLength)/0.1875 + (returnPoint1 a.value)) as string + " " format b to:ff b = ((a.time.frame as float) as string) + " " format b to:ff b = (returnPoint1 a.value) as string format b to:ff format "\n" to:ff -- RETURN if ctrlr.keys[i + 1] == undefined do exit -- dont want last two lines tmpLength = correctLength 1 i ctrlr b = (tmpLength + a.time.frame as float) as string + " " format b to:ff b = (((returnPoint1 a.outtangent) * tmpLength)/0.1875 + (returnPoint1 a.value)) as string + " " format b to:ff ) format (ff as string) to:fg close fg close fg -- twice, has trouble closing? ) ) fn saveKeyPoints ctrlr outputText = ( outputtext = replace outputtext 1 1 "" if outputtext[1] == "." do outputtext = replace outputtext 1 1 "" outputtext += ".spl" TheFilePath = getSaveFileName caption:"Save DF spl File" types:"DFSpline(*.spl)|*.spl|All|*.*|" filename:outputtext if TheFilePath == undefined do return() fg = createfile TheFilePath if fg != undefined then ( ff = "" as stringstream format "DFSP\n" to:ff ------- the first key, as it is structured differently in the DF spl file. for i in 1 to ctrlr.keys.count do ( a = ctrlr.keys[i] if a == undefined do exit b = ((a.time.frame as float) as string) + " " format b to:ff val = a.value if classof val == point3 do val = val[DFaniloadsave.rdo2.state] b = val as string format b to:ff format "\n" to:ff -- RETURN if ctrlr.keys[i + 1] == undefined do exit -- dont want last two lines ) format (ff as string) to:fg close fg close fg -- twice, has trouble closing? ) ) fn saveSamplePoints ctrlr outputText = ( outputtext = replace outputtext 1 1 "" if outputtext[1] == "." do outputtext = replace outputtext 1 1 "" outputtext += ".spl" TheFilePath = getSaveFileName caption:"Save DF spl File" types:"DFSpline(*.spl)|*.spl|All|*.*|" filename:outputtext if TheFilePath == undefined do return() fg = createfile TheFilePath if fg != undefined then ( local starttime, endtime ff = "" as stringstream format "DFSP\n" to:ff ------- the first key, as it is structured differently in the DF spl file. if dfaniloadsave.aniRange.checked then (startTime = animationrange.start.frame; endtime = animationrange.end.frame) else (starttime = DFAniLoadSave.starttime.value; endtime = DFAniLoadSave.endtime.value) if DFaniloadsave.subsamp.checked then sampleval = 0.5 else sampleval = 1 for i in startTime to endTime by sampleVal do ( b = ((i as float) as string) + " " format b to:ff val = (at time i (ctrlr.value)) if classof val == point3 do val = val[DFaniloadsave.rdo2.state] b = val as string format b to:ff format "\n" to:ff -- RETURN ) format (ff as string) to:fg close fg close fg -- twice, has trouble closing? ) ) fn getSelTracks = ( tv = trackviews.currentTrackView if tv == undefined then ( messagebox "You need a Trackview open and one track selected" title:"DF ani util" return(false) ) selTracks = #() if tv.numSelTracks() > 1 do (messagebox "More than one track selected in the TV - only one track at a time please, cancelling" title:"nope";return(false)) for i = 1 to 1 do --tv.numSelTracks() do -- maybe multiples later on ( try ( -- all these ands to stop various tracks interfering if selected. Not all catered for. if ((tv.getselected i != undefined) AND (classof (tv.getselected i) != MAXRefTarg) AND \ (classof (tv.getselected i) as string != "RenderEffects") ) then --AND ((tv.getSelected i).numsubs < 2)) do ( append selTracks (tv.getSelected i) ) ) catch() ) return selTracks ) ---------------------------------------------- try (destroydialog DFAniLoadSave ) catch() global DFAniLoadSave rollout dfaniloadsave "DFAniLoadSaver 0.35" width:196 height:380 ( label lb1 "Operates on a single controller selected in the Track View" pos:[16,13] width:147 height:31 radiobuttons rdo1 "DF spline type" pos:[24,63] width:140 height:62 labels:#("Samples (one per frame)", "Key Points (linear keys)", "All Points (keys, handles)") default:3 columns:1 button ldSpl "Load .spl file (autodetect type)" pos:[20,335] width:152 height:24 button svSpl "Save .spl file" pos:[21,277] width:152 height:24 GroupBox grp1 "Save" pos:[12,46] width:174 height:268 GroupBox grp2 "Load" pos:[12,315] width:174 height:54 GroupBox grp3 "" pos:[12,180] width:174 height:90 checkbox aniRange "Use Animation Range" pos:[28,199] width:148 height:18 checked:true spinner starttime "Start" pos:[29,221] width:79 height:16 type:#integer scale:1 spinner endtime "End" pos:[32,245] width:76 height:16 range:[0,100,0] type:#integer scale:1 checkbox subsamp "Sub Sample" pos:[119,222] width:56 height:35 GroupBox grp4 "" pos:[12,133] width:174 height:54 label lbl3 " Samples Control " pos:[23,179] width:84 height:15 label p3Output " Point3 output" pos:[25,132] width:71 height:15 radioButtons rdo2 "" pos:[32,155] width:134 height:16 labels:#("1st", "2nd", "3rd") columns:3 on getCntrl pressed do ( selTracksVal = undefined selTracksVal = getSelTracks() if selTracksVal[1] != undefined then ( print selTracksVal[1] --lb1.text = selTracksVal[1].name ) else messagebox "Unsuitable or no track selected in the Track View" ) on rdo1 changed state do ( if state != 1 then for i in 8 to 11 do DFaniloadsave.controls[i].enabled = false else for i in 8 to 11 do DFaniloadsave.controls[i].enabled = true ) on ldSpl pressed do ( try( selTracksVal = getSelTracks() if selTracksVal[1] != undefined do loadAllPoints selTracksVal[1] )catch() ) on svSpl pressed do ( selTracksVal = getSelTracks() if selTracksVal == false OR selTracksVal.count == 0 OR selTracksVal[1].keys.count == 0 OR selTracksVal[1] == undefined do return(messagebox "No keys, no track selected, or invalid track" title:"Nope") temp = exprForMAXObject selTracksVal[1] if temp == "" do temp = "unknown" outputText = temp + \ ( case rdo1.state of ( 1:(" - Samples" ) 2:(" - Key Points" ) 3:(" - All Points") ) ) if findstring ((classof selTracksVal[1]) as string) "bezier" == undefined AND rdo1.state != 1 do (messagebox "Non-bezier controllers can only be saved out using 'Samples', cancelling" title:"Bezier operation on non-bezier track" ;return()) if rdo1.state == 1 do ( modCurve = selTracksVal[1] -- make a dupe and convert all keys to #linear saveSamplePoints modCurve outputText ) if rdo1.state == 2 do ( modCurve = selTracksVal[1] saveKeyPoints modCurve outputText ) if rdo1.state == 3 do ( modCurve = (dupeThenModifyBezFloat selTracksVal[1] #custom) -- make a dupe and convert all keys to #custom saveAllPoints modCurve outputText ) ) on dfaniloadsave open do for i in 8 to 11 do ( DFaniloadsave.controls[i].enabled = false startTime.value = animationrange.start endTime.value = animationrange.end ) ) CreateDialog DFAniLoadSave style:#(#style_minimizebox, #style_sysmenu, #style_titlebar) )