-
Notifications
You must be signed in to change notification settings - Fork 3
/
sgm2obj.py
143 lines (130 loc) · 6.44 KB
/
sgm2obj.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
import struct, os, argparse
def main():
parser = argparse.ArgumentParser(description='Convert SGM to OBJ format')
parser.add_argument('input_file', type=str, help='path to input file')
parser.add_argument('output_file', nargs='?', type=str, help='path to output file')
parser.add_argument('--texture', type=str, help='name of texture file if there is only one texture')
args = parser.parse_args()
input_file = args.input_file
output_file = args.output_file
if args.output_file is None:
output_file = f'{os.path.splitext(args.input_file)[0]}.obj'
else:
output_file = args.output_file
texture = args.texture
data = read_sgm(input_file)
write_obj(data[0], data[1], output_file, texture)
def read_sgm(filename):
with open(filename, "rb") as file:
version = struct.unpack('<L B', file.read(5))
print(version)
num_materials = struct.unpack('<B', file.read(1))[0]
materials = []
for _ in range(num_materials):
material_id = struct.unpack('<B', file.read(1))[0]
uv_count = struct.unpack('<B', file.read(1))[0]
uv_data = []
for _ in range(uv_count):
image_count = struct.unpack('<B', file.read(1))[0]
images = []
for _ in range(image_count):
usage_hint = struct.unpack('<B', file.read(1))[0]
texname_len = struct.unpack('<H', file.read(2))[0] - 1
texname = struct.unpack(f'<{texname_len}s', file.read(texname_len))[0].decode("utf_8")
file.seek(1, 1) # skip null terminator
images.append((texname, usage_hint))
uv_data.append(images)
color_count = struct.unpack('<B', file.read(1))[0]
colors = []
for _ in range(color_count):
color_id = struct.unpack('<B', file.read(1))[0]
color = struct.unpack('<ffff', file.read(16))
colors.append((color, color_id))
materials.append({
'material_id': material_id,
'uv_data': uv_data,
'colors': colors
})
num_meshes = struct.unpack('<B', file.read(1))[0]
meshes = []
index_offset = 0 # for multiple meshes
for _ in range(num_meshes):
vertices = []
indices = []
mesh_id = struct.unpack('<B', file.read(1))[0]
material_id = struct.unpack('<B', file.read(1))[0]
vertex_count = struct.unpack('<I', file.read(4))[0]
uv_count = struct.unpack('<B', file.read(1))[0]
texdata_count = struct.unpack('<B', file.read(1))[0]
has_tangents = struct.unpack('<B', file.read(1))[0]
has_bones = struct.unpack('<B', file.read(1))[0]
for _ in range(vertex_count):
position = struct.unpack('<fff', file.read(12))
normal = struct.unpack('<fff', file.read(12))
uvs = []
for _ in range(uv_count):
uv = struct.unpack('<ff', file.read(8))
uvs.append(uv)
color = None
if texdata_count == 4:
color = struct.unpack('<ffff', file.read(16))
tangent = None
if has_tangents:
tangent = struct.unpack('<ffff', file.read(16))
weights = None
bones = None
if has_bones:
weights = struct.unpack('<ffff', file.read(16))
bones = struct.unpack('<ffff', file.read(16))
vertices.append((position, normal, uvs, color, tangent, weights, bones))
index_count = struct.unpack('<I', file.read(4))[0]
index_size = struct.unpack('<B', file.read(1))[0]
for _ in range(index_count):
if index_size == 4:
index = struct.unpack('<I', file.read(4))[0]
else:
index = struct.unpack('<H', file.read(2))[0]
indices.append(index + index_offset)
index_offset += len(vertices)
meshes.append({"mesh_id": mesh_id, "material_id": material_id, "vertices": vertices, "indices": indices})
return [meshes, materials]
def write_obj(meshes, materials, filename, texturename):
mtl_filename = f"{os.path.splitext(filename)[0]}.mtl"
with open(mtl_filename, 'w') as mtl_file:
for i,m in enumerate(materials):
material_id = m["material_id"]
mtl_file.write(f"newmtl {material_id}\n")
color = m["colors"][0]
r, g, b, a = color[0]
mtl_file.write(f"Kd {r} {g} {b}\n")
mtl_file.write(f"d {a}\n")
if len(meshes[i]["vertices"][0][2]) > 0:
if texturename is None:
for uv_images in m["uv_data"]:
for texname, _ in uv_images:
mtl_file.write(f"map_Kd {texname}\n")
else:
mtl_file.write(f"map_Kd {texturename}\n")
with open(filename, 'w') as f:
f.write(f'mtllib {os.path.basename(mtl_filename)}\n')
for (i,m) in enumerate(meshes):
print(f"Working on mesh {i}")
f.write(f"o {m['mesh_id']}\n")
f.write(f'usemtl {m["material_id"]}\n')
vertices = m["vertices"]
indices = m["indices"]
for v in vertices:
f.write(f'v {v[0][0]} {v[0][1]} {v[0][2]}\n')
f.write(f'vn {v[1][0]} {v[1][1]} {v[1][2]}\n')
if v[2] is None or len(v[2]) == 0:
print(f"no uv: {v}")
f.write(f'vt 0 0\n')
else:
f.write(f'vt {v[2][0][0]} {1-v[2][0][1]}\n')
for i in range(0, len(indices), 3):
if len(m["vertices"][0][2]) > 0:
f.write(f'f {indices[i] + 1}/{indices[i] + 1}/{indices[i] + 1} {indices[i + 1] + 1}/{indices[i + 1] + 1}/{indices[i + 1] + 1} {indices[i + 2] + 1}/{indices[i + 2] + 1}/{indices[i + 2] + 1}\n')
else:
f.write(f'f {indices[i] + 1}//{indices[i] + 1} {indices[i + 1] + 1}//{indices[i + 1] + 1} {indices[i + 2] + 1}//{indices[i + 2] + 1}\n')
if __name__ == '__main__':
main()