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

1 Answer 1


You might want to try some of the built-in tools instead of python (my compliments for your algo-oriented lifestyle!)

The solution is in Object Constraint properties.

For a created camera I created an empty that "Copies transforms" of the object I want to track (a "car" named "Cube"). Then the camera Copies Transforms constraint of the empty "before original" to keep the original offset, and also "Track To" constraint of the Empty (to keep looking at it).

I then did a simple animation (keyed) of the Cube (standing in for your car). You might have a roadway "path" (curve) that the car follows instead. I include a link to my example blend file.

  • 1
    $\begingroup$ Thank you very much for your answer @james_t. I implemented my code on your setup and it works fine. Even though I would like to do it programmatically, I think maybe this will work as well. $\endgroup$
    – Chris Patr
    Commented Feb 10, 2021 at 10:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .