-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathian_reader.py
201 lines (138 loc) · 4.58 KB
/
ian_reader.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import os
import struct
import sys
import time
def main():
input_folder = None
output_folder = None
ian_files = []
try:
input_folder = sys.argv[1]
if len(sys.argv) > 2:
output_folder = sys.argv[2]
else:
output_folder = os.path.join(os.getcwd(), f"ian_export_{int(time.time())}")
log(f"No output folder specified; using path {output_folder}")
except Exception as e:
print(f"Usage: {sys.argv[0]} <input folder path> [<output folder path>]")
print(f'e.g. {sys.argv[0]} "./Incoming/pcobject/"')
exit()
if not os.path.exists(input_folder):
log(f'Specified input folder does not exist: "{input_folder}"')
exit()
log("Scanning input folder for .ian files...")
for root, directories, filenames in os.walk(input_folder):
for filename in filenames:
name, ext = os.path.splitext(filename)
if ext.lower() == ".ian":
filepath = os.path.join(root, filename)
ian_files.append(filepath)
if not ian_files:
log("No .ian files found")
exit()
i = 0
total = len(ian_files)
log(f"Found {total} .ian file(s)")
for filepath in ian_files:
# Create output path name (must be a better way of doing this...)
abs_input_folder = os.path.abspath(input_folder)
abs_filepath = os.path.abspath(filepath)
output_path = os.path.join(output_folder, abs_filepath[len(abs_input_folder):].strip(os.path.sep))
output_dir = os.path.dirname(output_path)
name, ext = os.path.splitext(output_path)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
log(f"Processing file {i+1}/{total}")
# Read .ian data
ian_data = parse_ian_file(filepath)
# Convert to .obj
ian_to_obj(ian_data, name + ".obj")
i += 1
log(f"Finished processing {i} .ian file(s)")
def parse_ian_file(filepath):
log(f'Reading file "{filepath}" ...')
faces = []
vertices = []
normals = []
uvs = []
with open(filepath, "rb") as f:
ian_data = f.read()
f.seek(0x14)
face_count = struct.unpack("<H", f.read(2))[0]
f.seek(2, 1)
vertex_count = struct.unpack("<H", f.read(2))[0]
f.seek(2, 1)
vertices_offset = struct.unpack("<H", f.read(2))[0]
f.seek(2, 1)
faces_offset = struct.unpack("<H", f.read(2))[0]
f.seek(2, 1)
log(f"Faces count : {face_count}")
log(f"Faces offset : {faces_offset}")
log(f"Vertices count : {vertex_count}")
log(f"Vertices offset : {vertices_offset}")
f.seek(0x44, 1)
unknown_end_data_count = struct.unpack("<H", f.read(2))[0]
f.seek(2, 1)
unknown_end_data_offset = struct.unpack("<H", f.read(2))[0]
f.seek(2, 1)
if unknown_end_data_offset > len(ian_data):
unknown_end_data_offset = vertices_offset + vertex_count * 32
f.seek(faces_offset)
log("Reading faces...")
for i in range(face_count):
f.seek(4, 1)
f1 = struct.unpack("<I", f.read(4))[0]
f.seek(4, 1)
f2 = struct.unpack("<I", f.read(4))[0]
f.seek(4, 1)
f3 = struct.unpack("<I", f.read(4))[0]
f.seek(4, 1)
faces.append((f1, f3, f2))
log("Done")
f.seek(vertices_offset)
log("Reading vertices, normals, and UVs...")
for i in range(vertex_count):
x = struct.unpack("<f", f.read(4))[0]
y = struct.unpack("<f", f.read(4))[0]
z = struct.unpack("<f", f.read(4))[0]
vertices.append((x, y, z))
n_1 = struct.unpack("<f", f.read(4))[0]
n_2 = struct.unpack("<f", f.read(4))[0]
n_3 = struct.unpack("<f", f.read(4))[0]
normals.append((n_1, n_2, n_3))
u = struct.unpack("<f", f.read(4))[0]
v = 1 - struct.unpack("<f", f.read(4))[0]
uvs.append((u, v))
log("Done")
return {
"faces" : faces,
"vertices" : vertices,
"normals" : normals,
"uvs" : uvs
}
def ian_to_obj(ian_data, output_path):
log("Creating .obj file...")
name, ext = os.path.splitext(os.path.basename(output_path))
obj = []
obj.append(f"o {name}")
for vertex in ian_data["vertices"]:
obj.append(f"v {vertex[0]} {vertex[1]} {vertex[2]}")
for uv in ian_data["uvs"]:
obj.append(f"vt {uv[0]} {uv[1]}")
for normal in ian_data["normals"]:
obj.append(f"vn {normal[0]} {normal[1]} {normal[2]}")
for face in ian_data["faces"]:
f1 = face[0] + 1
f2 = face[1] + 1
f3 = face[2] + 1
obj.append(f"f {f1}/{f1}/{f1} {f2}/{f2}/{f2} {f3}/{f3}/{f3}")
obj = "\n".join(obj)
with open(output_path, "w", encoding = "utf-8") as f:
f.write(obj)
log("Done")
log(f"Saved as {output_path}")
print()
def log(msg):
print(f'[{time.strftime("%Y-%m-%d %H:%M:%S")}] {msg}')
if __name__ == "__main__":
main()