1
$\begingroup$

I am trying to export markers from the Blender motion tracker and found the following script that will do exactly that:

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]))

It works but I have two questions:

  1. How can I save the data on my Desktop rather than in folder in MacOS blender content where it saved right now. The following won't work:

    fn = '/User/me/Desktop/data/tr_{0}_{1}.csv'.format(clip.name.split('.')[0], track.name)

  2. The resulting values I get do not represent the more user friendly marker values in den Blender GUI. Instead of x/y coordinates in screen coordinates, I get a value between 0/1. How can I convert them to those screen coordinates ?

Thanks for your help

$\endgroup$
4
  • 2
    $\begingroup$ What OS? For windows the path should look like this: C:\\users\\user\\Desktop\\tr_{0}_{1}.... To get screen coords multiply the values with the height and width of your clip. For what application you'd like to export the data? $\endgroup$
    – p2or
    Commented Oct 21, 2016 at 12:32
  • $\begingroup$ You can open up a terminal and try it by your own. However I guess it's /Users/username/Desktop/. Also make sure there is a data folder... $\endgroup$
    – p2or
    Commented Oct 21, 2016 at 12:48
  • $\begingroup$ Thanks. That part I knew. But now I found the solution. Its the following if anybody every needs it. This is if you start in Applications. : fn = '../../../../../Users/YOURUSERNAMEHERE/Desktop/data/tr_{0}_{1}.csv'.format(clip.name.split('.')[0], track.name) $\endgroup$
    – digit
    Commented Oct 21, 2016 at 12:53
  • $\begingroup$ In Windows, you can access the root of your drive with \. In Linux, with / $\endgroup$ Commented Mar 6, 2020 at 7:15

1 Answer 1

2
$\begingroup$

(modified from this Source)

The script outputs a csv file with the coordinates for each tracker.

from __future__ import print_function
import bpy
D = bpy.data

outputFolder= "/set/your/output/directory/here"

printFrameNums = False # include frame numbers in the csv file
relativeCoords = False # marker coords will be relative to the dimensions of the clip

f2=open('export-markers.log', 'w')
print('First line test', file=f2)
for clip in D.movieclips:
    print('clip {0} found\n'.format(clip.name), file=f2)
    width=clip.size[0]
    height=clip.size[1]
    for ob in clip.tracking.objects:
        print('object {0} found\n'.format(ob.name), file=f2)
        for track in ob.tracks:
            print('track {0} found\n'.format(track.name), file=f2)
            fn = outputFolder+'{0}_{1}_tr_{2}.csv'.format(clip.name.split('.')[0], ob.name, track.name)
            with open(fn, 'w') as f:
                framenum = 0
                while framenum < clip.frame_duration:
                    markerAtFrame = track.markers.find_frame(framenum)
                    if markerAtFrame:
                        coords = markerAtFrame.co.xy
                        if relativeCoords:
                            if printFrameNums:
                                print('{0},{1},{2}'.format(framenum, coords[0], coords[1]), file=f)
                            else:
                                print('{0},{1}'.format(coords[0], coords[1]), file=f)
                        else:
                            if printFrameNums:
                                print('{0},{1},{2}'.format(framenum, coords[0]*width, coords[1]*height), file=f)
                            else:
                                print('{0},{1}'.format(coords[0]*width, coords[1]*height), file=f)

                    framenum += 1
f2.close()

Pay attention to the units used.

The script will output coordinates in pixels, where 0,0 is at the bottom left of the frame.

To use a float values, where the frame has coordinates 0 to 1, change relativeCoords = True

To include the frame number as part of the set, use printFrameNums = True

$\endgroup$

You must log in to answer this question.

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