-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj-mtl-importer.py
419 lines (332 loc) · 14.1 KB
/
obj-mtl-importer.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
#!/Applications/ParaView-5.8.1.app/Contents/bin/pvpython
# -----------------------------------------------------------------------------
# User configuration
# -----------------------------------------------------------------------------
# objToLoad = '/Users/seb/Documents/code/Web/vtk-js/Data/obj/ferrari-f1-race-car/ferrari-f1-race-car.obj'
# mtlToLoad = '/Users/seb/Documents/code/Web/vtk-js/Data/obj/ferrari-f1-race-car/ferrari-f1-race-car.mtl'
# objToLoad = '/Users/seb/Documents/code/Web/vtk-js/Data/obj/blskes-plane/blskes-plane.obj'
# mtlToLoad = '/Users/seb/Documents/code/Web/vtk-js/Data/obj/blskes-plane/blskes-plane.mtl'
# objToLoad = '/Users/seb/Documents/code/Web/vtk-js/Data/obj/mini-cooper/mini-cooper.obj'
# mtlToLoad = '/Users/seb/Documents/code/Web/vtk-js/Data/obj/mini-cooper/mini-cooper.mtl'
# objToLoad = '/Users/seb/Documents/code/Web/vtk-js/Data/obj/space-shuttle-orbiter/space-shuttle-orbiter.obj'
# mtlToLoad = '/Users/seb/Documents/code/Web/vtk-js/Data/obj/space-shuttle-orbiter/space-shuttle-orbiter.mtl'
# -----------------------------------------------------------------------------
from vtk import *
from paraview import simple
import os
import sys
import hashlib
import json
# -----------------------------------------------------------------------------
def nameRemap(name):
if 'Paneel' in name:
return '3204_Paneel_4Hoek_Definition1'
if 'Zijgevel' in name:
return 'seb_white_steel'
return name
# -----------------------------------------------------------------------------
# obj Parser
# -----------------------------------------------------------------------------
class OBJParser(object):
def __init__(self, objFilePath, splitMode = None):
self.splitOn = splitMode
self.pieces = [];
self.v = [];
self.vt = [];
self.vn = [];
self.f = [[]];
self.size = 0;
self.output = []
with open(objFilePath, "r", encoding="utf-8") as objLines:
for line in objLines:
self.parseLine(line.rstrip('\n'))
self.end();
@staticmethod
def createPoints(pythonArray):
pts = vtkPoints()
nbPoints = int(len(pythonArray) / 3)
pts.SetNumberOfPoints(nbPoints);
for i in range(nbPoints):
pts.SetPoint(i, pythonArray[(i * 3) + 0], pythonArray[(i * 3) + 1], pythonArray[(i * 3) + 2])
return pts
@staticmethod
def createCellArray(pythonArray, nbCells):
cellArray = vtkCellArray()
idx = 0
size = len(pythonArray)
while idx < size:
cellSize = pythonArray[idx]
cellArray.InsertNextCell(cellSize)
idx += 1
for i in range(cellSize):
cellArray.InsertCellPoint(int(pythonArray[idx]))
idx += 1
return cellArray
@staticmethod
def createFloatArray(name, nbComponents, pythonArray):
array = vtkFloatArray()
array.SetName(name)
array.SetNumberOfComponents(nbComponents)
array.SetNumberOfTuples(int(len(pythonArray) / nbComponents))
for i in range(len(pythonArray)):
array.SetValue(i, pythonArray[i])
return array
@staticmethod
def pushVector(src, srcOffset, dst, vectorSize):
for i in range(vectorSize):
dst.append(src[srcOffset + i])
@staticmethod
def faceMap(str):
idxs = [int(i) if len(i) else 1 for i in str.split('/')]
vertexIdx = int(idxs[0] - 1);
textCoordIdx = int(idxs[1] - 1) if len(idxs) > 1 else vertexIdx
vertexNormal = int(idxs[2] - 1) if len(idxs) > 2 else vertexIdx
return [vertexIdx, textCoordIdx, vertexNormal]
def parseLine(self, line):
if len(line.strip()) == 0 or line[0] == '#':
return
tokens = line.strip().split()
if tokens[0] == self.splitOn:
tokens.pop(0)
self.pieces.append(' '.join(tokens).strip())
self.f.append([])
self.size += 1;
elif tokens[0] == 'v':
self.v.append(float(tokens[1]))
self.v.append(float(tokens[2]))
self.v.append(float(tokens[3]))
elif tokens[0] == 'vt':
self.vt.append(float(tokens[1]))
self.vt.append(float(tokens[2]))
elif tokens[0] == 'vn':
self.vn.append(float(tokens[1]))
self.vn.append(float(tokens[2]))
self.vn.append(float(tokens[3]))
elif tokens[0] == 'f':
# Handle triangles for now
if self.size == 0:
self.size += 1
cells = self.f[self.size - 1];
tokens.pop(0)
size = len(tokens)
cells.append(size)
for i in range(size):
cells.append(OBJParser.faceMap(tokens[i]))
def end(self):
hasTcoords = True if len(self.vt) > 0 else False
hasNormals = True if len(self.vn) > 0 else False
if self.splitOn:
for idx in range(self.size):
ctMapping = {}
polydata = vtkPolyData()
pts = []
tc = []
normals = []
polys = []
nbCells = 0
polyIn = self.f[idx]
nbElems = len(polyIn)
offset = 0
while offset < nbElems:
cellSize = polyIn[offset]
nbCells += 1
polys.append(cellSize)
for pIdx in range(cellSize):
vIdx, tcIdx, nIdx = polyIn[offset + pIdx + 1]
key = '%d/%d/%d' % (vIdx, tcIdx, nIdx)
if key not in ctMapping:
ctMapping[key] = len(pts) / 3
OBJParser.pushVector(self.v, vIdx * 3, pts, 3)
if hasTcoords:
OBJParser.pushVector(self.vt, tcIdx * 2, tc, 2)
if hasNormals:
OBJParser.pushVector(self.vn, nIdx * 3, normals, 3)
polys.append(ctMapping[key])
offset += cellSize + 1;
polydata.SetPoints(OBJParser.createPoints(pts))
polydata.SetPolys(OBJParser.createCellArray(polys, nbCells))
if hasTcoords:
tcoords = OBJParser.createFloatArray('TextureCoordinates', 2, tc)
polydata.GetPointData().SetTCoords(tcoords);
if hasNormals:
normalsArray = OBJParser.createFloatArray('Normals', 3, normals)
polydata.GetPointData().SetNormals(normalsArray)
# register in output
self.output.append(polydata)
print(self.pieces[idx])
else:
polydata = vtkPolyData()
polydata.SetPoints(OBJParser.createPoints(self.v))
if hasTcoords and (len(self.v) / 3) == (len(self.vt) / 2):
tcoords = OBJParser.createFloatArray('TextureCoordinates', 2, self.vt)
polydata.GetPointData().SetTCoords(tcoords);
if hasNormals and (len(self.v) == len(self.vn)):
normalsArray = OBJParser.createFloatArray('Normals', 3, self.vn)
polydata.GetPointData().SetNormals(normalsArray)
polys = []
polyIn = self.f[0]
nbElems = len(polyIn)
offset = 0
nbCells = 0
while offset < nbElems:
cellSize = polyIn[offset]
nbCells += 1
polys.append(cellSize)
for pIdx in range(cellSize):
polys.append(polyIn[offset + pIdx + 1][0])
offset += cellSize + 1
polydata.SetPolys(OBJParser.createCellArray(polys, nbCells))
self.output.append(polydata)
# -----------------------------------------------------------------------------
# mtl Parser
# -----------------------------------------------------------------------------
def materialToSHA(mat):
keys = list(mat.keys())
keys.sort()
m = hashlib.md5()
for key in keys:
m.update(key.encode('utf-8'))
for token in mat[key]:
m.update(token.encode('utf-8'))
return m.hexdigest()
class MTLParser(object):
def __init__(self, mtlFilePath):
self.materials = {}
self.currentMaterial = None
self.textures = {}
self.baseDir = os.path.dirname(mtlFilePath)
self.reducedMaterialMap = {}
self.reverseReduceMap = {}
self.representationsParameters = {}
with open(mtlFilePath, "r", encoding="utf-8") as lines:
for line in lines:
self.parseLine(line.rstrip('\n'))
def parseLine(self, line):
if len(line.strip()) == 0 or line[0] == '#':
return
tokens = line.strip().split()
if tokens[0] == 'newmtl':
tokens.pop(0);
self.currentMaterial = ' '.join(tokens).strip()
elif self.currentMaterial:
if self.currentMaterial not in self.materials:
self.materials[self.currentMaterial] = {}
if len(tokens) > 1:
self.materials[self.currentMaterial][tokens[0]] = tokens[1:]
def reduceMaterialDefinitions(self):
for name in self.materials:
sha = materialToSHA(self.materials[name])
self.reducedMaterialMap[name] = sha
self.reverseReduceMap[sha] = name
print('Reducing materials from %s to %s' % (len(self.reducedMaterialMap), len(self.reverseReduceMap)))
def applyMaterialToRepresentation(self, name, representation):
self.representationsParameters[name] = {}
material = {}
if name in self.materials:
material = self.materials[name]
if name in self.reverseReduceMap:
material = self.materials[self.reverseReduceMap[name]]
if 'map_Kd' in material:
if name not in self.textures:
from paraview import servermanager
texture = servermanager._getPyProxy(servermanager.CreateProxy('textures', 'ImageTexture'))
texture.FileName = os.path.join(self.baseDir, material['map_Kd'][0])
self.textures[name] = texture
servermanager.Register(texture)
representation.Texture = self.textures[name]
if 'Ka' in material:
representation.AmbientColor = [float(n) for n in material['Ka']]
self.representationsParameters[name]['AmbientColor'] = [float(n) for n in material['Ka']]
if 'Ks' in material:
representation.SpecularColor = [float(v) for v in material['Ks']]
self.representationsParameters[name]['SpecularColor'] = [float(v) for v in material['Ks']]
if 'Kd' in material:
representation.DiffuseColor = [float(v) for v in material['Kd']]
self.representationsParameters[name]['DiffuseColor'] = [float(v) for v in material['Kd']]
if 'd' in material:
representation.Opacity = float(material['d'][0])
self.representationsParameters[name]['Opacity'] = float(material['d'][0])
if 'Ns' in material:
representation.SpecularPower = float(material['Ns'][0])
self.representationsParameters[name]['SpecularPower'] = float(material['Ns'][0])
if 'illum' in material:
representation.Ambient = 1.0 if 0 <= float(material['illum'][0]) else 0.0
representation.Diffuse = 1.0 if 1 <= float(material['illum'][0]) else 0.0
representation.Specular = 1.0 if 2 <= float(material['illum'][0]) else 0.0
self.representationsParameters[name]['Ambient'] = 1.0 if 0 <= float(material['illum'][0]) else 0.0
self.representationsParameters[name]['Diffuse'] = 1.0 if 1 <= float(material['illum'][0]) else 0.0
self.representationsParameters[name]['Specular'] = 1.0 if 2 <= float(material['illum'][0]) else 0.0
# else:
# representation.Ambient = 1.0
# representation.Diffuse = 1.0
# representation.Specular = 1.0
# -----------------------------------------------------------------------------
# Mesh writer builder
# -----------------------------------------------------------------------------
def writeMeshes(meshBaseDirectory, objReader, nameMapping = {}):
nameToFilePath = {}
writer = vtkXMLPolyDataWriter()
ext = '.vtp'
if not os.path.exists(meshBaseDirectory):
os.makedirs(meshBaseDirectory)
nbPieces = len(objReader.pieces)
dsList = {}
nameToKey = nameMapping
for idx in range(nbPieces):
name = objReader.pieces[idx]
if name not in nameToKey:
nameToKey[name] = name
# Gather polydata with same textures
for idx in range(nbPieces):
name = objReader.pieces[idx]
key = nameToKey[name]
if key not in dsList:
dsList[key] = []
dsList[key].append(objReader.output[idx])
# Write each dataset
idx = 0
size = len(dsList)
for name in dsList:
fullPath = os.path.join(meshBaseDirectory, '%s%s' % (name, ext))
merge = vtkAppendPolyData()
for block in dsList[name]:
merge.AddInputData(block)
merge.Update()
writer.SetInputData(merge.GetOutput(0))
writer.SetFileName(fullPath)
writer.Modified()
print('%d - %d/%d - %s => %s' % ((idx + 1), len(dsList[name]), size, name, fullPath))
writer.Write()
nameToFilePath[name] = fullPath
idx += 1
return nameToFilePath;
# -----------------------------------------------------------------------------
# Scene Loader
# -----------------------------------------------------------------------------
def loadScene(objFilePath, mtlFilePath):
mtlReader = MTLParser(mtlFilePath)
mtlReader.reduceMaterialDefinitions()
objReader = OBJParser(objFilePath, 'usemtl')
meshBaseDirectory = os.path.join(os.path.dirname(objFilePath), os.path.basename(objFilePath)[:-4])
# custom remap
mapToSha = {}
for key in mtlReader.reducedMaterialMap:
mapToSha[key] = mtlReader.reducedMaterialMap[nameRemap(key)]
meshMapping = writeMeshes(meshBaseDirectory, objReader, mapToSha)
for name in meshMapping:
source = simple.OpenDataFile(meshMapping[name], guiName=name)
rep = simple.Show(source)
mtlReader.applyMaterialToRepresentation(name, rep)
with open('%s/representations.json' % meshBaseDirectory, "w", encoding="utf-8") as text_file:
text_file.write(json.dumps(mtlReader.representationsParameters, indent=2, sort_keys=True))
simple.Render()
# -----------------------------------------------------------------------------
# Main
# -----------------------------------------------------------------------------
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: obj-mtl-importer /path/to/file.obj")
else:
objPath = sys.argv[1]
loadScene(objPath, '%s.mtl' % objPath[:-4])
simple.SaveState('%s-pv-state.pvsm' % objPath[:-4])