I have been involved with Blender the last two days and I want to use it in order to animate a moving vehicle from the calculations coming from Matlab/Simulink model. In particular, I export the results of the simulations from Matlab to an excel file which I read in Blender and I move an object based on the locations and angles coming from the simulation. However, I am facing an issue today; even though I have managed to make the object move, I want the camera to be somehow locked on the object and follow it (like in car games where the camera is behind the object and it follows it during the maneuver). I have written the following script thus far, but it is not doing what I expect it to do. I also have found many videos on youtube of people showing how a camera can keep track of an object while the camera is static. However, I have found no sources on how to implement something in the Python API and more importantly the way that I imagine it. Anyone can help?
import pandas as pd
import math
import bpy
import numpy
df = pd.read_excel (r'C:\Program Files\Blender Foundation\Blender 2.91\2.91\python\bin\Simulation_output.xls')
array = df.to_numpy()
rows,columns = array.shape
chassis = bpy.data.objects['Cube']
camera = bpy.data.objects['Camera']
light = bpy.data.objects['Light']
init_pos = camera.location
init_pos_light = light.location
fr = 1
for i in range(0,rows):
light.location = (init_pos_light[0]+array[i][0]*1000,init_pos_light[1]+array[i][1]*1000,init_pos_light[2]+array[i][2]*1000)
camera.location = (init_pos[0]+array[i][0]*1000,init_pos[1]+array[i][1]*1000,init_pos[2]+array[i][2]*1000)
chassis.location = (array[i][0]*1000,array[i][1]*1000,array[i][2]*1000)
chassis.keyframe_insert(data_path = "location",frame = fr,index = -1)
fr += 1