Importing keyframes from textfile freezes maya

Okey. I am quite new to scripting in Maya but I try to make the best out of my limited knowledge.

I have a textfile that we get from unity with ~162000 frames that I want to import into Maya as keyframes. And it has worked great when it is files with around 50000 frames but when it increases the program it seems to use up all the memory So I would say that there is where the problem is. I have searched on google and found that I could use openMaya and use the api instead of cmds. But I have not been able to get it to work.

import maya.cmds as cmds

with open("C:\Textfiles\FacialData_Doctor(Clone)2001_Peter_fps40_2017-08-02_14-04-28.txt", 'r') as f:
    for line in f:
      framecount, blendshape, name, x = line.strip(" \t\r\n[]").split(",")
      objectName = cmds.ls(name[10:] + "_Driver")
      #print(name)
      #print(objectName)
      print(framecount)
      cmds.setKeyframe( objectName, time=int(framecount), attribute='translateX', value=float(x)/float(10)*float(3) )
print("Done")            
f.close()

This is how i do it now. It is slow but it works. Except for the problem above.
The lines in the text document looks like this:

2,58.1543,FaceMorph.JawForward,0

where the lines are seperated with “,” first comes framecount then miliseconds then name of the blendshape and then we have the translate X.
we have 23 blendshapes per frame.

Hopefully I am able to express my problem.
Thank you!

Removing the print statements might help with overall speed. In general printing in a tight loop is rather slow in maya.

As for the crash, I can’t see anything in here that would cause anything like that.

162,000 frames is a lot (is that 90 minutes of animation?). On a heavy mesh I can see that being very taxing . It also looks like the object name is a list (since ls will return a list) – maybe it’s generating keys on unexpected objects? Does

objectName = cmds.ls(name[10:] + "_Driver")[0]

help?

Btw

float(x)/float(10)*float(3)

can just be

float(x) * 0.3

can’t it?

Thank you R.White. I will try it out.

Theodox:
I have created a number of controller objects in the scene(sliders) for each blendshape of the character(we have 23) so it is 162,000 frames * 23 so it might just be that my laptop can’t handle the load.
The reason I am doing the ls is to select the corresponding controller to that blendhsape and in this case our mesh had a naming convention of facemorph.(blendshapename) and that is why I needed to remove the (facemorph.) bit to find the correct driver to add the keyframe to.
And regarding the math. What an embarrassing mistake! Thank you for pointing it out!

For others that might face the same problem. It was printing out the number to console that resulted in a much slower import and the freezing. Don’t know why I did not test that before coming here but yeah. Thank you R.White!

Nice. Glad that worked for you.