forked from johnzero7/XNALaraMesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_bin_xps.py
407 lines (328 loc) · 11.5 KB
/
read_bin_xps.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
# <pep8 compliant>
import io
import ntpath
from . import bin_ops
from . import read_ascii_xps
from . import xps_const
from . import xps_types
def flagName(flag):
flagList = {
0: xps_const.BACK_FACE_CULLING,
1: xps_const.ALWAYS_FORCE_CULLING,
2: xps_const.MODEL_CAST_SHADOWS,
3: xps_const.TANGENT_SPACE_RED,
4: xps_const.TANGENT_SPACE_GREEN,
5: xps_const.TANGENT_SPACE_BLUE,
6: xps_const.GLOSS,
7: xps_const.HAS_BONE_DIRECTIONS,
}
return flagList.get(flag, flag)
def flagsDefault():
flags = {
xps_const.BACK_FACE_CULLING: False,
xps_const.ALWAYS_FORCE_CULLING: False,
xps_const.MODEL_CAST_SHADOWS: True,
xps_const.TANGENT_SPACE_RED: 0, # Straight X channel
xps_const.TANGENT_SPACE_GREEN: 1, # Invert Y channel
xps_const.TANGENT_SPACE_BLUE: 0, # Straight Z channel
xps_const.GLOSS: 10,
xps_const.HAS_BONE_DIRECTIONS: False,
}
return flags
def flagValue(flag, value):
# Flags
# 00: Backface culling
# 01: Always force culling
# 02: Model cast shadows
# 06: Save current bump specular gloss
if flag in (0, 1, 2, 6, 7):
return bool(value)
# Flags
# 03: X space
# 04: Y space
# 05: Z space
elif flag in (3, 4, 5):
return (value % 2)
else:
return value
def intToCoords(flag):
flagValue = {
0: '+',
1: '-',
}
return flagValue.get(flag, 'Uk')
def printNormalMapSwizzel(tangentSpaceRed, tangentSpaceGreen, tangentSpaceBlue):
# Default XPS NormalMapTangentSpace == 0 1 0 == X+ Y- Z+
print('Tangent Space Normal Map Swizzel Coordinates:')
print('X{} Y{} Z{}'.format(intToCoords(tangentSpaceRed), intToCoords(tangentSpaceGreen), intToCoords(tangentSpaceBlue)))
print('')
def readFilesString(file):
lengthByte2 = 0
lengthByte1 = bin_ops.readByte(file)
if (lengthByte1 >= xps_const.LIMIT):
lengthByte2 = bin_ops.readByte(file)
length = (lengthByte1 % xps_const.LIMIT) + (lengthByte2 * xps_const.LIMIT)
string = bin_ops.readString(file, length)
return string
def readVertexColor(file):
r = bin_ops.readByte(file)
g = bin_ops.readByte(file)
b = bin_ops.readByte(file)
a = bin_ops.readByte(file)
vertexColor = [r, g, b, a]
return vertexColor
def readUvVert(file):
x = bin_ops.readSingle(file) # X pos
y = bin_ops.readSingle(file) # Y pos
coords = [x, y]
return coords
def readXYZ(file):
x = bin_ops.readSingle(file) # X pos
y = bin_ops.readSingle(file) # Y pos
z = bin_ops.readSingle(file) # Z pos
coords = [x, y, z]
return coords
def read4Float(file):
x = bin_ops.readSingle(file)
y = bin_ops.readSingle(file)
z = bin_ops.readSingle(file)
w = bin_ops.readSingle(file)
coords = [x, y, z, w]
return coords
def read4Int16(file):
r = bin_ops.readInt16(file)
g = bin_ops.readInt16(file)
b = bin_ops.readInt16(file)
a = bin_ops.readInt16(file)
vertexColor = [r, g, b, a]
return vertexColor
def readTriIdxs(file):
face1 = bin_ops.readUInt32(file)
face2 = bin_ops.readUInt32(file)
face3 = bin_ops.readUInt32(file)
faceLoop = [face1, face2, face3]
return faceLoop
def readHeader(file):
xpsHeader = xps_types.XpsHeader()
flags = flagsDefault()
# MagicNumber
magic_number = bin_ops.readUInt32(file)
# XPS Version
version_mayor = bin_ops.readUInt16(file)
version_minor = bin_ops.readUInt16(file)
# XNAaral Name
xna_aral = readFilesString(file)
# Settings Length
settingsLen = bin_ops.readUInt32(file)
# MachineName
machineName = readFilesString(file)
# UserName
userName = readFilesString(file)
# File-->File
filesString = readFilesString(file)
xpsPoseData = None
# print('*'*80)
hasTangent = bin_ops.hasTangentVersion(version_mayor, version_minor)
if (hasTangent):
# print('OLD Format')
settingsStream = io.BytesIO(file.read(settingsLen * 4))
else:
# print('NEW Format')
valuesRead = 0
hash = bin_ops.readUInt32(file)
valuesRead += 1 * 4
items = bin_ops.readUInt32(file)
valuesRead += 1 * 4
# print('hash', hash)
# print('items', items)
for i in range(items):
# print('valuesRead', valuesRead)
optType = bin_ops.readUInt32(file)
valuesRead += 1 * 4
optcount = bin_ops.readUInt32(file)
valuesRead += 1 * 4
optInfo = bin_ops.readUInt32(file)
valuesRead += 1 * 4
# print('------')
# print('count',i)
# print('optType',optType)
# print('optcount',optcount)
# print('optInfo',optInfo)
if (optType == 0):
# print('Read None')
readNone(file, optcount)
valuesRead += optcount * 2
elif (optType == 1):
# print('Read Pose')
xpsPoseData = readDefaultPose(file, optcount, optInfo)
readCount = bin_ops.roundToMultiple(optcount, xps_const.ROUND_MULTIPLE)
valuesRead += readCount
elif (optType == 2):
# print('Read Flags')
flags = readFlags(file, optcount)
valuesRead += optcount * 2 * 4
else:
# print('Read Waste')
loopStart = valuesRead // 4
loopFinish = settingsLen
# print (loopStart, loopFinish)
for j in range(loopStart, loopFinish):
# print('waste',j - loopStart)
waste = bin_ops.readUInt32(file)
xpsHeader.magic_number = magic_number
xpsHeader.version_mayor = version_mayor
xpsHeader.version_minor = version_minor
xpsHeader.xna_aral = xna_aral
xpsHeader.settingsLen = settingsLen
xpsHeader.machine = machineName
xpsHeader.user = userName
xpsHeader.files = filesString
xpsHeader.pose = xpsPoseData
xpsHeader.flags = flags
return xpsHeader
def findHeader(file):
header = None
# Check for MAGIC_NUMBER
number = bin_ops.readUInt32(file)
file.seek(0)
if (number == xps_const.MAGIC_NUMBER):
print('Header Found')
header = readHeader(file)
# logHeader(header)
return header
def readNone(file, optcount):
for i in range(optcount):
waste = bin_ops.readUInt32(file)
def readFlags(file, optcount):
flags = {}
for i in range(optcount):
flag = bin_ops.readUInt32(file)
value = bin_ops.readUInt32(file)
flags[flagName(flag)] = flagValue(flag, value)
printNormalMapSwizzel(flags[flagName(3)], flags[flagName(4)], flags[flagName(5)])
return flags
def logHeader(xpsHeader):
print("MAGIX:", xpsHeader.magic_number)
print('VER MAYOR:', xpsHeader.version_mayor)
print('VER MINOR:', xpsHeader.version_minor)
print('NAME:', xpsHeader.xna_aral)
print('SETTINGS LEN:', xpsHeader.settingsLen)
print('MACHINE:', xpsHeader.machine)
print('USR:', xpsHeader.user)
print('FILES:', xpsHeader.files)
print('SETTING:', xpsHeader.settings)
print('DEFAULT POSE:', xpsHeader.pose)
def readBones(file, header):
bones = []
# Bone Count
boneCount = bin_ops.readUInt32(file)
for boneId in range(boneCount):
boneName = readFilesString(file)
parentId = bin_ops.readInt16(file)
coords = readXYZ(file)
xpsBone = xps_types.XpsBone(boneId, boneName, coords, parentId)
bones.append(xpsBone)
return bones
def readMeshes(file, xpsHeader, hasBones):
meshes = []
meshCount = bin_ops.readUInt32(file)
hasHeader = bool(xpsHeader)
verMayor = xpsHeader.version_mayor if hasHeader else 0
verMinor = xpsHeader.version_minor if hasHeader else 0
hasTangent = bin_ops.hasTangentVersion(verMayor, verMinor, hasHeader)
hasVariableWeights = bin_ops.hasVariableWeights(verMayor, verMinor, hasHeader)
for meshId in range(meshCount):
# Name
meshName = readFilesString(file)
if not meshName:
meshName = 'unnamed'
# print('Mesh Name:', meshName)
# uv Count
uvLayerCount = bin_ops.readUInt32(file)
# Textures
textures = []
textureCount = bin_ops.readUInt32(file)
for texId in range(textureCount):
textureFile = ntpath.basename(readFilesString(file))
# print('Texture file', textureFile)
uvLayerId = bin_ops.readUInt32(file)
xpsTexture = xps_types.XpsTexture(texId, textureFile, uvLayerId)
textures.append(xpsTexture)
# Vertices
vertex = []
vertexCount = bin_ops.readUInt32(file)
for vertexId in range(vertexCount):
coord = readXYZ(file)
normal = readXYZ(file)
vertexColor = readVertexColor(file)
uvs = []
for uvLayerId in range(uvLayerCount):
uvVert = readUvVert(file)
uvs.append(uvVert)
if hasTangent:
tangent = read4Float(file)
boneWeights = []
if hasBones:
# if cero bones dont have weights to read
boneIdx = []
boneWeight = []
if hasVariableWeights:
weightsCount = bin_ops.readInt16(file)
else:
weightsCount = 4
for x in range(weightsCount):
boneIdx.append(bin_ops.readInt16(file))
for x in range(weightsCount):
boneWeight.append(bin_ops.readSingle(file))
for idx in range(len(boneIdx)):
boneWeights.append(
xps_types.BoneWeight(boneIdx[idx], boneWeight[idx]))
xpsVertex = xps_types.XpsVertex(
vertexId, coord, normal, vertexColor, uvs, boneWeights)
vertex.append(xpsVertex)
# Faces
faces = []
triCount = bin_ops.readUInt32(file)
for i in range(triCount):
triIdxs = readTriIdxs(file)
faces.append(triIdxs)
xpsMesh = xps_types.XpsMesh(
meshName, textures, vertex, faces, uvLayerCount)
meshes.append(xpsMesh)
return meshes
def readIoStream(filename):
with open(filename, "rb") as a_file:
ioStream = io.BytesIO(a_file.read())
return ioStream
def readXpsModel(filename):
print('File:', filename)
ioStream = readIoStream(filename)
print('Reading Header')
xpsHeader = findHeader(ioStream)
print('Reading Bones')
bones = readBones(ioStream, xpsHeader)
hasBones = bool(bones)
print('Read', len(bones), 'Bones')
print('Reading Meshes')
meshes = readMeshes(ioStream, xpsHeader, hasBones)
print('Read', len(meshes), 'Meshes')
xpsData = xps_types.XpsData(xpsHeader, bones, meshes)
return xpsData
def readDefaultPose(file, poseLenghtUnround, poseBones):
# print('Import Pose')
poseBytes = b''
if poseLenghtUnround:
for i in range(0, poseBones):
poseBytes += file.readline()
poseLenght = bin_ops.roundToMultiple(
poseLenghtUnround, xps_const.ROUND_MULTIPLE)
emptyBytes = poseLenght - poseLenghtUnround
file.read(emptyBytes)
poseString = bin_ops.decodeBytes(poseBytes)
bonesPose = read_ascii_xps.poseData(poseString)
return bonesPose
if __name__ == "__main__":
readfilename = r'G:\3DModeling\XNALara\XNALara_XPS\Young Samus\Generic_Item.mesh'
print('----READ START----')
xpsData = readXpsModel(readfilename)
print('----READ END----')