-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobj_to_ucm.py
executable file
·65 lines (58 loc) · 2.24 KB
/
obj_to_ucm.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
#!/usr/bin/env python3
import argparse
import numpy as np
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Create a .ucm model from Wavefront .obj file.')
parser.add_argument('obj', type=str,
help='source model')
parser.add_argument('ucm', type=str,
help='conversion result')
parser.add_argument('name', type=str,
help='model name')
args = parser.parse_args()
with open(args.obj, 'r') as f:
lines = f.readlines()
indices = np.ndarray((0, 3), np.uint32)
vs = np.ndarray((0, 3), np.float32)
vts = np.ndarray((0, 2), np.float32)
vns = np.ndarray((0, 3), np.float32)
kostyl = []
vertices = np.ndarray((0, 8), np.float32)
for line in lines:
if line.startswith('v '):
vs = np.vstack([vs, np.asarray(line.split()[1:], dtype=np.float32)])
elif line.startswith('vt '):
uv = np.asarray(line.split()[1:], dtype=np.float32)
uv[1] *= -1
vts = np.vstack([vts, uv])
elif line.startswith('vn '):
vns = np.vstack([vns, np.asarray(line.split()[1:], dtype=np.float32)])
elif line.startswith('f '):
verts = line.split()[1:]
ind = np.ndarray((1, 3), np.uint32)
for i in range(3):
v, vt, vn = map(lambda x: int(x) - 1, verts[i].split('/'))
try:
idx = kostyl.index((v, vt, vn))
except ValueError:
kostyl.append((v, vt, vn))
idx = len(kostyl) - 1
ind[0][i] = idx
indices = np.vstack([indices, ind])
for vert in kostyl:
vertex = np.concatenate((vs[vert[0]], vns[vert[2]], vts[vert[1]]))
print(vertex)
vertices = np.vstack([vertices, vertex])
final = b''.join((
b'UCM1\x02\x00\x00\x00',
args.name.encode().ljust(0x48 - 0x08, b'\x00'),
b'\x01\x00\x00\x00',
(indices.size // 3).to_bytes(4, byteorder='little'),
(vertices.size // 8).to_bytes(4, byteorder='little'),
b'\x00' * 8,
indices.tobytes(),
vertices.tobytes(),
))
with open(args.ucm, 'wb') as f:
f.write(final)