Trying to set Keyframes for multiple materials

I have a python script which creates 5 cube objects equally spaced along a path, assigns a unique material to each, and then tries to assign keyframes to each material based on its position along the path. The critical section of code is:

    for x in bpy.context.selected_objects:
        print(x)
        objs = x.constraints.new(type='FOLLOW_PATH')
        objs.target = context.scene.objects.get(curveName)
        objs.use_curve_follow = True
        objs.offset = -count*offset
        
        # get the material
        mat = bpy.data.materials[matNames[count]]
        # get the nodes
        nodes = mat.node_tree.nodes
        # get some specific node:
        mix = nodes.get("Mix Shader")
        
        print("Setting values for material ",mat.name,"  Offset ",objs.offset)
        
        num = 10
        numInc = float(1.0/num)
        for ix in range(num+1):
            frame = float(numInc*ix) + abs(objs.offset/pathLength)
            if frame > 1.0:
                frame = frame - 1.
            fact = GetFactor(frame)
            frame = frame*pathLength
            mix.inputs[0].default_value = fact
            mix.inputs[0].keyframe_insert("default_value",frame = int(max(1,frame)))
            print("Setting Keyframe %.2f"%int(max(1,frame))," to %.2f"%fact);  
            
        count = count - 1

Several strange things happen once i execute the script and then play the animation. Initially the value for the mix shader for each cube is assigned OK. Once the animation starts, these values seem to change. I know i am doing something wrong, but don’t know quite what.

Attachments

Arrows along Path 2.blend (851 KB)

I have solved this issue.