forked from johnzero7/XNALaraMesh
-
Notifications
You must be signed in to change notification settings - Fork 6
/
read_ascii_xps.py
251 lines (202 loc) · 7.3 KB
/
read_ascii_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
import io
import ntpath
from . import ascii_ops
from . import xps_const
from . import xps_types
from mathutils import Vector
def readUvVert(file):
line = ascii_ops.readline(file)
values = ascii_ops.splitValues(line)
x = (ascii_ops.getFloat(values[0])) # X pos
y = (ascii_ops.getFloat(values[1])) # Y pos
coords = [x, y]
return coords
def readXYZ(file):
line = ascii_ops.readline(file)
values = ascii_ops.splitValues(line)
x = (ascii_ops.getFloat(values[0])) # X pos
y = (ascii_ops.getFloat(values[1])) # Y pos
z = (ascii_ops.getFloat(values[2])) # Z pos
coords = [x, y, z]
return coords
def fillArray(array, minLen, value):
# Complete the array with selected value
filled = array + [value] * (minLen - len(array))
return filled
def read4Float(file):
line = ascii_ops.readline(file)
values = ascii_ops.splitValues(line)
values = fillArray(values, 4, 0)
x = (ascii_ops.getFloat(values[0]))
y = (ascii_ops.getFloat(values[1]))
z = (ascii_ops.getFloat(values[2]))
w = (ascii_ops.getFloat(values[3]))
coords = [x, y, z, w]
return coords
def readBoneWeight(file):
line = ascii_ops.readline(file)
values = ascii_ops.splitValues(line)
values = fillArray(values, 4, 0)
weights = [ascii_ops.getFloat(val) for val in values]
return weights
def readBoneId(file):
line = ascii_ops.readline(file)
values = ascii_ops.splitValues(line)
values = fillArray(values, 4, 0)
ids = [ascii_ops.getInt(val) for val in values]
return ids
def read4Int(file):
line = ascii_ops.readline(file)
values = ascii_ops.splitValues(line)
values = fillArray(values, 4, 0)
r = ascii_ops.getInt(values[0])
g = ascii_ops.getInt(values[1])
b = ascii_ops.getInt(values[2])
a = ascii_ops.getInt(values[3])
vertexColor = [r, g, b, a]
return vertexColor
def readTriIdxs(file):
line = ascii_ops.readline(file)
values = ascii_ops.splitValues(line)
face1 = ascii_ops.getInt(values[0])
face2 = ascii_ops.getInt(values[1])
face3 = ascii_ops.getInt(values[2])
faceLoop = [face1, face2, face3]
return faceLoop
def readBones(file):
bones = []
# Bone Count
boneCount = ascii_ops.readInt(file)
for boneId in range(boneCount):
boneName = ascii_ops.readString(file)
parentId = ascii_ops.readInt(file)
coords = readXYZ(file)
xpsBone = xps_types.XpsBone(boneId, boneName, coords, parentId)
bones.append(xpsBone)
return bones
def readMeshes(file, hasBones):
meshes = []
meshCount = ascii_ops.readInt(file)
for meshId in range(meshCount):
# Name
meshName = ascii_ops.readString(file)
if not meshName:
meshName = 'xxx'
# print('Mesh Name:', meshName)
# uv Count
uvLayerCount = ascii_ops.readInt(file)
# Textures
textures = []
textureCount = ascii_ops.readInt(file)
for texId in range(textureCount):
textureFile = ntpath.basename(ascii_ops.readString(file))
# print('Texture file', textureFile)
uvLayerId = ascii_ops.readInt(file)
xpsTexture = xps_types.XpsTexture(texId, textureFile, uvLayerId)
textures.append(xpsTexture)
# Vertices
vertex = []
vertexCount = ascii_ops.readInt(file)
for vertexId in range(vertexCount):
coord = readXYZ(file)
normal = readXYZ(file)
vertexColor = read4Int(file)
uvs = []
for uvLayerId in range(uvLayerCount):
uvVert = readUvVert(file)
uvs.append(uvVert)
# if ????
# tangent????
# tangent = read4float(file)
boneWeights = []
if hasBones:
# if cero bones dont have weights to read
boneIdx = readBoneId(file)
boneWeight = readBoneWeight(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 = ascii_ops.readInt(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 readPoseFile(file):
return file.read()
def poseData(string):
poseData = {}
poseList = string.split('\n')
for bonePose in poseList:
if bonePose:
pose = bonePose.split(':')
boneName = pose[0]
dataList = fillArray(pose[1].split(), 9, 1)
rotDelta = Vector((
ascii_ops.getFloat(dataList[0]),
ascii_ops.getFloat(dataList[1]),
ascii_ops.getFloat(dataList[2])))
coordDelta = Vector((
ascii_ops.getFloat(dataList[3]),
ascii_ops.getFloat(dataList[4]),
ascii_ops.getFloat(dataList[5])))
scale = Vector((
ascii_ops.getFloat(dataList[6]),
ascii_ops.getFloat(dataList[7]),
ascii_ops.getFloat(dataList[8])))
bonePose = xps_types.XpsBonePose(
boneName, coordDelta, rotDelta, scale)
poseData[boneName] = bonePose
return poseData
def boneDictData(string):
boneDictRename = {}
boneDictRestore = {}
poseList = string.split('\n')
for bonePose in poseList:
if bonePose:
pose = bonePose.split(';')
if len(pose) == 2:
oldName, newName = pose
boneDictRename[oldName] = newName
boneDictRestore[newName] = oldName
return boneDictRename, boneDictRestore
def readIoStream(filename):
with open(filename, "r", encoding=xps_const.ENCODING_READ) as a_file:
ioStream = io.StringIO(a_file.read())
return ioStream
def readXpsModel(filename):
ioStream = readIoStream(filename)
# print('Reading Header')
# xpsHeader = readHeader(ioStream)
print('Reading Bones')
bones = readBones(ioStream)
hasBones = bool(bones)
print('Reading Meshes')
meshes = readMeshes(ioStream, hasBones)
xpsModelData = xps_types.XpsData(bones=bones, meshes=meshes)
return xpsModelData
def readXpsPose(filename):
ioStream = readIoStream(filename)
# print('Import Pose')
poseString = readPoseFile(ioStream)
bonesPose = poseData(poseString)
return bonesPose
def readBoneDict(filename):
ioStream = readIoStream(filename)
boneDictString = readPoseFile(ioStream)
boneDictRename, boneDictRestore = boneDictData(boneDictString)
return boneDictRename, boneDictRestore
if __name__ == "__main__":
readModelfilename = r'G:\3DModeling\XNALara\XNALara_XPS\data\TESTING2\Tekken\Tekken - Lili Bride\generic_item.mesh.ascii'
readPosefilename = r'G:\3DModeling\XNALara\XNALara_XPS\data\TESTING2\Tekken\Tekken - Lili Bride\Lili 1.pose'
print('----READ START----')
xpsData = readXpsModel(readModelfilename)
xpsData = readXpsPose(readPosefilename)
print('----READ END----')