Python change of bone rotation is not update when export .bvh file
the scrip is active.

""""""""""""""""""""""""""""""""""""""""""""""""

for f in range(sce.frame_start, sce.frame_end+1):
    sce.frame_set(f)
    if f == FrameNumber:
        keyFrame = context.scene.frame_current
        keyInterp = context.user_preferences.edit.keyframe_new_interpolation_type
        print('key interp: {}'.format(keyInterp))
        for pbone in ob.pose.bones:
            if pbone.name == BoneName:

                lastMode = pbone.rotation_mode
                pbone.rotation_mode = "XYZ"
                bpy.ops.object.mode_set(mode='POSE')
                pbone.bone.select = True                    
                pbone[pbone.name].rotation_euler.x = math.radians(ValueX)
                pbone[pbone.name].rotation_euler.y = math.radians(ValueY)
                pbone[pbone.name].rotation_euler.z = math.radians(ValueZ)   

                bpy.context.scene.update()
                pbone.keyframe_insert(data_path="rotation_euler" ,frame=keyFrame)
                context.user_preferences.edit.keyframe_new_interpolation_type = keyInterp
                bpy.ops.object.mode_set(mode='OBJECT')
                pbone.rotation_mode = lastMode