-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathexport.py
203 lines (173 loc) · 5.76 KB
/
export.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
202
203
from io import BytesIO
from .utils import BinaryReader
class OBJVector2:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def read(self, buf):
self.x = buf.read_float()
self.y = buf.read_float()
return self
def __str__(self):
return "%s %s" % (self.x, 1 - self.y)
class OBJVector3(OBJVector2):
def __init__(self, x=0, y=0, z=0):
super().__init__(x, y)
self.z = z
def read(self, buf):
super().read(buf)
self.z = buf.read_float()
return self
def __str__(self):
return "%s %s %s" % (-self.x, self.y, self.z)
class OBJVector4(OBJVector3):
def __init__(self, x=0, y=0, z=0, w=0):
super().__init__(x, y, z)
self.w = w
def read(self, buf):
super().read(buf)
self.w = buf.read_float()
return self
def read_color(self, buf):
self.x = buf.read_ubyte()
self.y = buf.read_ubyte()
self.z = buf.read_ubyte()
self.w = buf.read_ubyte()
return self
def __str__(self):
return "%s %s %s %s" % (self.x, self.y, self.z, self.w)
class MeshData:
def __init__(self, mesh):
self.mesh = mesh
self.indices = []
self.triangles = []
self.vertices = []
self.normals = []
self.colors = []
self.uv1 = []
self.uv2 = []
self.uv3 = []
self.uv4 = []
self.tangents = []
self.extract_indices()
self.extract_vertices()
def extract_indices(self):
for sub in self.mesh.submeshes:
sub_indices = []
sub_triangles = []
buf = BinaryReader(BytesIO(self.mesh.index_buffer))
buf.seek(sub.first_byte)
for i in range(0, sub.index_count):
sub_indices.append(buf.read_uint16())
if not sub.topology:
sub_triangles.extend(sub_indices)
else:
raise NotImplementedError("(%s) topologies are not supported" % (self.mesh.name))
self.indices.append(sub_indices)
self.triangles.append(sub_triangles)
def extract_vertices(self):
# unity 5+ has 8 channels (6 otherwise)
v5_channel_count = 8
# unity 2018 has 14 channels
v2018_channel_count = 14
buf = BinaryReader(BytesIO(self.mesh.vertex_data.data))
channels = self.mesh.vertex_data.channels
# actual streams attribute 'm_Streams' may only exist in unity 4,
# use of channel data alone seems to be sufficient
stream_count = self.get_num_streams(channels)
channel_count = len(channels)
for s in range(0, stream_count):
for i in range(0, self.mesh.vertex_data.vertex_count):
for j in range(0, channel_count):
ch = None
if channel_count > 0:
ch = channels[j]
# format == 1, use half-floats (16 bit)
if ch["format"] == 1:
raise NotImplementedError("(%r) 16 bit floats are not supported" % (self.mesh))
# read the appropriate vertex value into the correct list
if ch and ch["dimension"] > 0 and ch["stream"] == s:
if j == 0:
self.vertices.append(OBJVector3().read(buf))
elif j == 1:
self.normals.append(OBJVector3().read(buf))
elif j == 2:
if channel_count == v2018_channel_count:
self.tangents.append(OBJVector4().read(buf))
else:
self.colors.append(OBJVector4().read_color(buf))
elif j == 3:
self.uv1.append(OBJVector2().read(buf))
elif j == 4:
self.uv2.append(OBJVector2().read(buf))
elif j == 5:
if channel_count == v5_channel_count || channel_count == v2018_channel_count:
self.uv3.append(OBJVector2().read(buf))
else:
self.tangents.append(OBJVector4().read(buf))
elif j == 6: # for unity 5+
self.uv4.append(OBJVector2().read(buf))
elif j == 7: # for unity 5+
if channel_count == v5_channel_count:
self.tangents.append(OBJVector4().read(buf))
# TODO investigate possible alignment here, after each stream
def get_num_streams(self, channels):
streams = []
# scan the channel's stream value for distinct entries
for c in channels:
if c["stream"] not in streams:
streams.append(c["stream"])
return len(streams)
class OBJMesh:
def __init__(self, mesh):
if mesh.mesh_compression:
# TODO handle compressed meshes
raise NotImplementedError("(%s) compressed meshes are not supported" % (mesh.name))
self.mesh_data = MeshData(mesh)
self.mesh = mesh
@staticmethod
def face_str(indices, coords, normals):
ret = ["f "]
for i in indices[::-1]:
ret.append(str(i + 1))
if coords or normals:
ret.append("/")
if coords:
ret.append(str(i + 1))
if normals:
ret.append("/")
ret.append(str(i + 1))
ret.append(" ")
ret.append("\n")
return "".join(ret)
def export(self):
ret = []
verts_per_face = 3
normals = self.mesh_data.normals
tex_coords = self.mesh_data.uv1
if not tex_coords:
tex_coords = self.mesh_data.uv2
for v in self.mesh_data.vertices:
ret.append("v %s\n" % (v))
for v in normals:
ret.append("vn %s\n" % (v))
for v in tex_coords:
ret.append("vt %s\n" % (v))
ret.append("\n")
# write group name and set smoothing to 1
ret.append("g %s\n" % (self.mesh.name))
ret.append("s 1\n")
sub_count = len(self.mesh.submeshes)
for i in range(0, sub_count):
if sub_count == 1:
ret.append("usemtl %s\n" % (self.mesh.name))
else:
ret.append("usemtl %s_%d\n" % (self.mesh.name, i))
face_tri = []
for t in self.mesh_data.triangles[i]:
face_tri.append(t)
if len(face_tri) == verts_per_face:
ret.append(self.face_str(face_tri, tex_coords, normals))
face_tri = []
ret.append("\n")
return "".join(ret)