#!/usr/bin/env python3

with open('2dtracks_v02.txt', 'r') as f:
    lines = f.read().strip().split('\n')

track_count = int(lines[0])
tracks = []
current_line = 1

for i in range(track_count):
    track_num = lines[current_line]
    current_line += 1
    frame_start = int(lines[current_line])
    current_line += 1
    num_points = int(lines[current_line])
    current_line += 1
    
    points = []
    for j in range(num_points):
        parts = lines[current_line].strip().split()
        points.append({
            'frame': int(parts[0]),
            'x': float(parts[1]),
            'y': float(parts[2])
        })
        current_line += 1
    
    tracks.append({
        'name': f'track {int(track_num)}',
        'frame_start': frame_start,
        'points': points
    })

def generate_curve_data(points, axis):
    curve_data = '{curve'
    for point in points:
        value = point['x'] if axis == 'x' else point['y']
        curve_data += f' x{point["frame"]} {value}'
    curve_data += '}'
    return curve_data

track_entries = []
for index, track in enumerate(tracks):
    track_x = generate_curve_data(track['points'], 'x')
    track_y = generate_curve_data(track['points'], 'y')
    
    enabled = '1' if index == 0 else '0'
    entry = f' {{ {{curve K x1 1}} "{track["name"]}" {track_x} {track_y} {{curve K x1 0}} {{curve K x1 0}} {enabled} 0 0 {{curve x1 0}} 1 0 -32 -32 32 32 -22 -22 22 22 {{}} {{}}  {{}}  {{}}  {{}}  {{}}  {{}}  {{}}  {{}}  {{}}  {{}}   }}'
    
    track_entries.append(entry)

nuke_file = f"""#! C:/Program Files/Nuke15.2v3/nuke-15.2.3.dll -nx
version 15.2 v3
define_window_layout_xml {{<?xml version="1.0" encoding="UTF-8"?>
<layout version="1.0">
    <window x="-1" y="-7" w="2560" h="1377" maximized="1" screen="0">
        <splitter orientation="1">
            <split size="1620"/>
            <splitter orientation="2">
                <split size="1010"/>
                <splitter orientation="1">
                    <split size="40"/>
                    <dock id="" activePageId="Toolbar.1">
                        <page id="Toolbar.1"/>
                    </dock>
                    <split size="1576"/>
                    <dock id="" activePageId="Viewer.1">
                        <page id="Viewer.1"/>
                    </dock>
                </splitter>
                <split size="325"/>
                <splitter orientation="1">
                    <split size="994"/>
                    <dock id="" activePageId="uk.co.thefoundry.backgroundrenderview.1">
                        <page id="uk.co.thefoundry.scripteditor.1"/>
                        <page id="DopeSheet.1"/>
                        <page id="Curve Editor.1"/>
                        <page id="uk.co.thefoundry.waveformscope.1"/>
                        <page id="uk.co.thefoundry.histogram.1"/>
                        <page id="uk.co.thefoundry.backgroundrenderview.1"/>
                    </dock>
                    <split size="622"/>
                    <dock id="" activePageId="uk.co.thefoundry.vectorscope.1">
                        <page id="Pixel Analyzer.1"/>
                        <page id="uk.co.thefoundry.vectorscope.1"/>
                    </dock>
                </splitter>
            </splitter>
            <split size="936"/>
            <splitter orientation="2">
                <split size="1339"/>
                <dock id="" hideTitles="1" activePageId="DAG.1" focus="true">
                    <page id="DAG.1"/>
                </dock>
            </splitter>
        </splitter>
    </window>
</layout>
}}
Root {{
inputs 0
name T:/ADXA/exampletracks.nk
format "2048 1556 0 0 2048 1556 1 2K_Super_35(full-ap)"
proxy_type scale
proxy_format "1024 778 0 0 1024 778 1 1K_Super_35(full-ap)"
colorManagement OCIO
OCIO_config fn-nuke_studio-config-v1.0.0_aces-v1.3_ocio-v2.1
defaultViewerLUT "OCIO LUTs"
workingSpaceLUT scene_linear
monitorLut "ACES 1.0 - SDR Video (sRGB - Display)"
monitorOutLUT "ACES 1.0 - SDR Video (sRGB - Display)"
int8Lut matte_paint
int16Lut texture_paint
logLut compositing_log
floatLut scene_linear
}}
Tracker4 {{
inputs 0
tracks {{ {{ 1 31 {track_count} }} 
{{ {{ 5 1 20 enable e 1 }} 
{{ 3 1 75 name name 1 }} 
{{ 2 1 58 track_x track_x 1 }} 
{{ 2 1 58 track_y track_y 1 }} 
{{ 2 1 63 offset_x offset_x 1 }} 
{{ 2 1 63 offset_y offset_y 1 }} 
{{ 4 1 27 T T 1 }} 
{{ 4 1 27 R R 1 }} 
{{ 4 1 27 S S 1 }} 
{{ 2 0 45 error error 1 }} 
{{ 1 1 0 error_min error_min 1 }} 
{{ 1 1 0 error_max error_max 1 }} 
{{ 1 1 0 pattern_x pattern_x 1 }} 
{{ 1 1 0 pattern_y pattern_y 1 }} 
{{ 1 1 0 pattern_r pattern_r 1 }} 
{{ 1 1 0 pattern_t pattern_t 1 }} 
{{ 1 1 0 search_x search_x 1 }} 
{{ 1 1 0 search_y search_y 1 }} 
{{ 1 1 0 search_r search_r 1 }} 
{{ 1 1 0 search_t search_t 1 }} 
{{ 2 1 0 key_track key_track 1 }} 
{{ 2 1 0 key_search_x key_search_x 1 }} 
{{ 2 1 0 key_search_y key_search_y 1 }} 
{{ 2 1 0 key_search_r key_search_r 1 }} 
{{ 2 1 0 key_search_t key_search_t 1 }} 
{{ 2 1 0 key_track_x key_track_x 1 }} 
{{ 2 1 0 key_track_y key_track_y 1 }} 
{{ 2 1 0 key_track_r key_track_r 1 }} 
{{ 2 1 0 key_track_t key_track_t 1 }} 
{{ 2 1 0 key_centre_offset_x key_centre_offset_x 1 }} 
{{ 2 1 0 key_centre_offset_y key_centre_offset_y 1 }} 
}} 
{{ 
{chr(10).join(track_entries)}
}} 
}}

center {{1024 778}}
selected_tracks 3
name Tracker1
xpos -75
ypos -94
}}
"""
with open('output_tracks.nk', 'w') as f:
    f.write(nuke_file)

print('Conversion complete!')
print(f'Converted {track_count} tracks')
print('Output saved to: output_tracks.nk')