Blender: exporting camera tracking markers to CSV

For university, I needed a camera tracker which could track motion of some particles under a microscope, and give me the coordinates of a particle for each frame. Blender has a very nice camera tracker which is ridiculously easy to use (and very fast), but it cannot export its results in a gnuplot-compatible format. But, it has a nice Python API, so you can just do it yourself!
This is a script which exports all camera tracking markers of all videos in a blender file to CSV files in a directory called “data”:

import bpy
D = bpy.data

for clip in D.movieclips:
    for track in clip.tracking.tracks:
        fn = 'data/tr_{0}_{1}.csv'.format(clip.name.split('.')[0], track.name)
        with open(fn, 'w') as f:
            frameno = 0
            while True:
                markerAtFrame = track.markers.find_frame(frameno)
                if not markerAtFrame:
                    break
                frameno += 1
                coords = markerAtFrame.co.xy
                f.write('{0} {1}n'.format(coords[0], coords[1]))

Just paste this to a blender text editor window and press Alt+P to run it. You have to create a directory named “data” manually before that.

Careful: The coordinate output is in units of the video height / width. So if your video is not square sized, you need to multiply the output by the video clip dimensions (clip.size[0] and clip.size[1]).

Categories: Everything

Tags: , ,

11 replies

  1. Thanks so much for this! Do you know the units of the coordinates (I'm getting values like 0.643…), and do you know if (0,0) is the center of the video or top left corner?

    • I don't know about (0, 0), but I'm pretty sure it's one of the corners since I don't remember getting any negative values ever.

      The coordinates are fractions of your video size, so if you get (0.6, 0.8) then it's really (0.6*video_width, 0.8*video_height) in pixels.

      Cheers!

  2. Thanks for the script, it was really helpful. However there are a couple of problems (at least in the more recent version of blender):
    1. The script will miss any tracks if they don't start at frame 0. (My tracks usually start at frame 1, for example).
    2. Blender now has object tracking, and this script misses the tracks that are assigned to objects instead of the camera.

    The script below should fix these:
    https://gist.github.com/anonymous/5663418

  3. [Re-posting interesting anonymous comment which was eaten by the blogpost spam filter]

    Hello, there is small improvment for this script
    – use of csv module instead of print() > easier to change data fromat
    – dict() for saving data with another parts.

    https://gist.github.com/anonymous/31c915d611f0a84e5d33

  4. Thank you very much! Under Windows, especially Win7, the path needs some tweaking

  5. Under Windows, the default working directory seems to be that one where blender.exe is located. Maybe, this can be changed in User preferences.

    Anyway, the following addition makes the folder with your blend file your default directory, then it checks if the subfolder_for_exports exists and creates it if it does not.

    import os
    subfolder_for_exports = 'data' # change it to whatever name
    os.chdir(os.path.dirname(os.path.dirname(__file__)))
    if not os.path.exists(subfolder_for_exports):
    REPLACE WTH SPACESos.makedirs(subfolder_for_exports)

    You know that notorious IdentationError when copy-n-pasting python scripts 😉

  6. Thank you. I was precisely going to write a script to export the trackers position and a quick search on google brought me here. You saved me a couple of hours 😉

  7. I was helped by this post of yours, thanks for the information, still useful after 10 years.

Trackbacks

  1. How can I get the list of all tracking markers using python command – GrindSkills

Leave a Reply to sven Cancel reply

Your email address will not be published. Required fields are marked *