Skip to content

Instantly share code, notes, and snippets.

@groakat
Forked from anonymous/gist:5663418
Last active March 25, 2024 11:28
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save groakat/e7d8394d57fd4d3fe016 to your computer and use it in GitHub Desktop.
Save groakat/e7d8394d57fd4d3fe016 to your computer and use it in GitHub Desktop.
Blender python script to export the motion tracking markers to .csv files.
from __future__ import print_function
import bpy
import os
D = bpy.data
printFrameNums = True # include frame numbers in the csv file
relativeCoords = False # marker coords will be relative to the dimensions of the clip
proj_folder = os.path.join(bpy.path.abspath('//'))
if os.path.basename(os.path.dirname(proj_folder)) == 'blender':
output_folder = os.path.join(bpy.path.abspath('//'), '..', '..', 'tracks/B_man/')
else:
output_folder = os.path.join(bpy.path.abspath('//'), '..', 'tracks/B_man/')
f2=open(output_folder + 'export-markers.log', 'w')
print('First line test', file=f2)
total_count = 0
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:
cnt = 0
print('track {0} found\n'.format(track.name), file=f2)
fn = output_folder + '{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 and not markerAtFrame.mute:
if markerAtFrame.is_keyed:
cnt += 1
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
print('track {0} has {1} keyframes\n'.format(track.name, cnt), file=f2)
total_count += cnt
print('total keyframes: {}\n'.format(total_count), file=f2)
f2.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment