-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj_converter.py
executable file
·80 lines (66 loc) · 2.06 KB
/
obj_converter.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
#!/usr/bin/env python
import sys
import argparse
parser = argparse.ArgumentParser(
description='Tool for converting .obj files into format consumable by worldJS.')
parser.add_argument('input_file', type=argparse.FileType('r'))
parser.add_argument('-o', '--output_file',
dest='output_file', type=argparse.FileType('w'))
parser.add_argument('-n', '--name',
dest='name')
args = parser.parse_args()
input_file = args.input_file
output_file = args.output_file
name = args.name
coordinateLines = []
textureLines = []
normalLines = []
faces = []
for line in input_file:
tokens = line.strip().split(' ');
if tokens[0] == 'v':
coordinateLines.append(map(float, tokens[1:]))
if tokens[0] == 'vt':
textureLines.append(map(float, tokens[1:]))
if tokens[0] == 'vn':
normalLines.append(map(float, tokens[1:]))
if tokens[0] == 'f':
faces.append(tokens[1:]);
input_file.close()
coordinates = []
textureCoordinates = []
normals = []
def addVertex(vertex):
indices = vertex.split('/')
coordinateIndex = int(indices[0]) - 1
coordinates.extend(coordinateLines[coordinateIndex])
# some models don't have a middle texture index
if (indices[1]):
textureIndex = int(indices[1]) - 1
textureCoordinates.extend(textureLines[textureIndex])
else:
textureCoordinates.extend([0, 0, 0])
normalIndex = int(indices[2]) - 1
normals.extend(normalLines[normalIndex])
for face in faces:
if len(face) == 3:
addVertex(face[0]);
addVertex(face[1]);
addVertex(face[2]);
elif len(face) == 4:
addVertex(face[0]);
addVertex(face[1]);
addVertex(face[2]);
addVertex(face[2]);
addVertex(face[3]);
addVertex(face[0]);
output_file = args.output_file
output_file.write('goog.provide(\'%s\');' % name);
output_file.write('\n');
output_file.write('%s = {\n' % name)
output_file.write(' type: \'%s\',\n' % name)
output_file.write(' vertexCoordinates: %s,\n' % coordinates)
output_file.write(' textureCoordinates: %s,\n' % textureCoordinates)
output_file.write(' normalCoordinates: %s,\n' % normals)
output_file.write('}')
output_file.close()