-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDat2ObjTex.py
executable file
·143 lines (128 loc) · 4.33 KB
/
Dat2ObjTex.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
#!/usr/bin/python
"""
This script takes a .dat file from the Elite/Oolite source
and exports a .obj file containing the same geometry.
Material for the faces is created from the dat file.
Only 1 texture can be handled at this time.
"""
import sys, string, math
def tex_index(tex, vts):
for i in range(len(vts)):
if vts[i]==tex:
return i
return -1
inputfilenames = sys.argv[1:]
print "converting..."
print inputfilenames
for inputfilename in inputfilenames:
outputfilename = inputfilename.lower().replace(".dat",".obj")
materialfilename = inputfilename.lower().replace(".dat",".mtl")
mtllibname = string.split(materialfilename, "/")[-1]
objname=mtllibname.replace(".mtl","")
texname=objname+'_auv'
inputfile = open(inputfilename,"r")
lines = inputfile.read().splitlines(0)
mode = 'SKIP'
vertex_lines_out = []
tex_lines_out = []
faces_lines_out = ['g '+objname+'_'+texname+'\n']
faces_lines_out.append ('usemtl '+texname)
n_verts = 0
n_faces = 0
skips = 0
vertex=[]
texfile=''
vts=[]
texForFace={}
texErr=0
for line in lines:
if (line[:8] == 'TEXTURES'):
mode = 'TEXTURE'
if (mode == 'TEXTURE'):
tokens = string.split(line, '\t') # split line by tabs
if (len(tokens) > 3):
if texfile == '':
texfile=tokens[0]
else:
if tokens[0] != texfile:
print ''
print inputfilename+' : more than 1 texture, cannot convert. Use Dat2Obj instead.'
print ''
texErr=1
break
points = tokens[2:]
tff=[]
for point in points:
v=string.split (point,' ')
vt = ('%.6f %.6f' % (float(v[0]),1-float(v[1])))
if not(vt in vts):
vts.append(vt)
tex_lines_out.append('vt '+vt+'\n')
tff[len(tff):]=[tex_index(vt, vts)]
texForFace[n_faces]=tff
#print tff
n_faces=n_faces + 1
if texErr==0:
n_faces=0
for line in lines:
if (mode == 'VERTEX'):
coordinates = string.split(line, ',') # split line by commas
if (len(coordinates) == 3):
n_verts = n_verts + 1
x = -float(coordinates[0])
y = float(coordinates[1])
z = float(coordinates[2])
vertex.append( (x, y, z) )
vertex_lines_out.append('v %.6f %.6f %.6f\n' % ( x, y, z))
vertex.append( (x, y, z ) )
elif (mode == 'FACES'):
tokens = string.split(line, ',') # split line by commas
if (len(tokens) > 9) :
color_data = tokens[0:3]
normal_data =tokens[3:6]
n_points = tokens[6]
point_data = tokens[7:]
faces_lines_out.append ('\nf ')
for i in range( 0,int(n_points)) :
faces_lines_out.append ('%i/%i/ ' % (int(point_data[i])+1,texForFace[n_faces][i]+1))
n_faces = n_faces + 1
#
elif (mode == 'SKIP'):
skips = skips + 1
#
if (line[:6] == 'NVERTS'):
mode = 'SKIP'
if (line[:6] == 'NFACES'):
mode = 'SKIP'
if (line[:6] == 'VERTEX'):
mode = 'VERTEX'
if (line[:5] == 'FACES'):
mode = 'FACES'
if (line[:8] == 'TEXTURES'):
mode = 'TEXTURE'
#
outputfile = open(outputfilename,"w")
outputfile.write('# Exported with Dat2ObjTex.py (C) Giles Williams 2005 - Kaks 2008\n')
outputfile.write('mtllib %s\n' % mtllibname)
outputfile.write('o '+objname+'\n')
outputfile.write('# %d vertices,' % n_verts)
outputfile.write(' %d faces\n' % n_faces)
outputfile.writelines(vertex_lines_out)
outputfile.writelines(tex_lines_out)
outputfile.writelines(faces_lines_out)
outputfile.writelines('\n\n')
outputfile.close();
materialfile = open(materialfilename,"w")
materialfile.write('# Exported with Dat2ObjTex.py (C) Giles Williams 2005 - Kaks 2008\n')
materialfile.write('newmtl '+texname+'\nNs 100.000\n')
materialfile.write('d 1.00000\nillum 2\n')
materialfile.write('Kd 1.00000 1.00000 1.00000\nKa 1.00000 1.00000 1.00000\n')
materialfile.write('Ks 1.00000 1.00000 1.00000\nKe 0.00000e+0 0.00000e+0 0.00000e+0\n')
materialfile.write('map_Kd '+texfile+'\n\n')
materialfile.close();
print inputfilename+"->"+outputfilename+" & "+materialfilename
print "done"
print ""
#
# end
#