1 of 21

Blender-Track an object and save the position data

Dr. Sunny Jung

2 of 21

3 of 21

4 of 21

Click “Add” in “Marker”

5 of 21

You will place a square near the sphere.

6 of 21

7 of 21

8 of 21

9 of 21

10 of 21

11 of 21

12 of 21

13 of 21

14 of 21

Change its “filepath”

15 of 21

16 of 21

17 of 21

18 of 21

19 of 21

# http://scummos.blogspot.cz/2012/11/blender-exporting-camera-tracking.html

# https://gist.github.com/anonymous/5663418

import bpy

import csv

D = bpy.data

frameNums = True # include frame numbers in the csv file

relativeCoords = False # marker coords will be relative to the dimensions of the clip

filepath = "/Users/sunnyjung/Desktop/Python_folder/Image analysis/"

markers = {}

for clip in D.movieclips:

print('Clip {} found'.format(clip.name))

if relativeCoords:

width = 1

height = 1

else:

width = clip.size[0]

height = clip.size[1]

for ob in clip.tracking.objects:

print('Object {} found'.format(ob.name))

for track in ob.tracks:

fn = '{}_{}_tr_{}'.format(clip.name.split('.')[0], ob.name, track.name)

markers[fn] = []

print('track {} found'.format(track.name))

for framenum in range(clip.frame_duration):

markerAtFrame = track.markers.find_frame(framenum)

if markerAtFrame:

coords = list(markerAtFrame.co.xy)

coords = [coords[0] * width, coords[1] * height]

markers[fn].append(coords)

Tracking code in python

Change its “filepath”

20 of 21

for key, value in markers.items():

print(key)

filename = "{}{}{}".format(filepath, key, ".csv")

open_data = open(filename, 'w', newline='')

with open_data as writer:

writer = csv.writer(writer, delimiter=';', quotechar='"', quoting=csv.QUOTE_NONNUMERIC)

if frameNums:

writer.writerow([key, "x", "y"])

for i, data in enumerate(value):

writer.writerow([i] + data)

else:

writer.writerow(["x", "y"])

for data in value:

writer.writerow(data)

21 of 21

References