-
Notifications
You must be signed in to change notification settings - Fork 7
/
OgreMeshSerializerImpl.py
489 lines (396 loc) · 22 KB
/
OgreMeshSerializerImpl.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import bpy
import mathutils
import io;
from struct import unpack_from;
try:
from OgreSerializer import OgreSerializer
from OgreVertexIndexData import *
from OgreVertexBuffer import *
from OgreMeshFileFormat import *
from OgreHardwareBuffer import *
except ImportError as e:
directory = os.path.dirname(os.path.realpath(__file__));
print("Import error: " + str(e) + " manual compilation" );
srcfile="OgreSerializer.py"; exec(compile(open(os.path.join(directory,srcfile)).read(), srcfile, 'exec'))
srcfile="OgreVertexIndexData.py"; exec(compile(open(os.path.join(directory,srcfile)).read(), srcfile, 'exec'))
srcfile="OgreVertexBuffer.py"; exec(compile(open(os.path.join(directory,srcfile)).read(), srcfile, 'exec'))
srcfile="OgreMeshFileFormat.py"; exec(compile(open(os.path.join(directory,srcfile)).read(), srcfile, 'exec'))
srcfile="OgreHardwareBuffer.py"; exec(compile(open(os.path.join(directory,srcfile)).read(), srcfile, 'exec'))
class OgreMeshSerializerImpl(OgreSerializer):
"""
Internal implementation of Mesh reading / writing for the latest version of the
.mesh format.
@remarks
In order to maintain compatibility with older versions of the .mesh format, there
will be alternative subclasses of this class to load older versions, whilst this class
will remain to load the latest version.
@note
This mesh format was used from Ogre v1.10.
from: OgreMain/OgreMeshSerializerImpl.h
url: https://bitbucket.org/sinbad/ogre/src/0d580c7216abe27fafe41cb43e31d8ed86ded591/OgreMain/include/OgreMeshSerializerImpl.h
"""
MSTREAM_OVERHEAD_SIZE = 2 + 4;
def __init__(self):
OgreSerializer.__init__(self);
self._version = "[MeshSerializer_v1.100]";
def _readGeometryVertexElement(self,stream,mesh,dest, pstr_indent_lvl):
str_indent_lvl = pstr_indent_lvl + " ";
source = self._readUShorts(stream,1)[0]; #Buffer ogre correspondant ?
vType = self._readUShorts(stream,1)[0]; #Type de valeur
vSemantic = self._readUShorts(stream, 1)[0]; #Utilité du vertex
offset = self._readUShorts(stream, 1)[0]; #offset si les buffer sont entrelaces
index = self._readUShorts(stream, 1)[0]; #index du buffer utilise ?
dest.vertexDeclaration.addElement(source,offset,vType,vSemantic, index)
print(str_indent_lvl + "source: " + str(source));
print(str_indent_lvl + "type: " + OgreVertexElementType.toStr(vType));
print(str_indent_lvl + "semantic: " + OgreVertexElementSemantic.toStr(vSemantic));
print(str_indent_lvl + "offset: " + str(offset))
print(str_indent_lvl + "index: " + str(index));
def _readGeometryVertexDeclaration(self,stream,mesh,dest, pstr_indent_lvl):
self._pushInnerChunk(stream);
streamID = None;
eof = False;
str_indent_lvl = pstr_indent_lvl + " ";
try:
streamID = self._readChunk(stream);
#print(str_indent_lvl + "streamID " + "{0:#04x}".format(streamID) + " (offset: {0:#0x})".format(stream.tell()));
except EOFError as e:
eof = True;
while (not eof and streamID == OgreMeshChunkID.M_GEOMETRY_VERTEX_ELEMENT):
if (streamID == OgreMeshChunkID.M_GEOMETRY_VERTEX_ELEMENT):
print(str_indent_lvl + "M_GEOMETRY_VERTEX_ELEMENT");
self._readGeometryVertexElement(stream,mesh,dest,str_indent_lvl);
try:
streamID = self._readChunk(stream);
#print(str_indent_lvl + "streamID " + "{0:#0{1}x}".format(streamID,4) + " (offset: {0:#0x})".format(stream.tell()));
except EOFError as e:
eof = True;
if (not eof):
self._backpedalChunkHeader(stream);
self._popInnerChunk(stream);
def _readGeometryVertexBuffer(self, stream, mesh, dest, pstr_indent_lvl):
bindIndex = self._readUShorts(stream,1)[0];
vertexSize = self._readUShorts(stream,1)[0];
str_indent_lvl = pstr_indent_lvl + " ";
print(str_indent_lvl + "bind index: " + str(bindIndex));
print(str_indent_lvl + "vertex size: " + str(vertexSize));
self._pushInnerChunk(stream);
headerID = self._readChunk(stream);
if (headerID != OgreMeshChunkID.M_GEOMETRY_VERTEX_BUFFER_DATA):
raise ValueError("OgreMeshSerializerImpl._readGeometryVertexBuffer: Can't find vertex buffer data area");
#Check that vertex size agrees
if (dest.vertexDeclaration.getVertexSize(bindIndex) != vertexSize):
raise ValueError("OgreMeshSerializerImpl._readGeometryVertexBuffer: Buffer vertex size does not agree with vertex declarationBuffer vertex size does not agree with vertex declaration");
if (OgreSerializer.Endian.ENDIAN_NATIVE != self._endianness):
raise NotImplementedError("TODO Implement read data for endianness different of native endianness");
else:
hb = OgreVertexBuffer(vertexSize,dest.vertexCount);
hb.data = stream.read(dest.vertexCount*vertexSize);
print(str_indent_lvl + "buffered " + str(len(hb.data)) + " elements");
dest.vertexBufferBinding.setBinding(bindIndex,hb);
self._popInnerChunk(stream);
def _readGeometry(self, stream, mesh, dest, pstr_indent_lvl):
dest.vertexStart = 0;
dest.vertexCount = self._readUInts(stream,1)[0];
str_indent_lvl = pstr_indent_lvl + " ";
print(str_indent_lvl + "Vertex count: " + str(dest.vertexCount));
eof = False;
if (not eof):
self._pushInnerChunk(stream);
streamID = None;
try:
streamID = self._readChunk(stream);
print(str_indent_lvl + "streamID " + "{0:#0{1}x}".format(streamID,4) + " (offset: {0:#0x})".format(stream.tell()));
except EOFError as e:
eof = True;
while (not eof and \
(streamID == OgreMeshChunkID.M_GEOMETRY_VERTEX_DECLARATION or
streamID == OgreMeshChunkID.M_GEOMETRY_VERTEX_BUFFER)):
if (streamID == OgreMeshChunkID.M_GEOMETRY_VERTEX_DECLARATION):
print(str_indent_lvl + "M_GEOMETRY_VERTEX_DECLARATION");
self._readGeometryVertexDeclaration(stream,mesh,dest,str_indent_lvl);
elif (streamID == OgreMeshChunkID.M_GEOMETRY_VERTEX_BUFFER):
print(str_indent_lvl + "M_GEOMETRY_VERTEX_BUFFER");
self._readGeometryVertexBuffer(stream,mesh,dest,str_indent_lvl);
try:
streamID = self._readChunk(stream);
print(str_indent_lvl + "streamID " + "{0:#0{1}x}".format(streamID,4) + " (offset: {0:#0x})".format(stream.tell()));
except EOFError as e:
eof = True;
if (not eof):
self._backpedalChunkHeader(stream);
self._popInnerChunk(stream);
positionElements = dest.vertexDeclaration.findElementsBySemantic(OgreVertexElementSemantic.VES_POSITION);
assert(len(positionElements)==1); #FIXME: I don't know how to handle multiple position for now
pe=positionElements[0];
assert(OgreVertexElement.getTypeCount(pe.getType()) == 3); # if != 3 cannot be interpreted like a position
pe.extractFromBuffer(dest.vertexBufferBinding, dest.positions, self.endianness);
def _readSubMeshOperation(self, stream, mesh, submesh, pstr_indent_lvl):
str_indent_lvl = pstr_indent_lvl + " ";
opType = self._readShorts(stream,1)[0];
print(str_indent_lvl + "Operation type: " + str(opType));
if (opType != 4):
raise NotImplementedError("TODO implement different modes other than OP_TRIANGLE_LIST");
def _readSubMesh(self,stream, mesh, listener):
streamID = None;
eof = False;
str_indent_lvl = " ";
submesh = mesh.createSubMesh();
submesh.materialName = OgreSerializer.readString(stream);
submesh.useSharedVertices = self._readBools(stream,1)[0];
submesh.indexData.indexStart = 0; #always 0 because we have a separate buffer so this variable is useless
submesh.indexData.indexCount = self._readUInts(stream,1)[0];
print(str_indent_lvl + "Material name: " + submesh.materialName);
print(str_indent_lvl + "Use shared vertices: " + str(submesh.useSharedVertices));
#print(str_indent_lvl + "Index start: " + str(submesh.indexData.indexStart));
print(str_indent_lvl + "Index count: " + str(submesh.indexData.indexCount));
idx32bit = self._readBools(stream,1)[0];
print(str_indent_lvl + "Index 32 bits: " + str(idx32bit));
if (submesh.indexData.indexCount > 0):
if (idx32bit==True):
submesh.indexData.data = self._readUInts(stream,submesh.indexData.indexCount);
else:
submesh.indexData.data = self._readUShorts(stream,submesh.indexData.indexCount);
self._pushInnerChunk(stream);
if (not submesh.useSharedVertices):
try:
streamID = self._readChunk(stream);
print(str_indent_lvl + "streamID " + "{0:#0{1}x}".format(streamID,4) + " (offset: {0:#0x})".format(stream.tell()));
except EOFError as e:
eof = True;
if (streamID != OgreMeshChunkID.M_GEOMETRY):
raise ValueError("OgreMeshSerializerImpl._readSubMesh: Missing geometry data in mesh file");
submesh.vertexData = OgreVertexData();
print(str_indent_lvl + "M_GEOMETRY");
self._readGeometry(stream,mesh,submesh.vertexData,str_indent_lvl);
print("Warning: I never tested the behavior with this configuration");
#raise NotImplementedError("TODO Implement support for submesh not using sharedVertexData"); #Don't support this at the moment I hop it's not commonly used
try:
streamID = self._readChunk(stream);
#print(str_indent_lvl + "streamID " + "{0:#0{1}x}".format(streamID,4) + " (offset: {0:#0x})".format(stream.tell()));
except EOFError as e:
eof = True;
while (not eof and \
(streamID == OgreMeshChunkID.M_SUBMESH_OPERATION) or
(streamID == OgreMeshChunkID.M_SUBMESH_BONE_ASSIGNMENT) or
(streamID == OgreMeshChunkID.M_SUBMESH_TEXTURE_ALIAS)):
if (streamID == OgreMeshChunkID.M_SUBMESH_OPERATION):
print(str_indent_lvl + "M_SUBMESH_OPERATION");
self._readSubMeshOperation(stream,mesh,submesh,str_indent_lvl);
elif (streamID == OgreMeshChunkID.M_SUBMESH_BONE_ASSIGNMENT):
print(str_indent_lvl + "M_SUBMESH_BONE_ASSIGNMENT");
raise NotImplementedError("TODO implement M_SUBMESH_BONE_ASSIGNMENT");
elif (streamID == OgreMeshChunkID.M_SUBMESH_TEXTURE_ALIAS):
print(str_indent_lvl + "M_SUBMESH_TEXTURE_ALIAS");
raise NotImplementedError("TODO implement M_SUBMESH_TEXTURE_ALIAS");
try:
streamID = self._readChunk(stream);
#print(str_indent_lvl + "streamID " + "{0:#0{1}x}".format(streamID,4) + " (offset: {0:#0x})".format(stream.tell()));
except EOFError as e:
eof = True;
if (not eof):
self._backpedalChunkHeader(stream);
self._popInnerChunk(stream);
submesh.indexData.indexes = [];
for i in range(0,submesh.indexData.indexCount,3):
idx = (int(submesh.indexData.data[i]), int(submesh.indexData.data[i+1]), int(submesh.indexData.data[i+2]));
submesh.indexData.indexes.append(idx);
print(str_indent_lvl + "Vertices: " + str(len(submesh.vertexData.positions)));
print(str_indent_lvl + "Faces: " + str(len(submesh.indexData.indexes)));
mesh.blender_mesh.from_pydata(submesh.vertexData.positions, [], submesh.indexData.indexes);
def _readSkeletonLink(self,stream,mesh,listener):
str_indent_lvl = " ";
mesh.skeletonName = OgreSerializer.readString(stream);
print(str_indent_lvl + "Skeleton Name: " + mesh.skeletonName);
if (listener is not None):
listener.processSkeletonName(mesh,name);
def _readMeshBoneAssignment(self,stream, mesh):
str_indent_lvl = " ";
vertexIndex = self._readUInts(stream,1)[0];
boneIndex = self._readShorts(stream,1)[0];
weight = self._readFloats(stream,1)[0];
#So much lines in debug
#print(str_indent_lvl + "Vertex index: " + str(vertexIndex));
#print(str_indent_lvl + "Bone index: " + str(boneIndex));
#print(str_indent_lvl + "Weight: " + str(weight));
def _readBoundsInfos(self, stream, mesh):
str_indent_lvl = " ";
minBound = self._readVector3(stream);
maxBound = self._readVector3(stream);
radius = self._readFloats(stream,1)[0];
print(str_indent_lvl + "Min: " + str(minBound));
print(str_indent_lvl + "Max: " + str(maxBound));
print(str_indent_lvl + "Radius: " + str(radius));
def _readSubMeshNameTable(self,stream, mesh):
str_indent_lvl = " ";
streamID = None;
subMeshIndex = None;
eof = False;
subMeshNames = {};
self._pushInnerChunk(stream);
try:
streamID = self._readChunk(stream);
#print(str_indent_lvl + "streamID " + "{0:#0{1}x}".format(streamID,4) + " (offset: {0:#0x})".format(stream.tell()));
except EOFError as e:
eof = True;
while (not eof and (streamID == OgreMeshChunkID.M_SUBMESH_NAME_TABLE_ELEMENT)):
subMeshIndex = self._readUShorts(stream,1)[0];
subMeshNames[subMeshIndex] = OgreSerializer.readString(stream);
print(str_indent_lvl + str(subMeshIndex) + ": " + subMeshNames[subMeshIndex]);
try:
streamID = self._readChunk(stream);
#print(str_indent_lvl + "streamID " + "{0:#0{1}x}".format(streamID,4) + " (offset: {0:#0x})".format(stream.tell()));
except EOFError as e:
eof = True;
if (not eof):
self._backpedalChunkHeader(stream);
self._popInnerChunk(stream);
def _readEdgetListLodInfo(self,stream,edgeData):
str_indent_lvl = " ";
isClosed = self._readBools(stream,1)[0];
numTriangles = self._readUInts(stream,1)[0];
numEdgeGroups = self._readUInts(stream,1)[0];
print(str_indent_lvl + "Closed: " + str(isClosed));
print(str_indent_lvl + "Triangles: " + str(numTriangles));
print(str_indent_lvl + "Edge groups: " + str(numEdgeGroups));
#stream.seek(numTriangles * (8 * 4 + 4 * 4), io.SEEK_SET);
self._readUInts(stream, numTriangles * 8);
self._readFloats(stream, numTriangles * 4);
self._pushInnerChunk(stream);
for eg in range(numEdgeGroups):
streamID = self._readChunk(stream);
if (streamID != OgreMeshChunkID.M_EDGE_GROUP):
raise ValueError("OgreMeshSerializerImpl._readEdgetListLodInfo: Missing M_EDGE_GROUP stream");
self._readUInts(stream,3);
numEdges = self._readUInts(stream,1)[0];
#stream.seek(numEdges * (6 * 4 + 1));
self._readUInts(stream,numEdges*6);
self._readBools(stream, numEdges);
self._popInnerChunk(stream);
def _readEdgeList(self,stream,mesh):
str_indent_lvl = " ";
estr_indent_lvl = str_indent_lvl + " ";
eof = False;
streamID = None;
self._pushInnerChunk(stream);
try:
streamID = self._readChunk(stream);
print(str_indent_lvl + "streamID " + "{0:#0{1}x}".format(streamID,4) + " (offset: {0:#0x})".format(stream.tell()) + " (size: " + str(self._currentstreamLen) + ")");
except EOFError as e:
eof = True;
while (not eof and streamID==OgreMeshChunkID.M_EDGE_LIST_LOD):
print (str_indent_lvl + "M_EDGE_LIST_LOD");
lodIndex = self._readUShorts(stream, 1)[0];
isManual = self._readBools(stream,1)[0];
print(estr_indent_lvl + "LOD Index: " + str(lodIndex));
print(estr_indent_lvl + "Manual: " + str(isManual));
if (not isManual):
self._readEdgetListLodInfo(stream,None);
try:
streamID = self._readChunk(stream);
except EOFError:
eof = True;
if (not eof):
self._backpedalChunkHeader(stream);
self._popInnerChunk(stream);
def _readMesh(self, stream, mesh, listener):
skeletallyAnimated = self._readBools(stream,1)[0];
print("Skeletally animated: " + str(skeletallyAnimated));
eof = False;
str_indent_lvl = " ";
while (not eof):
self._pushInnerChunk(stream);
streamID=None;
try:
streamID = self._readChunk(stream);
#print(str_indent_lvl + "streamID " + "{0:#0{1}x}".format(streamID,4) + " (offset: {0:#0x})".format(stream.tell()));
except EOFError as e:
eof = True;
while (not eof and \
(streamID == OgreMeshChunkID.M_GEOMETRY or \
streamID == OgreMeshChunkID.M_SUBMESH or \
streamID == OgreMeshChunkID.M_MESH_SKELETON_LINK or \
streamID == OgreMeshChunkID.M_MESH_BONE_ASSIGNMENT or \
streamID == OgreMeshChunkID.M_MESH_LOD_LEVEL or \
streamID == OgreMeshChunkID.M_MESH_BOUNDS or \
streamID == OgreMeshChunkID.M_SUBMESH_NAME_TABLE or \
streamID == OgreMeshChunkID.M_EDGE_LISTS or \
streamID == OgreMeshChunkID.M_POSES or \
streamID == OgreMeshChunkID.M_ANIMATIONS or \
streamID == OgreMeshChunkID.M_TABLE_EXTREMES)):
if (streamID==OgreMeshChunkID.M_GEOMETRY):
print(str_indent_lvl + "M_GEOMETRY");
mesh.sharedVertexData = OgreVertexData();
self._readGeometry(stream,mesh, mesh.sharedVertexData, str_indent_lvl);
elif (streamID==OgreMeshChunkID.M_SUBMESH):
print(str_indent_lvl + "M_SUBMESH");
self._readSubMesh(stream,mesh,listener);
elif (streamID==OgreMeshChunkID.M_MESH_SKELETON_LINK):
print(str_indent_lvl + "M_MESH_SKELETON_LINK");
self._readSkeletonLink(stream,mesh,listener);
elif (streamID==OgreMeshChunkID.M_MESH_BONE_ASSIGNMENT):
#print(str_indent_lvl + "M_MESH_BONE_ASSIGNMENT");
self._readMeshBoneAssignment(stream,mesh);
elif (streamID==OgreMeshChunkID.M_MESH_LOD_LEVEL):
print(str_indent_lvl + "M_MESH_LOD_LEVEL");
self._readMeshLodLevel(stream,mesh);
elif (streamID==OgreMeshChunkID.M_MESH_BOUNDS):
print(str_indent_lvl + "M_MESH_BOUNDS");
self._readBoundsInfos(stream,mesh);
elif (streamID==OgreMeshChunkID.M_SUBMESH_NAME_TABLE):
print(str_indent_lvl + "M_SUBMESH_NAME_TABLE");
self._readSubMeshNameTable(stream,mesh);
elif (streamID==OgreMeshChunkID.M_EDGE_LISTS):
print(str_indent_lvl + "M_EDGE_LISTS");
self._readEdgeList(stream,mesh);
elif (streamID==OgreMeshChunkID.M_POSES):
print(str_indent_lvl + "M_POSES");
raise NotImplementedError("TODO Implement: M_POSES");
self._readPoses(stream,mesh);
elif (streamID==OgreMeshChunkID.M_ANIMATIONS):
print(str_indent_lvl + "M_ANIMATIONS");
raise NotImplementedError("TODO Implement: M_ANIMATIONS");
self._readAnimations(stream,mesh);
elif (streamID==OgreMeshChunkID.M_TABLE_EXTREMES):
print(str_indent_lvl + "M_TABLE_EXTREMES");
raise NotImplementedError("TODO Implement: M_TABLE_EXTREMES");
self._readExtremes(stream,mesh);
try:
streamID = self._readChunk(stream);
#print(str_indent_lvl + "streamID " + "{0:#0{1}x}".format(streamID,4) + " (offset: {0:#0x})".format(stream.tell()));
except EOFError as e:
eof = True;
if (not eof):
self._backpedalChunkHeader(stream);
self._popInnerChunk(stream);
#If the mesh didn't link with any other objects of the scene create it
mesh.blender_mesh.update();
if (mesh.blender_object is None):
mesh.blender_object = bpy.data.objects.new(mesh.name,mesh.blender_mesh);
scene = bpy.context.scene;
scene.objects.link(mesh.blender_object);
def importMesh(self, stream, mesh, listener=None):
self._determineEndianness(stream);
self._readFileHeader(stream);
self._pushInnerChunk(stream);
streamID = self._readChunk(stream);
str_indent_lvl = "";
eof = False;
while (not eof):
#print(str_indent_lvl + "streamID " + "{0:#0{1}x}".format(streamID,4) + " (offset: {0:#0x})".format(stream.tell()));
if (streamID == OgreMeshChunkID.M_MESH):
print("M_MESH");
self._readMesh(stream, mesh, listener);
try:
streamID = self._readChunk(stream);
except EOFError as e:
print("Legit end of file (no more chunks) at " + str(stream.tell()));
eof = True;
class OgreMeshSerializerImpl_v1_8(OgreMeshSerializerImpl):
"""
Class for providing backwards-compatibility for loading version 1.8 of the .mesh format.
This mesh format was used from Ogre v1.8.
"""
def __init__(self):
OgreMeshSerializerImpl.__init__(self);
self._version = "[MeshSerializer_v1.8]";