dark-hammer / src / h3dimport-gui / h3dimport-gui.py

The branch '0.4.7' does not exist.
Sepehr Taghdisia… 391d72a 
Sepehr Taghdisia… d6e8abf 
Sepehr Taghdisia… 391d72a 

Sepehr Taghdisia… 0c8fc8e 


Sepehr Taghdisia… 391d72a 

Sepehr Taghdisia… d6e8abf 
Sepehr Taghdisia… 391d72a 
Sepehr Taghdisia… d6e8abf 



Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 
Sepehr Taghdisia… d6e8abf 
Sepehr Taghdisia… 391d72a 












Sepehr Taghdisia… 0c8fc8e 


Sepehr Taghdisia… 391d72a 
Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 











Sepehr Taghdisia… 0c8fc8e 











Sepehr Taghdisia… 391d72a 
Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 







Sepehr Taghdisia… 0c8fc8e 

Sepehr Taghdisia… 391d72a 

Sepehr Taghdisia… 0c8fc8e 







Sepehr Taghdisia… 391d72a 















Sepehr Taghdisia… 0c8fc8e 



Sepehr Taghdisia… 391d72a 

Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 

Sepehr Taghdisia… 0c8fc8e 


Sepehr Taghdisia… c873bb3 






Sepehr Taghdisia… 9efd394 



Sepehr Taghdisia… c873bb3 
Sepehr Taghdisia… 9efd394 
Sepehr Taghdisia… c873bb3 





Sepehr Taghdisia… 9efd394 
Sepehr Taghdisia… c873bb3 

Sepehr Taghdisia… 6cc4bde 
Sepehr Taghdisia… c873bb3 
Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… c873bb3 


Sepehr Taghdisia… 9efd394 


Sepehr Taghdisia… c873bb3 
Sepehr Taghdisia… 9efd394 


Sepehr Taghdisia… c873bb3 






Sepehr Taghdisia… 6cc4bde 

Sepehr Taghdisia… c873bb3 





Sepehr Taghdisia… 9efd394 




Sepehr Taghdisia… c873bb3 



Sepehr Taghdisia… 9efd394 
Sepehr Taghdisia… 6cc4bde 
Sepehr Taghdisia… c873bb3 
Sepehr Taghdisia… 9efd394 


































Sepehr Taghdisia… c873bb3 









Sepehr Taghdisia… d6e8abf 
Sepehr Taghdisia… 6cc4bde 
Sepehr Taghdisia… c873bb3 






Sepehr Taghdisia… 0c8fc8e 






Sepehr Taghdisia… c873bb3 
Sepehr Taghdisia… 9efd394 
Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 6cc4bde 


Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 6cc4bde 
Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 6cc4bde 


Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 6cc4bde 



Sepehr Taghdisia… 9efd394 
Sepehr Taghdisia… 6cc4bde 


Sepehr Taghdisia… c873bb3 

















Sepehr Taghdisia… 6cc4bde 

Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 6cc4bde 
Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 6cc4bde 


Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 6cc4bde 












Sepehr Taghdisia… 391d72a 



Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 


Sepehr Taghdisia… d6e8abf 

Sepehr Taghdisia… 391d72a 





Sepehr Taghdisia… f129387 
Sepehr Taghdisia… 391d72a 


Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 




Sepehr Taghdisia… c8a8d88 
Sepehr Taghdisia… d6e8abf 
Sepehr Taghdisia… c8a8d88 
Sepehr Taghdisia… 391d72a 











Sepehr Taghdisia… c8a8d88 
Sepehr Taghdisia… d6e8abf 
Sepehr Taghdisia… 391d72a 






Sepehr Taghdisia… d6e8abf 


Sepehr Taghdisia… 0c8fc8e 

Sepehr Taghdisia… d6e8abf 

Sepehr Taghdisia… 0c8fc8e 

Sepehr Taghdisia… f129387 



Sepehr Taghdisia… 0c8fc8e 

Sepehr Taghdisia… f129387 
Sepehr Taghdisia… d6e8abf 

Sepehr Taghdisia… f129387 








Sepehr Taghdisia… 391d72a 









Sepehr Taghdisia… d6e8abf 
Sepehr Taghdisia… f129387 




Sepehr Taghdisia… 391d72a 





Sepehr Taghdisia… 0c8fc8e 





Sepehr Taghdisia… 391d72a 


Sepehr Taghdisia… 0c8fc8e 

Sepehr Taghdisia… 391d72a 
Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… f129387 

Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 
Sepehr Taghdisia… c8a8d88 

Sepehr Taghdisia… 391d72a 














Sepehr Taghdisia… c8a8d88 
Sepehr Taghdisia… 391d72a 









Sepehr Taghdisia… c8a8d88 
Sepehr Taghdisia… f129387 
Sepehr Taghdisia… 391d72a 



















Sepehr Taghdisia… 9efd394 
Sepehr Taghdisia… 391d72a 


Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 
Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 


Sepehr Taghdisia… c8a8d88 

Sepehr Taghdisia… 391d72a 

























Sepehr Taghdisia… 6cc4bde 
Sepehr Taghdisia… 391d72a 













Sepehr Taghdisia… c8a8d88 

Sepehr Taghdisia… 391d72a 


Sepehr Taghdisia… 0c8fc8e 


Sepehr Taghdisia… 391d72a 

Sepehr Taghdisia… 0c8fc8e 


Sepehr Taghdisia… 391d72a 


Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 



















Sepehr Taghdisia… 0c8fc8e 

Sepehr Taghdisia… 391d72a 





Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 



























































Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 



Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 


Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 



















Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 



Sepehr Taghdisia… 0c8fc8e 


Sepehr Taghdisia… 391d72a 



Sepehr Taghdisia… d6e8abf 
Sepehr Taghdisia… 391d72a 






Sepehr Taghdisia… 0c8fc8e 





Sepehr Taghdisia… 391d72a 





Sepehr Taghdisia… 0c8fc8e 






Sepehr Taghdisia… 391d72a 












Sepehr Taghdisia… c8a8d88 
Sepehr Taghdisia… 391d72a 











Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 

Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 




Sepehr Taghdisia… 0c8fc8e 


Sepehr Taghdisia… 391d72a 


Sepehr Taghdisia… 0c8fc8e 

Sepehr Taghdisia… 391d72a 

Sepehr Taghdisia… c873bb3 
Sepehr Taghdisia… 391d72a 

















Sepehr Taghdisia… c8a8d88 

Sepehr Taghdisia… 391d72a 













Sepehr Taghdisia… 0c8fc8e 



Sepehr Taghdisia… 391d72a 























Sepehr Taghdisia… c873bb3 



Sepehr Taghdisia… 391d72a 




Sepehr Taghdisia… 6cc4bde 

Sepehr Taghdisia… 391d72a 

Sepehr Taghdisia… 6cc4bde 





Sepehr Taghdisia… 9efd394 

Sepehr Taghdisia… 6cc4bde 





Sepehr Taghdisia… 391d72a 

Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 







Sepehr Taghdisia… 0c8fc8e 




















Sepehr Taghdisia… 391d72a 


Sepehr Taghdisia… d6e8abf 

Sepehr Taghdisia… 391d72a 



Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 
Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 
Sepehr Taghdisia… 6cc4bde 


Sepehr Taghdisia… 391d72a 

Sepehr Taghdisia… c873bb3 
Sepehr Taghdisia… 391d72a 
Sepehr Taghdisia… 6cc4bde 

Sepehr Taghdisia… 391d72a 


Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 




Sepehr Taghdisia… c873bb3 
Sepehr Taghdisia… 0c8fc8e 

Sepehr Taghdisia… 391d72a 

Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 


Sepehr Taghdisia… 0c8fc8e 
Sepehr Taghdisia… 391d72a 

Sepehr Taghdisia… 0c8fc8e 


Sepehr Taghdisia… 391d72a 


  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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
from PyQt4 import QtCore, QtGui
import ConfigParser, os, platform, subprocess, sys, json
from copy_reg import add_extension

# globals
prefs = {}
main_wnd = None

# register module path
mod_path = os.path.normpath(os.path.dirname(os.path.abspath(__file__)) + '/../pymodules')
sys.path.append(mod_path)
from dhdlg import about
from dhutil import util
import dheng
import clipedit
import engine

def add_extension(filepath, ext):
    (filename, fileext) = os.path.splitext(filepath)
    if not ("." + ext) in fileext:
        filepath = filepath + "." + ext
    return filepath

###############################################################################################
class w_prefs(QtGui.QDialog):
    def __init__(self, parent):
        super(w_prefs, self).__init__(parent)
        self.init_ui()
        
    def init_ui(self):
        self.setMinimumWidth(500)
        self.setWindowTitle('Preferences')
        #self.setFixedHeight(100) 

        layout = QtGui.QVBoxLayout(self)

        self.edit_bin = QtGui.QLineEdit(self)
        self.check_verbose = QtGui.QCheckBox("Verbose mode", self)
        
        btn_browse = QtGui.QPushButton("Browse ...", self)
        btn_browse.setFixedWidth(80)
        btn_browse.clicked.connect(self.browse_clicked)
        
        layout2 = QtGui.QHBoxLayout()
        layout2.addWidget(QtGui.QLabel("Importer binary :"))
        layout2.addWidget(self.edit_bin)
        layout2.addWidget(btn_browse)
        layout.addLayout(layout2)

        layout4 = QtGui.QHBoxLayout()
        ed_assetdir = QtGui.QLineEdit(self)
        btn_browse = QtGui.QPushButton('Browse', self)
        btn_browse.setFixedWidth(80)
        btn_browse.clicked.connect(self.btn_assetdir_clicked)
        layout4.addWidget(QtGui.QLabel('Asset Directory :')) 
        layout4.addWidget(ed_assetdir)
        layout4.addWidget(btn_browse)
        layout.addLayout(layout4)
        self.ed_assetdir = ed_assetdir

        layout.addWidget(self.check_verbose)

        btn_ok = QtGui.QPushButton("Ok", self)
        btn_cancel = QtGui.QPushButton("Cancel", self)
        btn_ok.clicked.connect(self.ok_clicked)
        btn_cancel.clicked.connect(self.cancel_clicked)
        layout3 = QtGui.QHBoxLayout()
        layout3.addWidget(btn_ok)
        layout3.addWidget(btn_cancel)
        layout3.addStretch()
        layout.addLayout(layout3)  

        layout.addStretch()
        self.setLayout(layout)

    def btn_assetdir_clicked(self, checked):
        dlg = QtGui.QFileDialog(self, "Choose asset directory", self.ed_assetdir.text())
        dlg.setFileMode(QtGui.QFileDialog.Directory)
        if dlg.exec_():
            dirs = dlg.selectedFiles()
            self.ed_assetdir.setText(os.path.abspath(str(dirs[0])))

    def browse_clicked(self, checked):
        if platform.system() == "Windows":
            filters = "Executables (*.exe)"
        else:
            filters = "Executables (*)"
        binfile = QtGui.QFileDialog.getOpenFileName(self, "Open h3dimport binary", "", filters)
        if binfile != "":
            self.edit_bin.setText(binfile)
    
    def ok_clicked(self, checked):
        self.accept()
        
    def cancel_clicked(self, checked):
        self.reject()
        
    def load_config(self, cfg):
        global prefs
        self.edit_bin.setText(prefs['binpath'])
        self.check_verbose.setChecked(prefs['verbose'])
        self.ed_assetdir.setText(prefs['assetdir'])
    
    def save_config(self, cfg):
        global prefs
        if not "general" in cfg.sections():
            cfg.add_section("general")
        prefs['verbose'] = self.check_verbose.isChecked()
        prefs['binpath'] = str(self.edit_bin.text())
        prefs['assetdir'] = str(self.ed_assetdir.text())
          
###############################################################################################
class w_phx(QtGui.QWidget):
    def __init__(self, parent):
        super(w_phx, self).__init__(parent)
        self.infiledir = ""
        self.outfiledir = ""
        self.ctrls = []
        self.quiet_mode = False
        self.watcher = QtCore.QFileSystemWatcher(self)
        self.watcher.fileChanged.connect(self.filemon_onfilechange)    
        self.init_ui()

    def init_ui(self):
        layout = QtGui.QFormLayout(self)
        self.setLayout(layout)
        
        # make controls
        self.edit_infilepath = QtGui.QLineEdit(self)
        self.edit_infilepath.setReadOnly(True)
        btn_browse_infile = QtGui.QPushButton("Browse", self)
        btn_browse_infile.setFixedWidth(60)
        self.combo_names = QtGui.QComboBox(self)
        self.edit_outfilepath = QtGui.QLineEdit(self)
        self.edit_outfilepath.setReadOnly(True)
        btn_browse_outfile = QtGui.QPushButton("Browse", self)
        btn_browse_outfile.setFixedWidth(60)
        self.check_zup = QtGui.QCheckBox(self)
        
        btn_auto = QtGui.QCheckBox("Auto", self)
        btn_auto.setMaximumWidth(50)
        btn_import = QtGui.QPushButton("Import", self)

        self.btn_auto = btn_auto
        self.btn_import = btn_import        
        
        # layouts
        layout_infile = QtGui.QHBoxLayout()
        layout_infile.addWidget(self.edit_infilepath)
        layout_infile.addWidget(btn_browse_infile)
        layout.addRow("Input file:", layout_infile)
        
        layout.addRow("Object name:", self.combo_names)
        
        layout_outfile = QtGui.QHBoxLayout()
        layout_outfile.addWidget(self.edit_outfilepath)
        layout_outfile.addWidget(btn_browse_outfile)
        layout.addRow("Output file:", layout_outfile)
        
        layout.addRow("Up is Z (3dsmax):", self.check_zup)

        layout_import = QtGui.QHBoxLayout()
        layout_import.addWidget(btn_import)
        layout_import.addWidget(btn_auto)
        layout.addRow(layout_import)
        
        #events
        btn_browse_infile.clicked.connect(self.btn_browseinfile_click)
        btn_browse_outfile.clicked.connect(self.btn_browseoutfile_click)
        btn_auto.stateChanged.connect(self.btn_auto_checkstate)
        btn_import.clicked.connect(self.btn_import_click)
        
        # group controls for enable/disable
        self.ctrls.append(btn_browse_infile)
        self.ctrls.append(self.edit_infilepath)
        self.ctrls.append(self.combo_names)
        self.ctrls.append(self.edit_outfilepath)
        self.ctrls.append(self.check_zup)
        self.ctrls.append(btn_browse_outfile)
        self.ctrls.append(btn_import)        

    def filemon_onfilechange(self, qfilepath):
        filepath = str(qfilepath)
        if filepath == str(self.edit_infilepath.text()):
            self.btn_import_click(True)
                        
    def enable_controls(self, enable):
        for c in self.ctrls:
            c.setEnabled(enable)
        
    def btn_auto_checkstate(self, state):
        if state == QtCore.Qt.Checked:
            self.btn_import.setCheckable(True)
            self.btn_import_click(True)
            self.btn_import.setChecked(True)
            self.enable_controls(False)
            self.quiet_mode = True
            if len(self.edit_infilepath.text()) > 0:
                self.watcher.addPath(self.edit_infilepath.text())
        else:
            self.quiet_mode = False
            self.btn_import.setChecked(False)
            self.btn_import.setCheckable(False)
            self.enable_controls(True)   
            if len(self.edit_infilepath.text()) > 0:
                self.watcher.removePath(self.edit_infilepath.text())
            
    def btn_browseinfile_click(self):
        dlg = QtGui.QFileDialog(self, "Open physics", self.infiledir, \
            "Physx3 files (*.RepX)")
        dlg.setFileMode(QtGui.QFileDialog.ExistingFile)
        if dlg.exec_():
            files = dlg.selectedFiles()
            self.edit_infilepath.setText(os.path.normpath(str(files[0])))
            self.infiledir = os.path.normpath(str(dlg.directory().path()))
            in_filepath = str(self.edit_infilepath.text())
            out_filepath = str(self.edit_outfilepath.text())
            self.edit_outfilepath.setText(util.make_samefname(out_filepath, in_filepath, "h3dp"))
            self.enum_phxobjects()
        
    def btn_browseoutfile_click(self):
        dlg = QtGui.QFileDialog(self, "Save h3dp file", self.outfiledir, \
            "dark-hammer physics (*.h3dp)")
        dlg.setFileMode(QtGui.QFileDialog.AnyFile)
        dlg.setAcceptMode(QtGui.QFileDialog.AcceptSave)
        if dlg.exec_():
            relative_path = util.get_rel_path(str(dlg.selectedFiles()[0]), prefs['assetdir'])
            if not relative_path:
                QtGui.QMessageBox.warning(self, 'h3dimport', \
                    'Path must be under asset directory tree')
                return
            self.edit_outfilepath.setText(add_extension(relative_path, "h3dp"))
            self.outfiledir = os.path.normcase(str(dlg.directory().path()))
            
    def btn_import_click(self, checked):
        global prefs
        
        name = str(self.combo_names.itemText(self.combo_names.currentIndex()))

        args = [str(prefs['binpath']), 
                "-p", str(self.edit_infilepath.text()),
                "-o", os.path.normcase(prefs['assetdir'] + '/' + str(self.edit_outfilepath.text())),
                "-n", name]
        if self.check_zup.isChecked():
            args.extend(["-zup"])
        if prefs['verbose']:
            args.extend(["-v"])
        QtGui.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))
        r = subprocess.call(args)
        QtGui.QApplication.restoreOverrideCursor()
        if r == -1 and not self.quiet_mode:
            QtGui.QMessageBox.critical(self, "h3dimport", """Failed to import file, see the"""\
                                       """ terminal for more info""")        
            
    def save_config(self, cfg):
        if not "physics" in cfg.sections():
            cfg.add_section("physics")
        cfg.set("physics", "in_filepath", str(self.edit_infilepath.text()))
        cfg.set("physics", "out_filepath", str(self.edit_outfilepath.text()))
        cfg.set("physics", "in_filedir", str(self.infiledir))
        cfg.set("physics", "out_filedir", str(self.outfiledir))
        cfg.set("physics", "zup", str(self.check_zup.isChecked()))
    
    def load_config(self, cfg):
        if not "physics" in cfg.sections():
            return
        
        self.edit_infilepath.setText(cfg.get("physics", "in_filepath"))
        self.edit_outfilepath.setText(cfg.get("physics", "out_filepath"))
        self.infiledir = cfg.get("physics", "in_filedir")
        self.outfiledir = cfg.get("physics", "out_filedir")
        self.check_zup.setChecked(cfg.getboolean("physics", "zup"))
        
    def enum_phxobjects(self):
        global prefs
        self.combo_names.clear()
        if len(prefs['binpath']) == 0 or len(self.edit_infilepath.text()) == 0:
            return
        
        phx_filepath = str(self.edit_infilepath.text())
        args = [prefs['binpath'], "-l", "-p", phx_filepath]
        try:
            r = subprocess.check_output(args)
        except subprocess.CalledProcessError as ce:
            print ce.output
            QtGui.QMessageBox.critical(self, "h3dimport", "h3dimport raised error!")
        else:
            objs = str(r).replace("\r", "").split("\n")
            for ln in objs:
                if len(ln) > 0:
                    self.combo_names.addItem(ln)
            if self.combo_names.count() > 1:
                self.combo_names.setCurrentIndex(1)

###############################################################################################
class w_anim(QtGui.QWidget):
    def __init__(self, parent):
        super(w_anim, self).__init__(parent)
        global prefs
        self.infiledir = ""
        self.outfiledir = ""
        self.init_ui()
        self.dlg_clipedit = clipedit.qClipEditDlg(self)

    def init_ui(self):
        layout = QtGui.QFormLayout(self)
        self.setLayout(layout)
        
        # make controls
        self.edit_infilepath = QtGui.QLineEdit(self)
        self.edit_infilepath.setReadOnly(True)
        btn_browse_infile = QtGui.QPushButton("Browse", self)
        btn_browse_infile.setFixedWidth(60)
        self.edit_outfilepath = QtGui.QLineEdit(self)
        self.edit_outfilepath.setReadOnly(True)
        btn_browse_outfile = QtGui.QPushButton("Browse", self)
        btn_browse_outfile.setFixedWidth(60)
        btn_import = QtGui.QPushButton("Import", self)
        self.edit_fps = QtGui.QLineEdit("30", self)
        self.edit_fps.setMaximumWidth(40)
        self.check_zup = QtGui.QCheckBox(self)
        btn_clipedit = QtGui.QPushButton('Edit clips', self)

        # add to layout
        layout_infile = QtGui.QHBoxLayout()
        layout_infile.addWidget(self.edit_infilepath)
        layout_infile.addWidget(btn_browse_infile)
        layout.addRow("Input file:", layout_infile)
        
        layout_outfile = QtGui.QHBoxLayout()
        layout_outfile.addWidget(self.edit_outfilepath)
        layout_outfile.addWidget(btn_browse_outfile)
        layout.addRow("Output file:", layout_outfile)
        
        layout.addRow("Fps:", self.edit_fps)
        layout.addRow("Up is Z (3dsmax):", self.check_zup)
        layout.addRow(btn_clipedit)
        
        layout.addRow(btn_import)
        
        # events
        btn_browse_infile.clicked.connect(self.browse_infile_clicked)
        btn_browse_outfile.clicked.connect(self.browse_outfile_clicked)
        btn_import.clicked.connect(self.import_clicked)
        btn_clipedit.clicked.connect(self.btn_clipedit_clicked)
    
    def btn_clipedit_clicked(self, checked):
        global prefs
        if not engine.initialize(prefs['assetdir'], self.dlg_clipedit.eng_view):
            print 'could not initialize dark-hammer engine'
        else:
            # get model file from the current imported model
            global main_wnd

            # before anything, do the animation import process
            self.import_clicked()

            model_file = str(main_wnd.wnds['model'].edit_outfilepath.text())
            anim_file = str(self.edit_outfilepath.text())
            self.dlg_clipedit.load_props(model_file, anim_file, self.in_jsonfile)
            self.dlg_clipedit.exec_()

    def init_clips_jsonfile(self):
        global prefs
        in_filepath = str(self.edit_infilepath.text())
        in_jsonfile = util.make_samefname(in_filepath, in_filepath, '.json')
        if not os.path.isfile(in_jsonfile):
            # create an empty json clips file
            open(in_jsonfile, 'w').write('[{"name":"all"}]')
        self.in_jsonfile = in_jsonfile

    def browse_infile_clicked(self):
        dlg = QtGui.QFileDialog(self, "Open animation", self.infiledir, \
            "Animation files (*.dae *.obj *.x *.ase *.ms3d)")
        dlg.setFileMode(QtGui.QFileDialog.ExistingFile)
        if dlg.exec_():
            files = dlg.selectedFiles()
            self.edit_infilepath.setText(os.path.normpath(str(files[0])))
            self.infiledir = os.path.normpath(str(dlg.directory().path()))
            in_filepath = str(self.edit_infilepath.text())
            out_filepath = str(self.edit_outfilepath.text())
            self.edit_outfilepath.setText(util.make_samefname(out_filepath, in_filepath, "h3da"))

            # try to locate clip (json) file in the same directory and same name as input file, 
            # if not found, create an empty
            self.init_clips_jsonfile()

    def browse_outfile_clicked(self):
        dlg = QtGui.QFileDialog(self, "Save h3da file", self.outfiledir, \
            "dark-hammer anims (*.h3da)")
        dlg.setFileMode(QtGui.QFileDialog.AnyFile)
        dlg.setAcceptMode(QtGui.QFileDialog.AcceptSave)
        if dlg.exec_():
            relative_path = util.get_rel_path(str(dlg.selectedFiles()[0]), prefs['assetdir'])
            if not relative_path:
                QtGui.QMessageBox.warning(self, 'h3dimport', \
                    'Path must be under asset directory tree')
                return            
            self.edit_outfilepath.setText(add_extension(relative_path, "h3da"))
            self.outfiledir = os.path.normpath(str(dlg.directory().path()))

    def import_clicked(self):
        global prefs
        args = [prefs['binpath'], 
                "-a", str(self.edit_infilepath.text()),
                "-o", os.path.normcase(prefs['assetdir'] + '/' + str(self.edit_outfilepath.text())),
                "-fps", str(self.edit_fps.text()),
                '-clips', self.in_jsonfile]
        if prefs['verbose']:
            args.extend(["-v"])
        if self.check_zup.isChecked():
            args.extend(["-zup"])
        QtGui.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))
        r = subprocess.call(args)
        QtGui.QApplication.restoreOverrideCursor()
        if r == -1:
            QtGui.QMessageBox.critical(self, "h3dimport", """Failed to import file, see the"""\
                                       """ terminal for more info""") 

    def save_config(self, cfg):
        if not "anim" in cfg.sections():
            cfg.add_section("anim")
        cfg.set("anim", "in_filepath", str(self.edit_infilepath.text()))
        cfg.set("anim", "out_filepath", str(self.edit_outfilepath.text()))
        cfg.set("anim", "in_filedir", str(self.infiledir))
        cfg.set("anim", "out_filedir", str(self.outfiledir))
        cfg.set("anim", "fps", str(self.edit_fps.text()))
        cfg.set("anim", "zup", str(self.check_zup.isChecked()))
    
    def load_config(self, cfg):
        if not "anim" in cfg.sections():
            return
        
        self.edit_infilepath.setText(cfg.get("anim", "in_filepath"))
        self.edit_outfilepath.setText(cfg.get("anim", "out_filepath"))
        self.infiledir = cfg.get("anim", "in_filedir")
        self.outfiledir = cfg.get("anim", "out_filedir")
        self.edit_fps.setText(cfg.get("anim", "fps"))
        self.check_zup.setChecked(cfg.getboolean("anim", "zup"))
        self.init_clips_jsonfile()

###############################################################################################
class w_model(QtGui.QWidget):
    def __init__(self, parent):
        super(w_model, self).__init__(parent)
        self.infile_dir = ""
        self.outfile_dir = ""
        self.texdir_dir = ""
        self.watcher = QtCore.QFileSystemWatcher(self)
        self.watcher.fileChanged.connect(self.monitor_onfilechange)
        self.quiet_mode = False
        self.ctrls = []
        self.textures = {}
        self.init_ui()
        
    def init_ui(self):
        layout = QtGui.QFormLayout(self)
        self.setLayout(layout)
        
        self.edit_infilepath = QtGui.QLineEdit(self)
        self.edit_infilepath.setReadOnly(True)
        self.combo_names = QtGui.QComboBox(self)
        self.combo_occ = QtGui.QComboBox(self)
        self.edit_outfilepath = QtGui.QLineEdit(self)
        self.edit_outfilepath.setReadOnly(True)
        self.edit_texdir = QtGui.QLineEdit(self)
        self.edit_texdir.setReadOnly(True)
        self.check_calctng = QtGui.QCheckBox(self)
        self.check_fastcompress = QtGui.QCheckBox(self)
        self.check_dxt3 = QtGui.QCheckBox(self)
        self.check_zup = QtGui.QCheckBox(self)

        btn_browse_infile = QtGui.QPushButton("Browse", self)
        btn_browse_outfile = QtGui.QPushButton("Browse", self)
        btn_choose_texdir = QtGui.QPushButton("Choose", self)
        btn_browse_infile.setFixedWidth(60)
        btn_browse_outfile.setFixedWidth(60)
        btn_choose_texdir.setFixedWidth(60)
        layout_infile = QtGui.QHBoxLayout()
        layout_infile.addWidget(self.edit_infilepath)
        layout_infile.addWidget(btn_browse_infile)
        layout_outfile = QtGui.QHBoxLayout()
        layout_outfile.addWidget(self.edit_outfilepath)
        layout_outfile.addWidget(btn_browse_outfile)
        layout_texdir = QtGui.QHBoxLayout()
        layout_texdir.addWidget(self.edit_texdir)
        layout_texdir.addWidget(btn_choose_texdir)
        
        btn_browse_infile.clicked.connect(self.browse_infile_clicked)
        btn_browse_outfile.clicked.connect(self.browse_outfile_clicked)
        btn_choose_texdir.clicked.connect(self.choose_texdir_clicked)
        self.cmbo_names_update = True
        self.combo_names.currentIndexChanged.connect(self.cmbo_names_changed)
        
        layout_import = QtGui.QHBoxLayout()
        btn_auto = QtGui.QCheckBox("Auto", self)
        btn_auto.stateChanged.connect(self.btn_auto_checkstate)
        btn_auto.setMaximumWidth(50)
        self.btn_auto = btn_auto
        btn_import = QtGui.QPushButton("Import", self)
        btn_import.clicked.connect(self.import_clicked)
        layout_import.addWidget(btn_import)
        layout_import.addWidget(btn_auto)
        self.btn_import = btn_import
        
        layout.addRow("Input file:", layout_infile)
        layout.addRow("Model name:", self.combo_names)
        layout.addRow("Occluder:", self.combo_occ)
        layout.addRow("Output file:", layout_outfile)
        layout.addRow("Texture dir:", layout_texdir)
        layout.addRow("Calculate tangents:", self.check_calctng)
        layout.addRow("Fast texture compress:", self.check_fastcompress)
        layout.addRow("Force DXT3 for alpha:", self.check_dxt3)
        layout.addRow("Up is Z (3dsmax):", self.check_zup)
        
        layout.addRow(layout_import)
        
        # add main controls to array for group enable/disable
        self.ctrls = [btn_browse_infile, self.edit_infilepath, self.combo_names, self.combo_occ, \
            self.edit_outfilepath, self.edit_texdir, self.check_calctng, self.check_dxt3, \
            self.check_fastcompress, btn_browse_outfile, btn_choose_texdir, btn_import]
    
    def import_texture(self, tex_filepath, tex_type):
        global prefs
        texdir = os.path.normcase(prefs['assetdir'] + '/' + str(self.edit_texdir.text()))
        texdir_alias = util.valid_engine_path(str(self.edit_texdir.text()))
        calctng = self.check_calctng.isChecked()
        fastcompress = self.check_fastcompress.isChecked()
        forcedxt3 = self.check_dxt3.isChecked()
        args = [prefs['binpath']]
        if tex_filepath != "":
            args.extend(["-t", tex_filepath])
        if texdir != "":
            args.extend(["-tdir", texdir])
        if fastcompress:
            args.extend(["-tfast"])
        if forcedxt3:
            args.extend(["-tdxt3"])
        args.extend(["-ttype", str(tex_type)])
            
        # call h3dimport command
        QtGui.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))
        r = subprocess.call(args)
        QtGui.QApplication.restoreOverrideCursor()
        if r == -1 and not self.quiet_mode:
            QtGui.QMessageBox.critical(self, "h3dimport", """Failed to import file, see the"""\
                                       """ terminal for more info""")
        self.watcher.addPaths(self.textures.keys())
        
    def cmbo_names_changed(self, idx):
        global prefs
        if len(prefs['binpath']) == 0 or len(self.edit_infilepath.text()) == 0:
            return
        if not self.cmbo_names_update:
            return
        
        model_filepath = str(self.edit_infilepath.text())
        name = str(self.combo_names.itemText(self.combo_names.currentIndex()))
        args = [prefs['binpath'], "-lm", "-m", model_filepath, "-n", name]
        try:
            r = subprocess.check_output(args)
        except subprocess.CalledProcessError as ce:
            print ce.output
            QtGui.QMessageBox.critical(self, "h3dimport", "h3dimport raised error!")
        else:
            self.textures = {}
            objs = str(r).replace("\r", "").split("\n")
            for ln in objs:
                if len(ln) > 0 and ("Error:" not in ln) and ("material:" in ln):
                    self.read_material_textures(ln.lstrip("material: "))
        
    def monitor_reg_files(self):
        if len(self.edit_infilepath.text()) > 0:
            self.watcher.addPath(self.edit_infilepath.text())
        if len(self.textures) > 0:
            self.watcher.addPaths(self.textures.keys())
    
    def monitor_unreg_files(self):
        if len(self.edit_infilepath.text()) > 0:
            self.watcher.removePath(self.edit_infilepath.text())
        if len(self.textures) > 0:
            self.watcher.removePaths(self.textures.keys())
        
    def monitor_onfilechange(self, qfilepath):
        filepath = str(qfilepath)
        if filepath == str(self.edit_infilepath.text()):
            self.import_clicked(True)
        elif filepath in self.textures:
            self.import_texture(filepath, self.textures[filepath])
    
    def enable_controls(self, enable):
        for c in self.ctrls:
            c.setEnabled(enable)
        
    def btn_auto_checkstate(self, state):
        if state == QtCore.Qt.Checked:
            self.btn_import.setCheckable(True)
            self.import_clicked(True)
            self.btn_import.setChecked(True)
            self.monitor_reg_files()
            self.enable_controls(False)
            self.quiet_mode = True
        else:
            self.quiet_mode = False
            self.btn_import.setChecked(False)
            self.btn_import.setCheckable(False)
            self.monitor_unreg_files()
            self.enable_controls(True)            
    
    def read_material_textures(self, jstr):
        # parse textures from material data
        jdata = json.loads(jstr)
        for (k, v) in jdata.items():
            if "-tex" in k: 
                v = os.path.normpath(v)
                if v not in self.textures:
                    self.textures[v] = k     # key=texture-file, value=type-string  
        
    def enum_models(self):
        global prefs
        self.cmbo_names_update = False
        self.combo_names.clear()
        self.combo_occ.clear()
        self.combo_occ.addItem("[None]")
        if len(prefs['binpath']) == 0 or len(self.edit_infilepath.text()) == 0:
            return
        
        model_filepath = str(self.edit_infilepath.text())
        args = [prefs['binpath'], "-l", "-lm", "-m", model_filepath]
        try:
            r = subprocess.check_output(args)
        except subprocess.CalledProcessError as ce:
            print ce.output
            QtGui.QMessageBox.critical(self, "h3dimport", "h3dimport raised error!")
            self.cmbo_names_update = True
        else:
            self.textures = {}
            objs = str(r).replace("\r", "").split("\n")
            for ln in objs:
                if len(ln) > 0 and ("Error:" not in ln):
                    if "model:" in ln:
                        name = ln.lstrip("model: ")
                        self.combo_names.addItem(name)
                        self.combo_occ.addItem(name)
                    elif "material:" in ln:
                        self.read_material_textures(ln.lstrip("material: "))
            self.cmbo_names_update = True

    def browse_infile_clicked(self, checked):
        global prefs
        dlg = QtGui.QFileDialog(self, "Open model", self.infile_dir, \
            "Models (*.dae *.obj *.x *.ase *.ms3d)")
        dlg.setFileMode(QtGui.QFileDialog.ExistingFile)
        if dlg.exec_():
            filepath = os.path.normcase(str(dlg.selectedFiles()[0]))
            self.edit_infilepath.setText(filepath)
            self.infile_dir = os.path.abspath(str(dlg.directory().path()))
            self.enum_models()
            # automatically set the name of the output file to the name of the input file
            in_filepath = str(self.edit_infilepath.text())
            out_filepath = str(self.edit_outfilepath.text())
            self.edit_outfilepath.setText(util.make_samefname(out_filepath, in_filepath, "h3dm"))

    def browse_outfile_clicked(self, checked):
        dlg = QtGui.QFileDialog(self, "Save h3dm file", self.outfile_dir, \
            "dark-hammer models (*.h3dm)")
        dlg.setFileMode(QtGui.QFileDialog.AnyFile)
        dlg.setAcceptMode(QtGui.QFileDialog.AcceptSave)
        if dlg.exec_():
            relative_path = util.get_rel_path(str(dlg.selectedFiles()[0]), prefs['assetdir'])
            if not relative_path:
                QtGui.QMessageBox.warning(self, 'h3dimport', \
                    'Path must be under asset directory tree')
                return
            self.edit_outfilepath.setText(add_extension(relative_path, "h3dm"))
            self.outfile_dir = os.path.normpath(str(dlg.directory().path()))
        
    def choose_texdir_clicked(self, checked):
        dlg = QtGui.QFileDialog(self, "Choose texture output directory", self.texdir_dir)
        dlg.setFileMode(QtGui.QFileDialog.Directory)
        if dlg.exec_():
            relative_path = util.get_rel_path(str(dlg.selectedFiles()[0]), prefs['assetdir'])
            if not relative_path:
                QtGui.QMessageBox.warning(self, 'h3dimport', \
                    'Path must be under asset directory tree')
                return

            self.edit_texdir.setText(relative_path)
            
    def load_config(self, cfg):
        if not "model" in cfg.sections():
            return
        self.edit_infilepath.setText(cfg.get("model", "in_filepath"))
        self.edit_outfilepath.setText(cfg.get("model", "out_filepath"))
        self.edit_texdir.setText(cfg.get("model", "texdir"))
        self.check_calctng.setChecked(cfg.getboolean("model", "calctng"))
        self.check_fastcompress.setChecked(cfg.getboolean("model", "fastcompress"))
        self.check_dxt3.setChecked(cfg.getboolean("model", "forcedxt3"))
        self.infile_dir = cfg.get("model", "infile_dir")
        self.outfile_dir = cfg.get("model", "outfile_dir")
        self.texdir_dir = cfg.get("model", "texdir")
        self.check_zup.setChecked(cfg.getboolean("physics", "zup"))
        
    def save_config(self, cfg):
        if not "model" in cfg.sections():
            cfg.add_section("model")
        cfg.set("model", "in_filepath", str(self.edit_infilepath.text()))
        cfg.set("model", "out_filepath", str(self.edit_outfilepath.text()))
        cfg.set("model", "texdir", str(self.edit_texdir.text()))
        cfg.set("model", "calctng", str(self.check_calctng.isChecked()))
        cfg.set("model", "fastcompress", str(self.check_fastcompress.isChecked()))
        cfg.set("model", "forcedxt3", str(self.check_dxt3.isChecked()))
        cfg.set("model", "infile_dir", str(self.infile_dir))
        cfg.set("model", "outfile_dir", str(self.outfile_dir))
        cfg.set("model", "zup", str(self.check_zup.isChecked()))
    
    def import_clicked(self, checked):
        global prefs
        name = str(self.combo_names.itemText(self.combo_names.currentIndex()))
        occname = ""
        if self.combo_occ.currentIndex() != 0:
            occname = str(self.combo_occ.itemText(self.combo_occ.currentIndex()))
        infilepath = str(self.edit_infilepath.text())
        outfilepath = os.path.normcase(prefs['assetdir'] + '/' + str(self.edit_outfilepath.text()))
        texdir = os.path.normcase(prefs['assetdir'] + '/' + str(self.edit_texdir.text()))
        texdir_alias = util.valid_engine_path(str(self.edit_texdir.text()))
        calctng = self.check_calctng.isChecked()
        fastcompress = self.check_fastcompress.isChecked()
        forcedxt3 = self.check_dxt3.isChecked()
        args = [prefs['binpath']]
        if prefs['verbose']:
            args.extend(["-v"])
        if name != "":
            args.extend(["-n", name])
        if infilepath != "":
            args.extend(["-m", infilepath])
        if outfilepath != "":
            args.extend(["-o", outfilepath])
        if texdir != "":
            args.extend(["-tdir", texdir])
        if texdir_alias != "":
            args.extend(["-talias", texdir_alias])
        if calctng:
            args.extend(["-calctng"])
        if fastcompress:
            args.extend(["-tfast"])
        if forcedxt3:
            args.extend(["-tdxt3"])
        if len(occname) > 0:
            args.extend(["-occ", occname])
        if self.quiet_mode:
            args.extend(["-toff"])
        if self.check_zup.isChecked():
            args.extend(["-zup"])
            
        # call h3dimport command
        QtGui.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))
        r = subprocess.call(args)
        QtGui.QApplication.restoreOverrideCursor()
        if r == -1 and not self.quiet_mode:
            QtGui.QMessageBox.critical(self, "h3dimport", """Failed to import file, see the""" \
                                       """ terminal for more info""")            
            
###############################################################################################
class w_main(QtGui.QMainWindow):
    def __init__(self):
        super(w_main, self).__init__()
        self.init_ui()

    def __del__(self):
        engine.release()

    def init_ui(self):
        main_menu = QtGui.QMenuBar(self)
        
        main_menu.addAction("Preferences", self.pref_clicked)
        mnu_help = QtGui.QMenu("Help", self)
        mnu_help.addAction("About", self.mnu_help_click)
        main_menu.addMenu(mnu_help)
        
        self.setMenuBar(main_menu)
        
        self.main_tab = QtGui.QTabWidget(self)
        self.setCentralWidget(self.main_tab)
        
        # child tabs
        self.wnds = dict()
        
        model_wnd = w_model(self)
        self.wnds["model"] = model_wnd        
        self.main_tab.addTab(model_wnd, "Model")
        
        anim_wnd = w_anim(self)
        self.wnds["anim"] = anim_wnd
        self.main_tab.addTab(anim_wnd, "Anim")
        
        phx_wnd = w_phx(self)
        self.wnds["phx"] = phx_wnd
        self.main_tab.addTab(phx_wnd, "Physics")
        
        self.setMinimumWidth(600)
        self.setWindowTitle("h3dimport gui: dark-hammer toolset")
        self.show()
        self.setSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
        
        self.main_tab.currentChanged.connect(self.tab_change)

        # load ui state
        self.load_config()
    
    def tab_change(self, index):
        # turn off auto import on all panels
        for (wnd_name, wnd) in self.wnds.iteritems():
            if wnd_name == "model":
                wnd.btn_auto.setChecked(False)
            elif wnd_name == "phx":
                wnd.btn_auto.setChecked(False)

        if index == 0:
            self.wnds["model"].enum_models()
        elif index == 2:
            self.wnds["phx"].enum_phxobjects()
                
        
    def mnu_help_click(self):
        wnd_about = about.qAboutDlg(self, "h3dimport-gui", "GUI front-end for h3dimport tool")
        wnd_about.exec_()
        
    def pref_clicked(self):
        pref_dlg = w_prefs(self)
        pref_dlg.load_config(self.config)
        if pref_dlg.exec_():
            pref_dlg.save_config(self.config)
    
    def load_prefs(self, cfg):
        global prefs
        if 'general' in cfg.sections():
            prefs['binpath'] = cfg.get('general', 'binary_path')
            prefs['assetdir'] = cfg.get('general', 'asset_dir')
            prefs['verbose'] = cfg.getboolean('general', 'verbose')
        else:
            prefs['binpath'] = os.path.abspath(util.get_exec_dir(__file__) + '/../../bin/h3dimport')
            prefs['assetdir'] = os.path.abspath(util.get_exec_dir(__file__) + '/../..')
            prefs['verbose'] = False        
            if platform.system() == "Windows":
                prefs['binpath'] += ".exe"

    def save_prefs(self, cfg):
        if not 'general' in cfg.sections():
            cfg.add_section('general')

        cfg.set('general', 'binary_path', prefs['binpath'])
        cfg.set('general', 'verbose', str(prefs['verbose']))
        cfg.set('general', 'asset_dir', prefs['assetdir'])        

    def load_config(self):
        try:
            self.config = ConfigParser.SafeConfigParser()
            f = self.config.read(os.path.normpath(util.get_exec_dir(__file__) + \
                "/h3dimport-gui.ini"))
            if len(f) == 0:
                raise BaseException()
        except:
            print "Warning: could not load program config, reset to defaults"
            self.load_prefs(self.config)
        else:
            self.load_prefs(self.config)
            if "ui" in self.config.sections():
                tab_idx = int(self.config.get("ui", "tab_idx"))
            else:
                tab_idx = 0
            self.wnds["model"].load_config(self.config)
            self.wnds["anim"].load_config(self.config)
            self.wnds["phx"].load_config(self.config)
            
            self.main_tab.setCurrentIndex(tab_idx)
            self.tab_change(tab_idx)
            
    def save_config(self):
        # save config
        self.save_prefs(self.config)
        if not "ui" in self.config.sections():
            self.config.add_section("ui")
        self.config.set("ui", "tab_idx", str(self.main_tab.currentIndex()))
        self.wnds["model"].save_config(self.config)
        self.wnds["anim"].save_config(self.config)
        self.wnds["phx"].save_config(self.config)
        self.config.write(open(os.path.normpath(util.get_exec_dir(__file__) + '/h3dimport-gui.ini'),
             'w'))        
        
    def closeEvent(self, e):
        self.save_config()        

###############################################################################################
def main():
    global main_wnd
    app = QtGui.QApplication(sys.argv)
    main_wnd = w_main()
    r = app.exec_()
    del main_wnd
    sys.exit(r)

if __name__ == "__main__":
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.