-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctl2csv.py
66 lines (51 loc) · 2.07 KB
/
ctl2csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
import sys
import os
import csv
import FGBinTools
def print_usage():
print("Usage: python ctl2csv.py <input_ctl> <output_csv> [controls_file]")
print(" <input_ctl>: Path to input CTL file or directory containing CTL files")
print(" <output_csv>: Path to output CSV file or directory")
print(" [controls_file]: Optional. Path to file containing list of controls to convert")
def process_ctl(ctl_file, csv_file, controls=None):
print(f"Loading control vectors from {ctl_file}...")
ctl = FGBinTools.readCtl(ctl_file)
controlshapevec = {}
controltexvec = {}
for label, vector in ctl['GS']:
if not controls or label in controls:
print(f"+ Found: shape {label}")
controlshapevec[label] = vector
for label, vector in ctl['TS']:
if not controls or label in controls:
print(f"+ Found: texture {label}")
controltexvec[label] = vector
with open(csv_file, 'w', newline='') as csvfile:
csvWriter = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for label in sorted(controltexvec.keys()):
row = [label] + controlshapevec[label] + controltexvec[label]
csvWriter.writerow(row)
def main():
if len(sys.argv) < 3:
print_usage()
sys.exit(1)
input_path = sys.argv[1]
output_path = sys.argv[2]
controls_file = sys.argv[3] if len(sys.argv) > 3 else None
controls = None
if controls_file:
with open(controls_file, 'r') as f:
controls = [line.strip() for line in f]
if os.path.isdir(input_path):
os.makedirs(output_path, exist_ok=True)
for filename in os.listdir(input_path):
if filename.endswith('.ctl'):
ctl_file = os.path.join(input_path, filename)
csv_file = os.path.join(output_path, filename.replace('.ctl', '.csv'))
process_ctl(ctl_file, csv_file, controls)
else:
process_ctl(input_path, output_path, controls)
print("Done.")
if __name__ == "__main__":
main()