Hello Guys!
I wrote a code with chatgpt help.
It partially works.
My problem now is how to tell Blender that each marker has a 4mm size.
I tried with the script code but it seems that not work well.
Some markers are fixed but when I run the script and see the csv file, the Y (vertical displacement) has information of displacement. But the markers are fixed, no movement.
The Y are reported in csv file with information less than 0.01… I tried to say the markers are 4mm… Chatgpt told that Blender use meters… so in the code values were divided per 1000…
If someone can help me…
Here is the script:
from future import print_function
import bpy
import os
import csv
Specify the desired output directory
output_directory = “C:/Users/tiago/OneDrive/Desktop/Piloto Teste/RAIVA”
Create the output directory if it doesn’t exist
os.makedirs(output_directory, exist_ok=True)
D = bpy.data
printFrameNums = False # include frame numbers in the csv file
relativeCoords = False # marker coords will be relative to the dimensions of the clip
Set the marker size to 4mm
marker_size_mm = 4
Convert the marker size from mm to the blender units (e.g., meters)
scale_factor = 0.001 # 1 Blender unit is 1 meter
Camera shake factor (adjust as needed)
camera_shake_factor = 0.1 # Example: 0.1 Blender units (meters) of horizontal displacement per frame
f2 = open(output_directory + ‘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 = output_directory + '{0}_{1}_tr_{2}.csv'.format(clip.name.split('.')[0], ob.name, track.name)
displacement_fn = output_directory + 'displacement_{0}_{1}_{2}.csv'.format(clip.name.split('.')[0], ob.name, track.name)
with open(fn, 'w', newline='') as f, open(displacement_fn, 'w', newline='') as df:
# Create CSV writers
f_csv = csv.writer(f)
df_csv = csv.writer(df)
# Add header to the CSV files
f_csv.writerow(["Time (s)", "Y"]) # Header for the position data
df_csv.writerow(["Time (s)", "Y"]) # Header for the displacement data
frame_start = 1
frame_end = 435
frame_rate = 30
frame_range = range(frame_start, frame_end + 1)
prev_coords = None
for framenum in frame_range:
markerAtFrame = track.markers.find_frame(framenum)
if markerAtFrame:
coords = markerAtFrame.co.xy
if prev_coords is not None:
x_distance = coords[0] - prev_coords[0]
y_distance = coords[1] - prev_coords[1]
else:
x_distance = 0
y_distance = 0
# Adjust horizontal displacement for camera shake
x_distance -= camera_shake_factor * (framenum - frame_start)
# Calculate the time in seconds based on the frame rate
time_seconds = (framenum - frame_start) / frame_rate
# Adjust Y distance relative to frame 1
if framenum == frame_start:
y_distance = 0
if relativeCoords:
f_csv.writerow([time_seconds, y_distance * scale_factor])
df_csv.writerow([time_seconds, y_distance * scale_factor])
else:
f_csv.writerow([time_seconds, y_distance * marker_size_mm])
df_csv.writerow([time_seconds, y_distance * marker_size_mm])
prev_coords = coords
# Clear all keyframes in the graph editor
for area in bpy.context.screen.areas:
if area.type == 'GRAPH_EDITOR':
override = bpy.context.copy()
override['area'] = area
override['space_data'] = area.spaces.active
bpy.ops.graph.select_all(override, action='SELECT')
bpy.ops.graph.delete(override)
f2.close()