Script dropping performance as animation range gets longer


I have a script that is supposed to run through every frame and output a selected camera’s animation data. The problem I’ve run into is that the performance of the script decreases as the total animation range increases. For example, if I have a 100 frame range it takes about 3 seconds to get through all 100 frames, but if I have a 3000 frame range it takes 20 seconds to get through those same 100 frames. I’ve already done everything I thought would help for optimization with no luck. What could be the underlying issue?

camExportDummy = Dummy name:"CameraExportDummy" pos:[0,0,0]
undo off
for i = animationRange.start to animationRange.end do
   sliderTime = i
   cbTimestamp = at time i $MetadataDummy.modifiers[1].replayCurrentFrame as string
   format "%\t\t"cbTimestamp to:outFile
   camx = at time i $.transform.pos.x
   camy = at time i $.transform.pos.y*-1
   camz = at time i $.transform.pos.z
   format "%,%,%\t" camx camy camz to:outFile
   camExportDummy.rotation = at time i $.transform.rotation
   camExportDummy.transform *= (rotateXmatrix 90)
   camExportDummy.transform *= (rotateZmatrix -90)
   camQX = camExportDummy.rotation.x
   camQY = camExportDummy.rotation.y
   camQZ = camExportDummy.rotation.z
   camQW = camExportDummy.rotation.w
   format "%,%,%,%\t" -camQX camQY -camQZ camQW to:outFile

   camFOV = at time i $.fov
   format "%\t" camFOV to:outFile
   camFocus = at time i $.mpassEffect.focalDepth
   format "%" camFocus to:outFile
   if i < end - 1 do
   	format "\n" to:outFile
   i += 1
delete camExportDummy
close outFile
undo on

for i = animationRange.start to animationRange.end do at time i
   cbTimestamp = $MetadataDummy.modifiers[1].replayCurrentFrame as string
   format "%\t\t"cbTimestamp to:outFile
   pos = $.transform.pos * [1,-1,1]
   format "%,%,%\t" pos.x pos.y pos.z to:outFile
   rot_tm = $.transform.rotation as matrix3
   rotateX rot_tm 90
   rotateZ rot_tm -90
   camQ = rot_tm.rotation
   format "%,%,%,%\t" -camQ.x camQ.y -camQ.z camQ.w to:outFile

   camFOV = $.fov
   format "%\t" camFOV to:outFile
   camFocus = $.mpassEffect.focalDepth
   format "%" camFocus to:outFile
   if i < end - 1 do
   	format "\n" to:outFile
   --i += 1
close outFile

i don’t have max around to test, but get the idea.

you don’t need to move slider, and you should use ‘at time’ context only once


Thanks, that fixed it! Now it’s doing all 3000 frames in under a second. However, I had to keep my original rotateXmatrix section of the code since the rot_tm section in the example was returning different values