Script is getting very slow on big scenes

Hi I need your help.
I am trying to make a script to bake and action in the NLA tracks, but it gets very slow on bigger scenes (not just 3 cubes).
Can You please get me some advice on the script below, i’m pretty sure here is my problem from what i’ve tested:


  
        objectToCheck = bpy.context.scene.prop
        context = bpy.context
        st = bpy.context.scene.frame_start
        en = bpy.context.scene.frame_end
        #_____________________the loop _______________________________________________

        for x in range(st, en):
            context.scene.frame_set(x)
            curframe = bpy.context.scene.frame_current
            
         #__________________________check the rotation_______________________________  
         
            #go back one frame
            context.scene.frame_set(curframe-1)
            #get the rotation of the object on previous frame
            objectToCheck["DefLast"] = objectToCheck.rotation_euler[rolaxis]
            #go back to current frame
            context.scene.frame_set(curframe)
            #get the rotation of the object on the current frame
            objectToCheck["DefCurrent"] = objectToCheck.rotation_euler[rolaxis]

            #work out the difference between current and last frame
            def_dif = objectToCheck["DefCurrent"] - objectToCheck["DefLast"]
            
            #print stuff
            
            print ("deformation on frame")
            print (bpy.context.scene.frame_current)
            print (def_dif)
        #___________________________here it performes the action_________________________________   
            

Thank you.

There are many things you can do better. I will give better explanation once i get time but here is the updated for loop.

rotations = []
for x in range(st-1,en):
    context.scene.frame_set(x)
    rotations.append(objectToCheck.rotation_euler[rolaxis])
diff = []
for i in range(1,len(diff)):
    diff.append(diff[i]-diff[i-1])

there is no need to go to previous frame for every frame. Just start with one less frame.
Store rotation in new list. Now iterate over that list but skip the first one(index 0) and find the subtraction and store it in another list.
Edit : I didn’t test the the code but you get the idea.

2 Likes

Thank you i made it to work a bit faster from 50s for 100 frames to 15s, but i will apply this technique to the rest of the code and see if it gets faster. :smiley:

by the way for others the code got to be:

        rotations = []
        for x in range(st-1,en):
            context.scene.frame_set(x)
            rotations.append(objectToCheck.rotation_euler[rolaxis])
            def_dif = rotations[x]-rotations[x-1]
2 Likes

This code will give error : First reason is when start frame is something like 100 there will be no element in rotation list at 100th position. Second one is that for the first iteration of loop

this line will throw error.
I suggest this snippet of code

rotations = []
context.scene.frame_set(st-1)
rotations.append(objectToCheck.rotation_euler[rolaxis])
        for i,x in enumerate(range(st,en)):
            context.scene.frame_set(x)
            rotations.append(objectToCheck.rotation_euler[rolaxis])
            def_dif = rotations[i+1]-rotations[i]
1 Like