-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtl2opengl.py
741 lines (607 loc) · 25.3 KB
/
mtl2opengl.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
#! /usr/bin/python
# =head1 NAME
#
# mtl2opengl - converts obj and mtl files to arrays for use with OpenGL ES
#
# =head1 SYNOPSIS
#
# mtl2opengl [options]
#
# use -help or -man for further information
#
# =head1 DESCRIPTION
#
# This script expects:
# An OBJ file consisting of vertices (v), texture coords (vt) and normals (vn). The
# corresponding MTL file consisting of ambient (Ka), diffuse (Kd), specular (Ks), and
# exponent (Ns) components.
#
# The resulting .H files offer three float arrays for the OBJ geometry data and
# four float arrays for the MTL material data to be rendered.
#
# =head1 AUTHOR
#
# Guilherme D'Amoreira <http://en.damoreira.com.br/>
#
# =head1 VERSION
#
# 13 March 2017 (1.1)
#
# =head1 VERSION HISTORY
#
# Version 1.1
# -----------
# - Adjusted code to use class attributes
# - Added docs on arguments parser
#
# Version 1.0
# -----------
# First mtl2opengl.py transcode
#
# =head1 COPYRIGHT
#
# MIT License
#
# Copyright (c) 2017 Guilherme D'Amoreira
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# =head1 ACKNOWLEDGEMENTS
#
# This script is based on the work of:
#
# Ricardo Rendon Cepeda <https://github.com/ricardo-rendoncepeda/mtl2opengl>
#
# Heiko Behrens <http://heikobehrens.net/2009/08/27/obj2opengl/>
#
# Margaret Geroch <http://people.sc.fsu.edu/~jburkardt/pl_src/obj2opengl/obj2opengl.html>
#
# =head1 REQUIRED ARGUMENTS
#
# The argument --objfile (1) must be an OBJ file.
# The argument --mtlfile (2) must be the corresponding MTL file.
#
# =head1 OPTIONS
#
# =over
#
# =item B<-noScale>
#
# Prevents automatic scaling. Otherwise the object will be scaled
# such the the longest dimension is 1 unit.
#
# =item B<-scale <float>>
#
# Sets the scale factor explicitly. Please be aware that negative numbers
# are not handled correctly regarding the orientation of the normals.
#
# =item B<-noMove>
#
# Prevents automatic scaling. Otherwise the object will be moved to the center of
# its vertices.
#
# =item B<-center>
#
# Sets center point of the object to centralize.
#
# =item B<-verbose>
#
# Runs this script logging some information.
#
# =cut
import argparse
import os
import re
import math
class ParameterInvalidException(Exception):
pass
class Converter():
def __init__(self, scalefac=0, verbose=None, xcen=None, ycen=None, zcen=None, objfile=None, mtlfile=None):
self.xcen = xcen
self.ycen = ycen
self.zcen = zcen
self.scalefac = scalefac
self.verbose = verbose
self.objfile = objfile
self.mtlfile = mtlfile
self.names = {}
self.values = {}
self.num_verts = 0
self.num_faces = 0
self.num_texture = 0
self.num_normals = 0
self.num_materials = 0
self.in_filename_obj = None
self.in_filename_mtl = None
self.out_filename_obj = None
self.out_filename_mtl = None
self.object_obj = None
self.object_mtl = None
self.xcoords = {}
self.ycoords = {}
self.zcoords = {}
self.tx = {}
self.ty = {}
self.nx = {}
self.ny = {}
self.nz = {}
self.va_idx = {}
self.ta_idx = {}
self.na_idx = {}
self.vb_idx = {}
self.tb_idx = {}
self.nb_idx = {}
self.vc_idx = {}
self.tc_idx = {}
self.nc_idx = {}
self.face_line = {}
self.face_mtl = {}
self.mtl = 0
self.count = {}
def init(self):
self.process_files()
# derive center coords and scale factor if neither provided nor disabled
if not (self.scalefac and self.xcen):
self.calc_size_and_center()
if self.verbose:
self.print_input_and_options()
self.load_data_mtl()
self.load_data_obj()
self.normalize_normals()
if self.verbose:
self.print_statistics()
self.write_output_obj()
self.write_output_mtl()
@staticmethod
def fileparse(path):
(dir_name, file_name) = os.path.split(path)
(file_base_name, file_extension) = os.path.splitext(file_name)
return file_base_name, dir_name, file_extension
def process_files(self):
if not self.objfile:
raise ParameterInvalidException()
if not self.mtlfile:
raise ParameterInvalidException()
(file_obj, dir_obj, ext_obj) = self.fileparse(self.objfile)
self.in_filename_obj = '%s/%s%s' % (dir_obj, file_obj, ext_obj)
(file_mtl, dir_mtl, ext_mtl) = self.fileparse(self.mtlfile)
self.in_filename_mtl = '%s/%s%s' % (dir_mtl, file_mtl, ext_mtl)
(file_obj, dir_obj, ext_obj) = self.fileparse(self.in_filename_obj)
self.out_filename_obj = '%s/%s%s' % (dir_obj, file_obj, "OBJ.h")
(file_mtl, dir_mtl, ext_mtl) = self.fileparse(self.in_filename_mtl)
self.out_filename_mtl = '%s/%s%s' % (dir_mtl, file_mtl, "MTL.h")
(file_obj, dir_obj, ext_obj) = self.fileparse(self.in_filename_obj)
self.object_obj = '%s%s' % (file_obj, "OBJ")
(file_mtl, dir_mtl, ext_mtl) = self.fileparse(self.in_filename_mtl)
self.object_mtl = '%s%s' % (file_mtl, "MTL")
# Stores center of object in xcen, ycen, zcen
# and calculates scaling factor scalefac to limit max
# side of object to 1.0 units
def calc_size_and_center(self):
file_input = open(self.in_filename_obj, 'r')
xsum = 0
ysum = 0
zsum = 0
xmin = 0
ymin = 0
zmin = 0
xmax = 0
ymax = 0
zmax = 0
for line in file_input:
if re.search(r'v\s+.*', line):
self.num_verts += 1
tokens = line.split(' ')
# remove space
tokens.pop(1)
xsum += float(tokens[1])
ysum += float(tokens[2])
zsum += float(tokens[3])
if self.num_verts == 1:
xmin = float(tokens[1])
xmax = float(tokens[1])
ymin = float(tokens[2])
ymax = float(tokens[2])
zmin = float(tokens[3])
zmax = float(tokens[3])
elif tokens[1] < xmin:
xmin = float(tokens[1])
elif tokens[1] > xmax:
xmax = float(tokens[1])
if tokens[2] < ymin:
ymin = float(tokens[2])
elif tokens[2] > ymax:
ymax = float(tokens[2])
if tokens[3] < zmin:
zmin = float(tokens[3])
elif tokens[3] > zmax:
zmax = float(tokens[3])
file_input.close()
# Calculate the center
if not self.xcen:
self.xcen = xsum / self.num_verts
self.ycen = ysum / self.num_verts
self.zcen = zsum / self.num_verts
# Calculate the scale factor
if not self.scalefac:
xdiff = (xmax - xmin)
ydiff = (ymax - ymin)
zdiff = (zmax - zmin)
if xdiff >= ydiff and xdiff >= zdiff:
self.scalefac = xdiff
elif ydiff >= xdiff and ydiff >= zdiff:
self.scalefac = ydiff
else:
self.scalefac = zdiff
self.scalefac = 1.0 / self.scalefac
def print_input_and_options(self):
print "Input files: '%s', '%s'" % (self.in_filename_obj, self.in_filename_mtl)
print "Output files: '%s', '%s'" % (self.out_filename_obj, self.out_filename_mtl)
print "Object names: '%s', '%s'" % (self.object_obj, self.object_mtl)
print "Center: <%s, %s, %s>" % (self.xcen, self.ycen, self.zcen)
print "Scale by: %s" % self.scalefac
def print_statistics(self):
print "----------------"
print "Vertices: %s" % self.num_verts
print "Faces: %s" % self.num_faces
print "Texture Coords: %s" % self.num_texture
print "Normals: %s" % self.num_normals
print "Materials: %s" % self.num_materials
# Reads MTL components for ambient (Ka), diffuse (Kd),
# specular (Ks), and exponent (Ns) values.
# Structure:
# mValues[n][0..2] = Ka
# mValues[n][3..5] = Kd
# mValues[n][6..8] = Ks
# mValues[n][9] = Ns
def load_data_mtl(self):
# MTL data
self.num_materials = -1
self.values = {}
file_input = open(self.in_filename_mtl, 'r')
for line in file_input:
# materials
if re.search(r'newmtl\s+.*', line):
self.num_materials = self.num_materials + 1
self.values[self.num_materials] = {}
# initialize material array
for i in xrange(0, 9):
self.values[self.num_materials][i] = 0.0
self.values[self.num_materials][9] = 1.0
tokens = line.split(' ')
self.names[self.num_materials] = tokens[1]
# ambient
if re.search(r'\s+Ka\s+.*', line):
tokens = line.split(' ')
self.values[self.num_materials][0] = "%.3f" % float(tokens[1])
self.values[self.num_materials][1] = "%.3f" % float(tokens[2])
self.values[self.num_materials][2] = "%.3f" % float(tokens[3])
# diffuse
if re.search(r'\s+Kd\s+.*', line):
tokens = line.split(' ')
self.values[self.num_materials][3] = "%.3f" % float(tokens[1])
self.values[self.num_materials][4] = "%.3f" % float(tokens[2])
self.values[self.num_materials][5] = "%.3f" % float(tokens[3])
# specular
if re.search(r'\s+Ks\s+.*', line):
tokens = line.split(' ')
self.values[self.num_materials][6] = "%.3f" % float(tokens[1])
self.values[self.num_materials][7] = "%.3f" % float(tokens[2])
self.values[self.num_materials][8] = "%.3f" % float(tokens[3])
# exponent
if re.search(r'\s+Ns\s+.*', line):
tokens = line.split(' ')
self.values[self.num_materials][9] = "%.3f" % float(tokens[1])
file_input.close()
self.num_materials += 1
# reads vertices into $xcoords[], $ycoords[], $zcoords[]
# where coordinates are moved and scaled according to
# $xcen, $ycen, $zcen and $scalefac
# reads texture coords into $tx[], $ty[]
# where y coordinate is mirrowed
# reads normals into $nx[], $ny[], $nz[]
# but does not normalize, see normalizeNormals()
# reads faces and establishes lookup data where
# va_idx[], vb_idx[], vc_idx[] for vertices
# ta_idx[], tb_idx[], tc_idx[] for texture coords
# na_idx[], nb_idx[], nc_idx[] for normals
# store indizes for the former arrays respectively
# also, $face_line[] store actual face string
def load_data_obj(self):
# OBJ data
self.num_verts = 0
self.num_faces = 0
self.num_texture = 0
self.num_normals = 0
self.xcoords = {}
self.ycoords = {}
self.zcoords = {}
self.tx = {}
self.ty = {}
self.nx = {}
self.ny = {}
self.nz = {}
self.va_idx = {}
self.ta_idx = {}
self.na_idx = {}
self.vb_idx = {}
self.tb_idx = {}
self.nb_idx = {}
self.vc_idx = {}
self.tc_idx = {}
self.nc_idx = {}
self.face_line = {}
self.face_mtl = {}
# MTL data
self.mtl = 0
file_input = open(self.in_filename_obj, 'r')
for line in file_input:
# vertices
if re.search(r'v\s+.*', line):
tokens = line.split(' ')
# remove space
tokens.pop(1)
x = (float(tokens[1]) - self.xcen) * self.scalefac
y = (float(tokens[2]) - self.ycen) * self.scalefac
z = (float(tokens[3]) - self.zcen) * self.scalefac
self.xcoords[self.num_verts] = "%.3f" % float(x)
self.ycoords[self.num_verts] = "%.3f" % float(y)
self.zcoords[self.num_verts] = "%.3f" % float(z)
self.num_verts += 1
# texture coords
if re.search(r'vt\s+.*', line):
tokens = line.split(' ')
x = float(tokens[1])
y = 1 - float(tokens[2])
self.tx[self.num_texture] = "%.3f" % float(x)
self.ty[self.num_texture] = "%.3f" % float(y)
self.num_texture += 1
# normals
if re.search(r'vn\s+.*', line):
tokens = line.split(' ')
x = tokens[1]
y = tokens[2]
z = tokens[3]
self.nx[self.num_normals] = "%.3f" % float(x)
self.ny[self.num_normals] = "%.3f" % float(y)
self.nz[self.num_normals] = "%.3f" % float(z)
self.num_normals += 1
# faces
regexp = re.compile(r'f\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)(\s+([^ ]+))?')
result_regexp = regexp.search(line)
if result_regexp:
(f1, f2, f3, f4, f5) = result_regexp.groups()
a = map(float, f1.split('/'))
b = map(float, f2.split('/'))
c = map(float, f3.split('/'))
self.va_idx[self.num_faces] = a[0] - 1
self.ta_idx[self.num_faces] = a[1] - 1
self.na_idx[self.num_faces] = a[2] - 1
self.vb_idx[self.num_faces] = b[0] - 1
self.tb_idx[self.num_faces] = b[1] - 1
self.nb_idx[self.num_faces] = b[2] - 1
self.vc_idx[self.num_faces] = c[0] - 1
self.tc_idx[self.num_faces] = c[1] - 1
self.nc_idx[self.num_faces] = c[2] - 1
self.face_line[self.num_faces] = line
self.face_mtl[self.num_faces] = self.names[self.mtl]
self.num_faces += 1
# rectangle => second triangle
if f5.rstrip() != "":
d = map(float, f5.split('/'))
self.va_idx[self.num_faces] = a[0] - 1
self.ta_idx[self.num_faces] = a[1] - 1
self.na_idx[self.num_faces] = a[2] - 1
self.vb_idx[self.num_faces] = d[0] - 1
self.tb_idx[self.num_faces] = d[1] - 1
self.nb_idx[self.num_faces] = d[2] - 1
self.vc_idx[self.num_faces] = c[0] - 1
self.tc_idx[self.num_faces] = c[1] - 1
self.nc_idx[self.num_faces] = c[2] - 1
self.face_line[self.num_faces] = line
self.face_mtl[self.num_faces] = self.names[self.mtl]
self.num_faces += 1
# materials
if re.search(r'usemtl\s+.*', line):
tokens = line.split(' ')
i = 0
for mName in self.names:
if tokens[1] == mName:
self.mtl = i
i += 1
file_input.close()
def normalize_normals(self):
for j in xrange(self.num_normals):
d = math.sqrt(float(self.nx[j]) * float(self.nx[j]) + float(self.ny[j]) * float(self.ny[j]) + float(self.nz[j]) * float(self.nz[j]))
if d == 0:
self.nx[j] = 1
self.ny[j] = 0
self.nz[j] = 0
else:
self.nx[j] = "%.3f" % (float(self.nx[j]) / d)
self.ny[j] = "%.3f" % (float(self.ny[j]) / d)
self.nz[j] = "%.3f" % (float(self.nz[j]) / d)
def write_output_obj(self):
self.count = {}
file_input = open(self.out_filename_obj, 'w')
file_input.write("// Created with mtl2opengl.py\n\n")
# some statistics
file_input.write("/*\n")
file_input.write("source files: %s, %s\n" % (self.in_filename_obj, self.in_filename_mtl))
file_input.write("vertices: %s\n" % self.num_verts)
file_input.write("faces: %s\n" % self.num_faces)
file_input.write("normals: %s\n" % self.num_normals)
file_input.write("texture coords: %s\n" % self.num_texture)
file_input.write("*/\n")
file_input.write("\n\n")
# needed constant for glDrawArrays
file_input.write("unsigned int " + self.object_obj + "NumVerts = " + str(self.num_faces * 3) + ";\n\n")
# write verts
file_input.write("float " + self.object_obj + "Verts [] = {\n")
for i in xrange(self.num_materials):
self.count[i] = 0
for j in xrange(self.num_faces):
if self.face_mtl[j] == self.names[i]:
ia = self.va_idx[j]
ib = self.vb_idx[j]
ic = self.vc_idx[j]
file_input.write("%.3f,%.3f,%.3f,\n" % (float(self.xcoords[ia]), float(self.ycoords[ia]), float(self.zcoords[ia])))
file_input.write("%.3f,%.3f,%.3f,\n" % (float(self.xcoords[ib]), float(self.ycoords[ib]), float(self.zcoords[ib])))
file_input.write("%.3f,%.3f,%.3f,\n" % (float(self.xcoords[ic]), float(self.ycoords[ic]), float(self.zcoords[ic])))
self.count[i] += 3
file_input.write("};\n\n")
# write normals
if self.num_normals > 0:
file_input.write("float " + self.object_obj + "Normals [] = {\n")
for i in xrange(self.num_materials):
for j in xrange(self.num_faces):
if self.face_mtl[j] == self.names[i]:
ia = self.na_idx[j]
ib = self.nb_idx[j]
ic = self.nc_idx[j]
file_input.write("%s,%s,%s,\n" % (self.nx[ia], self.ny[ia], self.nz[ia]))
file_input.write("%s,%s,%s,\n" % (self.nx[ib], self.ny[ib], self.nz[ib]))
file_input.write("%s,%s,%s,\n" % (self.nx[ic], self.ny[ic], self.nz[ic]))
file_input.write("};\n\n")
# write texture coords
if self.num_texture:
file_input.write("float " + self.object_obj + "TexCoords [] = {\n")
for i in xrange(self.num_materials):
for j in xrange(self.num_faces):
if self.face_mtl[j] == self.names[i]:
ia = self.ta_idx[j]
ib = self.tb_idx[j]
ic = self.tc_idx[j]
file_input.write("%s,%s,\n" % (self.tx[ia], self.ty[ia]))
file_input.write("%s,%s,\n" % (self.tx[ib], self.ty[ib]))
file_input.write("%s,%s,\n" % (self.tx[ic], self.ty[ic]))
file_input.write("};\n\n")
file_input.close()
def write_output_mtl(self):
file_input = open(self.out_filename_mtl, 'w')
file_input.write("// Created with mtl2opengl.py\n\n")
# some statistics
file_input.write("/*\n")
file_input.write("source files: %s, %s\n" % (self.in_filename_obj, self.in_filename_mtl))
file_input.write("materials: %s\n\n" % self.num_materials)
for i in xrange(self.num_materials):
kaR = float(self.values[i][0])
kaG = float(self.values[i][1])
kaB = float(self.values[i][2])
kdR = float(self.values[i][3])
kdG = float(self.values[i][4])
kdB = float(self.values[i][5])
ksR = float(self.values[i][6])
ksG = float(self.values[i][7])
ksB = float(self.values[i][8])
nsE = float(self.values[i][9])
file_input.write("Name: %s" % self.names[i])
file_input.write("Ka: %.3f, %.3f, %.3f\n" % (kaR, kaG, kaB))
file_input.write("Kd: %.3f, %.3f, %.3f\n" % (kdR, kdG, kdB))
file_input.write("Ks: %.3f, %.3f, %.3f\n" % (ksR, ksG, ksB))
file_input.write("Ns: %.3f\n\n" % nsE)
file_input.write("*/\n")
file_input.write("\n\n")
# needed constant for glDrawArrays
file_input.write("int " + self.object_mtl + "NumMaterials = " + str(self.num_materials) + ";\n\n")
# write firsts
file_input.write("int " + self.object_mtl + "First [" + str(self.num_materials) + "] = {\n")
for i in xrange(self.num_materials):
if i == 0:
first = 0
else:
first += self.count[i - 1]
file_input.write("%s,\n" % first)
file_input.write("};\n\n")
# write counts
file_input.write("int " + self.object_mtl + "Count [" + str(self.num_materials) + "] = {\n")
for i in xrange(self.num_materials):
count = self.count[i]
file_input.write("%s,\n" % count)
file_input.write("};\n\n")
# write ambients
file_input.write("float " + self.object_mtl + "Ambient [" + str(self.num_materials) + "][3] = {\n")
for i in xrange(self.num_materials):
kaR = float(self.values[i][0])
kaG = float(self.values[i][1])
kaB = float(self.values[i][2])
file_input.write("%.3f,%.3f,%.3f,\n" % (kaR, kaG, kaB))
file_input.write("};\n\n")
# write diffuses
file_input.write("float " + self.object_mtl + "Diffuse [" + str(self.num_materials) + "][3] = {\n")
for i in xrange(self.num_materials):
kdR = float(self.values[i][3])
kdG = float(self.values[i][4])
kdB = float(self.values[i][5])
file_input.write("%.3f,%.3f,%.3f,\n" % (kdR, kdG, kdB))
file_input.write("};\n\n")
# write speculars
file_input.write("float " + self.object_mtl + "Specular [" + str(self.num_materials) + "][3] = {\n")
for i in xrange(self.num_materials):
ksR = float(self.values[i][6])
ksG = float(self.values[i][7])
ksB = float(self.values[i][8])
file_input.write("%.3f,%.3f,%.3f,\n" % (ksR, ksG, ksB))
file_input.write("};\n\n")
# write exponents
file_input.write("float " + self.object_mtl + "Exponent [" + str(self.num_materials) + "] = {\n")
for i in xrange(self.num_materials):
nsE = float(self.values[i][9])
file_input.write("%.3f,\n" % nsE)
file_input.write("};\n\n")
file_input.close()
# main function call
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='An OBJ file consisting of vertices (v), texture coords (vt) and normals (vn). \
The corresponding MTL file consisting of ambient (Ka), diffuse (Kd), specular (Ks), and exponent (Ns) components. \
The resulting .H files offer three float arrays for the OBJ geometry data and \
four float arrays for the MTL material data to be rendered.')
parser.add_argument('--center', metavar='N/N/N', help='Sets center point of the object to centralize. The format is "N" as a number: N/N/N.')
parser.add_argument('--noMove', metavar='1/0', help='Prevents automatic scaling. Otherwise the object will be moved to the center of its vertices.')
parser.add_argument('--noScale', metavar='1/0', help='Prevents automatic scaling. Otherwise the object will be scaled such the the longest dimension is 1 unit.')
parser.add_argument('--scale', metavar='0..1',
help='Sets the scale factor explicitly. Please be aware that negative numbers are not handled correctly regarding the orientation of the normals.')
parser.add_argument('--verbose', metavar='1/0', help='Runs this script logging some information.')
parser.add_argument('--mtlfile', metavar='<mtlfile path>', help='Sets the .mtl file path.', required=True)
parser.add_argument('--objfile', metavar='<objfile path>', help='Sets the .obj file path.', required=True)
args = parser.parse_args()
objfile = args.objfile
mtlfile = args.mtlfile
scalefac = args.scale
verbose = args.verbose
no_scale = args.noScale
no_move = args.noMove
center = args.center
xcen = None
ycen = None
zcen = None
if no_scale:
scalefac = 1
elif scalefac < 0:
raise ParameterInvalidException()
if no_move:
center = '0/0/0'
if center:
center = center.split('/')
xcen = center[0]
ycen = center[1]
zcen = center[2]
# start converter
converter = Converter(scalefac=scalefac, verbose=verbose, xcen=xcen, ycen=ycen, zcen=zcen, objfile=objfile, mtlfile=mtlfile)
converter.init()