Welcome to mirror list, hosted at ThFree Co, Russian Federation.

md2_export.py « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bf214dd1b58d569e63145ef47cc30b1e60a12c60 (plain)
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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
#!BPY

"""
Name: 'MD2 (.md2)'
Blender: 241
Group: 'Export'
Tooltip: 'Export to Quake file format (.md2).'
"""

__author__ = 'Bob Holcomb'
__version__ = '0.17'
__url__ = ["Bob's site, http://bane.servebeer.com",
     "Support forum, http://scourage.servebeer.com/phpbb/", "blender", "elysiun"]
__email__ = ["Bob Holcomb, bob_holcomb:hotmail*com", "scripts"]
__bpydoc__ = """\
This script Exports a Quake 2 file (MD2).

 Additional help from: Shadwolf, Skandal, Rojo, Cambo<br>
 Thanks Guys!
"""

# ***** BEGIN GPL LICENSE BLOCK *****
#
# Script copyright (C): Bob Holcomb
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------


import Blender
from Blender import *
from Blender.Draw import *
from Blender.BGL import *
from Blender.Window import *

import struct, string
from types import *



######################################################
# GUI Loader
######################################################

# Export globals
g_filename=Create("/home/bob/work/blender_scripts/md2/test-export.md2")
g_frame_filename=Create("default")

g_filename_search=Create("model")
g_frame_search=Create("default")

user_frame_list=[]

#Globals
g_scale=Create(1.0)

# Events
EVENT_NOEVENT=1
EVENT_SAVE_MD2=2
EVENT_CHOOSE_FILENAME=3
EVENT_CHOOSE_FRAME=4
EVENT_EXIT=100

######################################################
# Callbacks for Window functions
######################################################
def filename_callback(input_filename):
	global g_filename
	g_filename.val=input_filename

def frame_callback(input_frame):
	global g_frame_filename
	g_frame_filename.val=input_frame

def draw_gui():
	global g_scale
	global g_filename
	global g_frame_filename
	global EVENT_NOEVENT,EVENT_SAVE_MD2,EVENT_CHOOSE_FILENAME,EVENT_CHOOSE_FRAME,EVENT_EXIT

	########## Titles
	glClear(GL_COLOR_BUFFER_BIT)
	glRasterPos2d(8, 103)
	Text("MD2 Export")

	######### Parameters GUI Buttons
	g_filename = String("MD2 file to save: ", EVENT_NOEVENT, 10, 55, 210, 18,
                            g_filename.val, 255, "MD2 file to save")
	########## MD2 File Search Button
	Button("Search",EVENT_CHOOSE_FILENAME,220,55,80,18)

	g_frame_filename = String("Frame List file to load: ", EVENT_NOEVENT, 10, 35, 210, 18,
                                g_frame_filename.val, 255, "Frame List to load-overrides MD2 defaults")
	########## Texture Search Button
	Button("Search",EVENT_CHOOSE_FRAME,220,35,80,18)

	########## Scale slider-default is 1/8 which is a good scale for md2->blender
	g_scale= Slider("Scale Factor: ", EVENT_NOEVENT, 10, 75, 210, 18,
                    1.0, 0.001, 10.0, 1, "Scale factor for obj Model");

	######### Draw and Exit Buttons
	Button("Export",EVENT_SAVE_MD2 , 10, 10, 80, 18)
	Button("Exit",EVENT_EXIT , 170, 10, 80, 18)

def event(evt, val):	
	if (evt == QKEY and not val):
		Exit()

def bevent(evt):
	global g_filename
	global g_frame_filename
	global EVENT_NOEVENT,EVENT_SAVE_MD2,EVENT_EXIT

	######### Manages GUI events
	if (evt==EVENT_EXIT):
		Blender.Draw.Exit()
	elif (evt==EVENT_CHOOSE_FILENAME):
		FileSelector(filename_callback, "MD2 File Selection")
	elif (evt==EVENT_CHOOSE_FRAME):
		FileSelector(frame_callback, "Frame Selection")
	elif (evt==EVENT_SAVE_MD2):
		if (g_filename.val == "model"):
			save_md2("blender.md2")
			Blender.Draw.Exit()
			return
		else:
			save_md2(g_filename.val)
			Blender.Draw.Exit()
			return

Register(draw_gui, event, bevent)

######################################################
# MD2 Model Constants
######################################################
MD2_MAX_TRIANGLES=4096
MD2_MAX_VERTICES=2048
MD2_MAX_TEXCOORDS=2048
MD2_MAX_FRAMES=512
MD2_MAX_SKINS=32
MD2_MAX_FRAMESIZE=(MD2_MAX_VERTICES * 4 + 128)

MD2_FRAME_NAME_LIST=(("stand",1,40),
					("run",41,46),
					("attack",47,54),
					("pain1",55,58),
					("pain2",59,62),
					("pain3",63,66),
					("jump",67,72),
					("flip",73,84),
					("salute", 85,95),
					("taunt",96,112),
					("wave",113,123),
					("point",124,135),
					("crstnd",136,154),
					("crwalk",155,160),
					("crattack",161,169),
					("crpain",170,173),
					("crdeath",174,178),
					("death1",179,184),
					("death2",185,190),
					("death3",191,198))
					#198 frames
					
MD2_NORMALS=((-0.525731, 0.000000, 0.850651), 
			(-0.442863, 0.238856, 0.864188), 
			(-0.295242, 0.000000, 0.955423), 
			(-0.309017, 0.500000, 0.809017), 
			(-0.162460, 0.262866, 0.951056), 
			(0.000000, 0.000000, 1.000000), 
			(0.000000, 0.850651, 0.525731), 
			(-0.147621, 0.716567, 0.681718), 
			(0.147621, 0.716567, 0.681718), 
			(0.000000, 0.525731, 0.850651), 
			(0.309017, 0.500000, 0.809017), 
			(0.525731, 0.000000, 0.850651), 
			(0.295242, 0.000000, 0.955423), 
			(0.442863, 0.238856, 0.864188), 
			(0.162460, 0.262866, 0.951056), 
			(-0.681718, 0.147621, 0.716567), 
			(-0.809017, 0.309017, 0.500000), 
			(-0.587785, 0.425325, 0.688191), 
			(-0.850651, 0.525731, 0.000000), 
			(-0.864188, 0.442863, 0.238856), 
			(-0.716567, 0.681718, 0.147621), 
			(-0.688191, 0.587785, 0.425325), 
			(-0.500000, 0.809017, 0.309017), 
			(-0.238856, 0.864188, 0.442863), 
			(-0.425325, 0.688191, 0.587785), 
			(-0.716567, 0.681718, -0.147621), 
			(-0.500000, 0.809017, -0.309017), 
			(-0.525731, 0.850651, 0.000000), 
			(0.000000, 0.850651, -0.525731), 
			(-0.238856, 0.864188, -0.442863), 
			(0.000000, 0.955423, -0.295242), 
			(-0.262866, 0.951056, -0.162460), 
			(0.000000, 1.000000, 0.000000), 
			(0.000000, 0.955423, 0.295242), 
			(-0.262866, 0.951056, 0.162460), 
			(0.238856, 0.864188, 0.442863), 
			(0.262866, 0.951056, 0.162460), 
			(0.500000, 0.809017, 0.309017), 
			(0.238856, 0.864188, -0.442863), 
			(0.262866, 0.951056, -0.162460), 
			(0.500000, 0.809017, -0.309017), 
			(0.850651, 0.525731, 0.000000), 
			(0.716567, 0.681718, 0.147621), 
			(0.716567, 0.681718, -0.147621), 
			(0.525731, 0.850651, 0.000000), 
			(0.425325, 0.688191, 0.587785), 
			(0.864188, 0.442863, 0.238856), 
			(0.688191, 0.587785, 0.425325), 
			(0.809017, 0.309017, 0.500000), 
			(0.681718, 0.147621, 0.716567), 
			(0.587785, 0.425325, 0.688191), 
			(0.955423, 0.295242, 0.000000), 
			(1.000000, 0.000000, 0.000000), 
			(0.951056, 0.162460, 0.262866), 
			(0.850651, -0.525731, 0.000000), 
			(0.955423, -0.295242, 0.000000), 
			(0.864188, -0.442863, 0.238856), 
			(0.951056, -0.162460, 0.262866), 
			(0.809017, -0.309017, 0.500000), 
			(0.681718, -0.147621, 0.716567), 
			(0.850651, 0.000000, 0.525731), 
			(0.864188, 0.442863, -0.238856), 
			(0.809017, 0.309017, -0.500000), 
			(0.951056, 0.162460, -0.262866), 
			(0.525731, 0.000000, -0.850651), 
			(0.681718, 0.147621, -0.716567), 
			(0.681718, -0.147621, -0.716567), 
			(0.850651, 0.000000, -0.525731), 
			(0.809017, -0.309017, -0.500000), 
			(0.864188, -0.442863, -0.238856), 
			(0.951056, -0.162460, -0.262866), 
			(0.147621, 0.716567, -0.681718), 
			(0.309017, 0.500000, -0.809017), 
			(0.425325, 0.688191, -0.587785), 
			(0.442863, 0.238856, -0.864188), 
			(0.587785, 0.425325, -0.688191), 
			(0.688191, 0.587785, -0.425325), 
			(-0.147621, 0.716567, -0.681718), 
			(-0.309017, 0.500000, -0.809017), 
			(0.000000, 0.525731, -0.850651), 
			(-0.525731, 0.000000, -0.850651), 
			(-0.442863, 0.238856, -0.864188), 
			(-0.295242, 0.000000, -0.955423), 
			(-0.162460, 0.262866, -0.951056), 
			(0.000000, 0.000000, -1.000000), 
			(0.295242, 0.000000, -0.955423), 
			(0.162460, 0.262866, -0.951056), 
			(-0.442863, -0.238856, -0.864188), 
			(-0.309017, -0.500000, -0.809017), 
			(-0.162460, -0.262866, -0.951056), 
			(0.000000, -0.850651, -0.525731), 
			(-0.147621, -0.716567, -0.681718), 
			(0.147621, -0.716567, -0.681718), 
			(0.000000, -0.525731, -0.850651), 
			(0.309017, -0.500000, -0.809017), 
			(0.442863, -0.238856, -0.864188), 
			(0.162460, -0.262866, -0.951056), 
			(0.238856, -0.864188, -0.442863), 
			(0.500000, -0.809017, -0.309017), 
			(0.425325, -0.688191, -0.587785), 
			(0.716567, -0.681718, -0.147621), 
			(0.688191, -0.587785, -0.425325), 
			(0.587785, -0.425325, -0.688191), 
			(0.000000, -0.955423, -0.295242), 
			(0.000000, -1.000000, 0.000000), 
			(0.262866, -0.951056, -0.162460), 
			(0.000000, -0.850651, 0.525731), 
			(0.000000, -0.955423, 0.295242), 
			(0.238856, -0.864188, 0.442863), 
			(0.262866, -0.951056, 0.162460), 
			(0.500000, -0.809017, 0.309017), 
			(0.716567, -0.681718, 0.147621), 
			(0.525731, -0.850651, 0.000000), 
			(-0.238856, -0.864188, -0.442863), 
			(-0.500000, -0.809017, -0.309017), 
			(-0.262866, -0.951056, -0.162460), 
			(-0.850651, -0.525731, 0.000000), 
			(-0.716567, -0.681718, -0.147621), 
			(-0.716567, -0.681718, 0.147621), 
			(-0.525731, -0.850651, 0.000000), 
			(-0.500000, -0.809017, 0.309017), 
			(-0.238856, -0.864188, 0.442863), 
			(-0.262866, -0.951056, 0.162460), 
			(-0.864188, -0.442863, 0.238856), 
			(-0.809017, -0.309017, 0.500000), 
			(-0.688191, -0.587785, 0.425325), 
			(-0.681718, -0.147621, 0.716567), 
			(-0.442863, -0.238856, 0.864188), 
			(-0.587785, -0.425325, 0.688191), 
			(-0.309017, -0.500000, 0.809017), 
			(-0.147621, -0.716567, 0.681718), 
			(-0.425325, -0.688191, 0.587785), 
			(-0.162460, -0.262866, 0.951056), 
			(0.442863, -0.238856, 0.864188), 
			(0.162460, -0.262866, 0.951056), 
			(0.309017, -0.500000, 0.809017), 
			(0.147621, -0.716567, 0.681718), 
			(0.000000, -0.525731, 0.850651), 
			(0.425325, -0.688191, 0.587785), 
			(0.587785, -0.425325, 0.688191), 
			(0.688191, -0.587785, 0.425325), 
			(-0.955423, 0.295242, 0.000000), 
			(-0.951056, 0.162460, 0.262866), 
			(-1.000000, 0.000000, 0.000000), 
			(-0.850651, 0.000000, 0.525731), 
			(-0.955423, -0.295242, 0.000000), 
			(-0.951056, -0.162460, 0.262866), 
			(-0.864188, 0.442863, -0.238856), 
			(-0.951056, 0.162460, -0.262866), 
			(-0.809017, 0.309017, -0.500000), 
			(-0.864188, -0.442863, -0.238856), 
			(-0.951056, -0.162460, -0.262866), 
			(-0.809017, -0.309017, -0.500000), 
			(-0.681718, 0.147621, -0.716567), 
			(-0.681718, -0.147621, -0.716567), 
			(-0.850651, 0.000000, -0.525731), 
			(-0.688191, 0.587785, -0.425325), 
			(-0.587785, 0.425325, -0.688191), 
			(-0.425325, 0.688191, -0.587785), 
			(-0.425325, -0.688191, -0.587785), 
			(-0.587785, -0.425325, -0.688191), 
			(-0.688191, -0.587785, -0.425325))


######################################################
# MD2 data structures
######################################################
class md2_point:
	vertices=[]
	lightnormalindex=0
	binary_format="<3BB"
	def __init__(self):
		self.vertices=[0]*3
		self.lightnormalindex=0
	def save(self, file):
		temp_data=[0]*4
		temp_data[0]=self.vertices[0]
		temp_data[1]=self.vertices[1]
		temp_data[2]=self.vertices[2]
		temp_data[3]=self.lightnormalindex
		data=struct.pack(self.binary_format, temp_data[0], temp_data[1], temp_data[2], temp_data[3])
		file.write(data)
	def dump(self):
		print "MD2 Point Structure"
		print "vertex X: ", self.vertices[0]
		print "vertex Y: ", self.vertices[1]
		print "vertex Z: ", self.vertices[2]
		print "lightnormalindex: ",self.lightnormalindex
		print ""
		
class md2_face:
	vertex_index=[]
	texture_index=[]
	binary_format="<3h3h"
	def __init__(self):
		self.vertex_index = [ 0, 0, 0 ]
		self.texture_index = [ 0, 0, 0]
	def save(self, file):
		temp_data=[0]*6
		#swap vertices around so they draw right
		temp_data[0]=self.vertex_index[0]
		temp_data[1]=self.vertex_index[2]
		temp_data[2]=self.vertex_index[1]
		#swap texture vertices around so they draw right
		temp_data[3]=self.texture_index[0]
		temp_data[4]=self.texture_index[2]
		temp_data[5]=self.texture_index[1]
		data=struct.pack(self.binary_format,temp_data[0],temp_data[1],temp_data[2],temp_data[3],temp_data[4],temp_data[5])
		file.write(data)
	def dump (self):
		print "MD2 Face Structure"
		print "vertex 1 index: ", self.vertex_index[0]
		print "vertex 2 index: ", self.vertex_index[1]
		print "vertex 3 index: ", self.vertex_index[2]
		print "texture 1 index: ", self.texture_index[0]
		print "texture 2 index: ", self.texture_index[1]
		print "texture 3 index: ", self.texture_index[2]
		print ""
		
class md2_tex_coord:
	u=0
	v=0
	binary_format="<2h"
	def __init__(self):
		self.u=0
		self.v=0
	def save(self, file):
		temp_data=[0]*2
		temp_data[0]=self.u
		temp_data[1]=self.v
		data=struct.pack(self.binary_format, temp_data[0], temp_data[1])
		file.write(data)
	def dump (self):
		print "MD2 Texture Coordinate Structure"
		print "texture coordinate u: ",self.u
		print "texture coordinate v: ",self.v
		print ""
		
class md2_GL_command:
	s=0.0
	t=0.0
	vert_index=0
	binary_format="<2fi"
	
	def __init__(self):
		self.s=0.0
		self.t=0.0
		vert_index=0
	def save(self,file):
		temp_data=[0]*3
		temp_data[0]=float(self.s)
		temp_data[1]=float(self.t)
		temp_data[2]=self.vert_index
		data=struct.pack(self.binary_format, temp_data[0],temp_data[1],temp_data[2])
		file.write(data)
	def dump (self):
		print "MD2 OpenGL Command"
		print "s: ", self.s
		print "t: ", self.t
		print "Vertex Index: ", self.vert_index
		print ""

class md2_GL_cmd_list:
	num=0
	cmd_list=[]
	binary_format="<i"
	
	def __init__(self):
		self.num=0
		self.cmd_list=[]
	
	def save(self,file):
		data=struct.pack(self.binary_format, self.num)
		file.write(data)
		for cmd in self.cmd_list:
			cmd.save(file)
	def dump(self):
		print "MD2 OpenGL Command List"
		print "number: ", self.num
		for cmd in self.cmd_list:
			cmd.dump()
		print ""

class md2_skin:
	name=""
	binary_format="<64s"
	def __init__(self):
		self.name=""
	def save(self, file):
		temp_data=self.name
		data=struct.pack(self.binary_format, temp_data)
		file.write(data)
	def dump (self):
		print "MD2 Skin"
		print "skin name: ",self.name
		print ""
		
class md2_frame:
	scale=[]
	translate=[]
	name=[]
	vertices=[]
	binary_format="<3f3f16s"

	def __init__(self):
		self.scale=[0.0]*3
		self.translate=[0.0]*3
		self.name=""
		self.vertices=[]
	def save(self, file):
		temp_data=[0]*7
		temp_data[0]=float(self.scale[0])
		temp_data[1]=float(self.scale[1])
		temp_data[2]=float(self.scale[2])
		temp_data[3]=float(self.translate[0])
		temp_data[4]=float(self.translate[1])
		temp_data[5]=float(self.translate[2])
		temp_data[6]=self.name
		data=struct.pack(self.binary_format, temp_data[0],temp_data[1],temp_data[2],temp_data[3],temp_data[4],temp_data[5],temp_data[6])
		file.write(data)
	def dump (self):
		print "MD2 Frame"
		print "scale x: ",self.scale[0]
		print "scale y: ",self.scale[1]
		print "scale z: ",self.scale[2]
		print "translate x: ",self.translate[0]
		print "translate y: ",self.translate[1]
		print "translate z: ",self.translate[2]
		print "name: ",self.name
		print ""
		
class md2_obj:
	#Header Structure
	ident=0				#int 0	This is used to identify the file
	version=0			#int 1	The version number of the file (Must be 8)
	skin_width=0		#int 2	The skin width in pixels
	skin_height=0		#int 3	The skin height in pixels
	frame_size=0		#int 4	The size in bytes the frames are
	num_skins=0			#int 5	The number of skins associated with the model
	num_vertices=0		#int 6	The number of vertices (constant for each frame)
	num_tex_coords=0	#int 7	The number of texture coordinates
	num_faces=0			#int 8	The number of faces (polygons)
	num_GL_commands=0	#int 9	The number of gl commands
	num_frames=0		#int 10	The number of animation frames
	offset_skins=0		#int 11	The offset in the file for the skin data
	offset_tex_coords=0	#int 12	The offset in the file for the texture data
	offset_faces=0		#int 13	The offset in the file for the face data
	offset_frames=0		#int 14	The offset in the file for the frames data
	offset_GL_commands=0#int 15	The offset in the file for the gl commands data
	offset_end=0		#int 16	The end of the file offset
	binary_format="<17i"  #little-endian (<), 17 integers (17i)
	#md2 data objects
	tex_coords=[]
	faces=[]
	frames=[]
	skins=[]
	GL_commands=[]
	
	def __init__ (self):
		self.tex_coords=[]
		self.faces=[]
		self.frames=[]
		self.skins=[]
	def save(self, file):
		temp_data=[0]*17
		temp_data[0]=self.ident
		temp_data[1]=self.version
		temp_data[2]=self.skin_width
		temp_data[3]=self.skin_height
		temp_data[4]=self.frame_size
		temp_data[5]=self.num_skins
		temp_data[6]=self.num_vertices
		temp_data[7]=self.num_tex_coords
		temp_data[8]=self.num_faces
		temp_data[9]=self.num_GL_commands
		temp_data[10]=self.num_frames
		temp_data[11]=self.offset_skins
		temp_data[12]=self.offset_tex_coords
		temp_data[13]=self.offset_faces
		temp_data[14]=self.offset_frames
		temp_data[15]=self.offset_GL_commands
		temp_data[16]=self.offset_end
		data=struct.pack(self.binary_format, temp_data[0],temp_data[1],temp_data[2],temp_data[3],temp_data[4],temp_data[5],temp_data[6],temp_data[7],temp_data[8],temp_data[9],temp_data[10],temp_data[11],temp_data[12],temp_data[13],temp_data[14],temp_data[15],temp_data[16])
		file.write(data)
		#write the skin data
		for skin in self.skins:
			skin.save(file)
		#save the texture coordinates
		for tex_coord in self.tex_coords:
			tex_coord.save(file)
		#save the face info
		for face in self.faces:
			face.save(file)
		#save the frames
		for frame in self.frames:
			frame.save(file)
			for vert in frame.vertices:
				vert.save(file)
		#save the GL command List
		for cmd in self.GL_commands:
			cmd.save(file)
	def dump (self):
		print "Header Information"
		print "ident: ", self.ident
		print "version: ", self.version
		print "skin width: ", self.skin_width
		print "skin height: ", self.skin_height
		print "frame size: ", self.frame_size
		print "number of skins: ", self.num_skins
		print "number of texture coordinates: ", self.num_tex_coords
		print "number of faces: ", self.num_faces
		print "number of frames: ", self.num_frames
		print "number of vertices: ", self.num_vertices
		print "number of GL commands: ",self.num_GL_commands
		print "offset skins: ", self.offset_skins
		print "offset texture coordinates: ", self.offset_tex_coords
		print "offset faces: ", self.offset_faces
		print "offset frames: ",self.offset_frames
		print "offset GL Commands: ",self.offset_GL_commands
		print "offset end: ",self.offset_end
		print ""

######################################################
# Validation
######################################################
def validation(object):
	global user_frame_list
	
	#get access to the mesh data
	mesh=object.getData(False, True) #get the object (not just name) and the Mesh, not NMesh

	#check it's composed of only tri's	
	result=0
	for face in mesh.faces:
		if len(face.verts)!=3:
			#select the face for future triangulation
			face.sel=1
			if result==0:  #first time we have this problem, don't pop-up a window every time it finds a quad
			  print "Model not made entirely of triangles"
			  result=Blender.Draw.PupMenu("Model not made entirely out of Triangles-Convert?%t|YES|NO")
	
	print "result: ", result
	#triangulate or quit
	if result==1:
		#selecting face mode
		Blender.Mesh.Mode(3)
		mesh.quadToTriangle(0) #use closest verticies in breaking a quad
	elif result==2:
		return False #user will fix (I guess)

	#check it has UV coordinates
	if mesh.vertexUV==True:
		print "Vertex UV not supported"
		result=Blender.Draw.PupMenu("Vertex UV not suppored-Use Sticky UV%t|OK")
		return False
			
	elif mesh.faceUV==True:
		for face in mesh.faces:
			if(len(face.uv)==3):
				pass
			else:
				print "Models vertices do not all have UV"
				result=Blender.Draw.PupMenu("Models vertices do not all have UV%t|OK")
				return False
	
	else:
		print "Model does not have UV (face or vertex)"
		result=Blender.Draw.PupMenu("Model does not have UV (face or vertex)%t|OK")
		return False

	#check it has only 1 associated texture map
	last_face=""
	last_face=mesh.faces[0].image
	if last_face=="":
		print "Model does not have a texture Map"
		result=Blender.Draw.PupMenu("Model does not have a texture Map%t|OK")
		return False

	for face in mesh.faces:
		mesh_image=face.image
		if not mesh_image:
			print "Model has a face without a texture Map"
			result=Blender.Draw.PupMenu("Model has a face without a texture Map%t|OK")
			return False
		if mesh_image!=last_face:
			print "Model has more than 1 texture map assigned"
			result=Blender.Draw.PupMenu("Model has more than 1 texture map assigned%t|OK")
			return False
		
	size=mesh_image.getSize()
	#is this really what the user wants
	if (size[0]!=256 or size[1]!=256):
		print "Texture map size is non-standard (not 256x256), it is: ",size[0],"x",size[1]
		result=Blender.Draw.PupMenu("Texture map size is non-standard (not 256x256), it is: "+str(size[0])+"x"+str(size[1])+": Continue?%t|YES|NO")
		if(result==2):
			return False

	#verify frame list data
	user_frame_list=get_frame_list()	
	temp=user_frame_list[len(user_frame_list)-1]
	temp_num_frames=temp[2]
	
	#verify tri/vert/frame counts are within MD2 standard
	face_count=len(mesh.faces)
	vert_count=len(mesh.verts)	
	frame_count=temp_num_frames
	
	if face_count>MD2_MAX_TRIANGLES:
		print "Number of triangles exceeds MD2 standard: ", face_count,">",MD2_MAX_TRIANGLES
		result=Blender.Draw.PupMenu("Number of triangles exceeds MD2 standard: Continue?%t|YES|NO")
		if(result==2):
			return False
	if vert_count>MD2_MAX_VERTICES:
		print "Number of verticies exceeds MD2 standard",vert_count,">",MD2_MAX_VERTICES
		result=Blender.Draw.PupMenu("Number of verticies exceeds MD2 standard: Continue?%t|YES|NO")
		if(result==2):
			return False
	if frame_count>MD2_MAX_FRAMES:
		print "Number of frames exceeds MD2 standard of",frame_count,">",MD2_MAX_FRAMES
		result=Blender.Draw.PupMenu("Number of frames exceeds MD2 standard: Continue?%t|YES|NO")
		if(result==2):
			return False
	#model is OK
	return True

######################################################
# Fill MD2 data structure
######################################################
def fill_md2(md2, object):
	global user_frame_list
	#get a Mesh, not NMesh
	mesh=object.getData(False, True)	
	
	#load up some intermediate data structures
	tex_list={}
	tex_count=0
	#create the vertex list from the first frame
	Blender.Set("curframe", 1)
	
	#header information
	md2.ident=844121161
	md2.version=8	
	md2.num_vertices=len(mesh.verts)
	md2.num_faces=len(mesh.faces)

	#get the skin information
	#use the first faces' image for the texture information
	mesh_image=mesh.faces[0].image
	size=mesh_image.getSize()
	md2.skin_width=size[0]
	md2.skin_height=size[1]
	md2.num_skins=1
	#add a skin node to the md2 data structure
	md2.skins.append(md2_skin())
	md2.skins[0].name=Blender.sys.basename(mesh_image.getFilename())

	#put texture information in the md2 structure
	#build UV coord dictionary (prevents double entries-saves space)
	for face in mesh.faces:
		for i in range(0,3):
			t=(face.uv[i])
			tex_key=(t[0],t[1])
			if not tex_list.has_key(tex_key):
				tex_list[tex_key]=tex_count
				tex_count+=1
	md2.num_tex_coords=tex_count #each vert has its own UV coord

	for this_tex in range (0, md2.num_tex_coords):
		md2.tex_coords.append(md2_tex_coord())
	for coord, index in tex_list.iteritems():
		#md2.tex_coords.append(md2_tex_coord())
		md2.tex_coords[index].u=int(coord[0]*md2.skin_width)
		md2.tex_coords[index].v=int((1-coord[1])*md2.skin_height)

	#put faces in the md2 structure
	#for each face in the model
	for this_face in range(0, md2.num_faces):
		md2.faces.append(md2_face())
		for i in range(0,3):
			#blender uses indexed vertexes so this works very well
			md2.faces[this_face].vertex_index[i]=mesh.faces[this_face].verts[i].index
			#lookup texture index in dictionary
			uv_coord=(mesh.faces[this_face].uv[i])
			tex_key=(uv_coord[0],uv_coord[1])
			tex_index=tex_list[tex_key]
			md2.faces[this_face].texture_index[i]=tex_index

	#compute GL commands
	md2.num_GL_commands=build_GL_commands(md2)

	#get the frame data
	#calculate 1 frame size  + (1 vert size*num_verts)
	md2.frame_size=40+(md2.num_vertices*4) #in bytes
	
	#get the frame list
	user_frame_list=get_frame_list()
	if user_frame_list=="default":
		md2.num_frames=198
	else:
		temp=user_frame_list[len(user_frame_list)-1]  #last item
		md2.num_frames=temp[2] #last frame number
	
	#fill in each frame with frame info and all the vertex data for that frame
	for frame_counter in range(0,md2.num_frames):
		#add a frame
		md2.frames.append(md2_frame())
		#update the mesh objects vertex positions for the animation
		Blender.Set("curframe", frame_counter)  #set blender to the correct frame
		mesh.getFromObject(object.name)  #update the mesh to make verts current
		
#each frame has a scale and transform value that gets the vertex value between 0-255
#since the scale and transform are the same for the all the verts in the frame, we only need
#to figure this out once per frame
		
		#we need to start with the bounding box
		bounding_box=object.getBoundBox() #uses the object, not the mesh data
		#initialize with the first vertex for both min and max.  X and Y are swapped for MD2 format
		point=bounding_box[0]
		frame_min_x=point[1]
		frame_max_x=point[1]
		frame_min_y=point[0]
		frame_max_y=point[0]
		frame_min_z=point[2]
		frame_max_z=point[2]
		#find min/max values
		for point in bounding_box:
			if frame_min_x>point[1]: frame_min_x=point[1]
			if frame_max_x<point[1]: frame_max_x=point[1]
			if frame_min_y>point[0]: frame_min_y=point[0]
			if frame_max_y<point[0]: frame_max_y=point[0]
			if frame_min_z>point[2]: frame_min_z=point[2]
			if frame_max_z<point[2]: frame_max_z=point[2]

		#the scale is the difference between the min and max (on that axis) / 255
		frame_scale_x=(frame_max_x-frame_min_x)/255
		frame_scale_y=(frame_max_y-frame_min_y)/255
		frame_scale_z=(frame_max_z-frame_min_z)/255
		
		#translate value of the mesh to center it on the origin
		frame_trans_x=frame_min_x
		frame_trans_y=frame_min_y
		frame_trans_z=frame_min_z
		
		#fill in the data
		md2.frames[frame_counter].scale=(-frame_scale_x, frame_scale_y, frame_scale_z)
		md2.frames[frame_counter].translate=(-frame_trans_x, frame_trans_y, frame_trans_z)
		
		#now for the vertices
		for vert_counter in range(0, md2.num_vertices):
			#add a vertex to the md2 structure
			md2.frames[frame_counter].vertices.append(md2_point())
			#figure out the new coords based on scale and transform
			#then translates the point so it's not less than 0
			#then scale it so it's between 0..255
			new_x=int((mesh.verts[vert_counter].co[1]-frame_trans_x)/frame_scale_x)
			new_y=int((mesh.verts[vert_counter].co[0]-frame_trans_y)/frame_scale_y)
			new_z=int((mesh.verts[vert_counter].co[2]-frame_trans_z)/frame_scale_z)
			#put them in the structure
			md2.frames[frame_counter].vertices[vert_counter].vertices=(new_x, new_y, new_z)

			#need to add the lookup table check here
			maxdot = -999999.0;
			maxdotindex = -1;

			for j in range(0,162):
				#dot = (x[0]*y[0]+x[1]*y[1]+x[2]*y[2])
				x1=mesh.verts[vert_counter].no[1]
				y1=mesh.verts[vert_counter].no[0]
				z1=mesh.verts[vert_counter].no[2]
				dot = (x1*MD2_NORMALS[j][0]+
				       y1*MD2_NORMALS[j][1]+
							 z1*MD2_NORMALS[j][2]);
				if (dot > maxdot):
					maxdot = dot;
					maxdotindex = j;
			
			md2.frames[frame_counter].vertices[vert_counter].lightnormalindex=maxdotindex	
			
			
			
			
	#output all the frame names-user_frame_list is loaded during the validation
	for frame_set in user_frame_list:
		for counter in range(frame_set[1]-1, frame_set[2]):
			md2.frames[counter].name=frame_set[0]+"_"+str(counter-frame_set[1]+2)

	#compute these after everthing is loaded into a md2 structure
	header_size=17*4 #17 integers, and each integer is 4 bytes
	skin_size=64*md2.num_skins #64 char per skin * number of skins
	tex_coord_size=4*md2.num_tex_coords #2 short * number of texture coords
	face_size=12*md2.num_faces #3 shorts for vertex index, 3 shorts for tex index
	frames_size=(((12+12+16)+(4*md2.num_vertices)) * md2.num_frames) #frame info+verts per frame*num frames
	GL_command_size=md2.num_GL_commands*4 #each is an int or float, so 4 bytes per
	
	#fill in the info about offsets
	md2.offset_skins=0+header_size
	md2.offset_tex_coords=md2.offset_skins+skin_size
	md2.offset_faces=md2.offset_tex_coords+tex_coord_size
	md2.offset_frames=md2.offset_faces+face_size
	md2.offset_GL_commands=md2.offset_frames+frames_size
	md2.offset_end=md2.offset_GL_commands+GL_command_size

######################################################
# Get Frame List
######################################################
def get_frame_list():
	global g_frame_filename
	frame_list=[]

	if g_frame_filename.val=="default":
		return MD2_FRAME_NAME_LIST

	else:
	#check for file
		if (Blender.sys.exists(g_frame_filename.val)==1):
			#open file and read it in
			file=open(g_frame_filename.val,"r")
			lines=file.readlines()
			file.close()

			#check header (first line)
			if lines[0]<>"# MD2 Frame Name List\n":
				print "its not a valid file"
				result=Blender.Draw.PupMenu("This is not a valid frame definition file-using default%t|OK")
				return MD2_FRAME_NAME_LIST
			else:
				#read in the data
				num_frames=0
				for counter in range(1, len(lines)):
					current_line=lines[counter]
					if current_line[0]=="#":
						#found a comment
						pass
					else:
						data=current_line.split()
						frame_list.append([data[0],num_frames+1, num_frames+int(data[1])])
						num_frames+=int(data[1])
				return frame_list
		else:
			print "Cannot find file"
			result=Blender.Draw.PupMenu("Cannot find frame definion file-using default%t|OK")
			return MD2_FRAME_NAME_LIST

######################################################
# Tri-Strip/Tri-Fan functions
######################################################
def find_strip_length(md2, start_tri, start_vert):
	#variables shared between fan and strip functions
	global used
	global strip_vert
	global strip_st
	global strip_tris
	global strip_count

	m1=m2=0
	st1=st2=0
	
	used[start_tri]=2

	last=start_tri

	strip_vert[0]=md2.faces[last].vertex_index[start_vert%3]
	strip_vert[1]=md2.faces[last].vertex_index[(start_vert+1)%3]
	strip_vert[2]=md2.faces[last].vertex_index[(start_vert+2)%3]

	strip_st[0]=md2.faces[last].texture_index[start_vert%3]
	strip_st[1]=md2.faces[last].texture_index[(start_vert+1)%3]
	strip_st[2]=md2.faces[last].texture_index[(start_vert+2)%3]

	strip_tris[0]=start_tri
	strip_count=1

	m1=md2.faces[last].vertex_index[(start_vert+2)%3]
	st1=md2.faces[last].texture_index[(start_vert+2)%3]
	m2=md2.faces[last].vertex_index[(start_vert+1)%3]
	st2=md2.faces[last].texture_index[(start_vert+1)%3]
	
	#look for matching triangle
	check=start_tri+1
	
	for tri_counter in range(start_tri+1, md2.num_faces):
		
		for k in range(0,3):
			if md2.faces[check].vertex_index[k]!=m1:
				continue
			if md2.faces[check].texture_index[k]!=st1:
				continue
			if md2.faces[check].vertex_index[(k+1)%3]!=m2:
				continue
			if md2.faces[check].texture_index[(k+1)%3]!=st2:
				continue
			
			#if we can't use this triangle, this tri_strip is done
			if (used[tri_counter]!=0):
				for clear_counter in range(start_tri+1, md2.num_faces):
					if used[clear_counter]==2:
						used[clear_counter]=0
				return strip_count

			#new edge
			if (strip_count & 1):
				m2=md2.faces[check].vertex_index[(k+2)%3]
				st2=md2.faces[check].texture_index[(k+2)%3]
			else:
				m1=md2.faces[check].vertex_index[(k+2)%3]
				st1=md2.faces[check].texture_index[(k+2)%3]
			
			strip_vert[strip_count+2]=md2.faces[tri_counter].vertex_index[(k+2)%3]
			strip_st[strip_count+2]=md2.faces[tri_counter].texture_index[(k+2)%3]
			strip_tris[strip_count]=tri_counter
			strip_count+=1
	
			used[tri_counter]=2
		check+=1
	return strip_count

def find_fan_length(md2, start_tri, start_vert):
	#variables shared between fan and strip functions
	global used
	global strip_vert
	global strip_st
	global strip_tris
	global strip_count

	m1=m2=0
	st1=st2=0
	
	used[start_tri]=2

	last=start_tri

	strip_vert[0]=md2.faces[last].vertex_index[start_vert%3]
	strip_vert[1]=md2.faces[last].vertex_index[(start_vert+1)%3]
	strip_vert[2]=md2.faces[last].vertex_index[(start_vert+2)%3]
	
	strip_st[0]=md2.faces[last].texture_index[start_vert%3]
	strip_st[1]=md2.faces[last].texture_index[(start_vert+1)%3]
	strip_st[2]=md2.faces[last].texture_index[(start_vert+2)%3]

	strip_tris[0]=start_tri
	strip_count=1

	m1=md2.faces[last].vertex_index[(start_vert+0)%3]
	st1=md2.faces[last].texture_index[(start_vert+0)%3]
	m2=md2.faces[last].vertex_index[(start_vert+2)%3]
	st2=md2.faces[last].texture_index[(start_vert+2)%3]

	#look for matching triangle	
	check=start_tri+1
	for tri_counter in range(start_tri+1, md2.num_faces):
		for k in range(0,3):
			if md2.faces[check].vertex_index[k]!=m1:
				continue
			if md2.faces[check].texture_index[k]!=st1:
				continue
			if md2.faces[check].vertex_index[(k+1)%3]!=m2:
				continue
			if md2.faces[check].texture_index[(k+1)%3]!=st2:
				continue
			
			#if we can't use this triangle, this tri_strip is done
			if (used[tri_counter]!=0):
				for clear_counter in range(start_tri+1, md2.num_faces):
					if used[clear_counter]==2:
						used[clear_counter]=0
				return strip_count

			#new edge
			m2=md2.faces[check].vertex_index[(k+2)%3]
			st2=md2.faces[check].texture_index[(k+2)%3]
			
			strip_vert[strip_count+2]=m2
			strip_st[strip_count+2]=st2
			strip_tris[strip_count]=tri_counter
			strip_count+=1
	
			used[tri_counter]=2
		check+=1
	return strip_count


######################################################
# Globals for GL command list calculations
######################################################
used=[]
strip_vert=0
strip_st=0
strip_tris=0
strip_count=0

######################################################
# Build GL command List
######################################################
def build_GL_commands(md2):
	#variables shared between fan and strip functions
	global used
	used=[0]*md2.num_faces
	global strip_vert
	strip_vert=[0]*128
	global strip_st
	strip_st=[0]*128
	global strip_tris
	strip_tris=[0]*128
	global strip_count
	strip_count=0
	
	#variables
	num_commands=0
	start_vert=0
	fan_length=strip_length=0
	length=best_length=0
	best_type=0
	best_vert=[0]*1024
	best_st=[0]*1024
	best_tris=[0]*1024
	s=0.0
	t=0.0
	
	for face_counter in range(0,md2.num_faces):
		if used[face_counter]!=0: #don't evaluate a tri that's been used
			#print "found a used triangle: ", face_counter
			pass
		else:
			best_length=0 #restart the counter
			#for each vertex index in this face
			for start_vert in range(0,3):
				fan_length=find_fan_length(md2, face_counter, start_vert)
				if (fan_length>best_length):
					best_type=1
					best_length=fan_length
					for index in range (0, best_length+2):
						best_st[index]=strip_st[index]
						best_vert[index]=strip_vert[index]
					for index in range(0, best_length):
						best_tris[index]=strip_tris[index]
				
				strip_length=find_strip_length(md2, face_counter, start_vert)
				if (strip_length>best_length): 
					best_type=0
					best_length=strip_length
					for index in range (0, best_length+2):
						best_st[index]=strip_st[index]
						best_vert[index]=strip_vert[index]
					for index in range(0, best_length):
						best_tris[index]=strip_tris[index]
			
			#mark the tris on the best strip/fan as used
			for used_counter in range (0, best_length):
				used[best_tris[used_counter]]=1
	
			temp_cmdlist=md2_GL_cmd_list()
			#push the number of commands into the command stream
			if best_type==1:
				temp_cmdlist.num=best_length+2
				num_commands+=1
			else:	
				temp_cmdlist.num=(-(best_length+2))
				num_commands+=1
			for command_counter in range (0, best_length+2):
				#emit a vertex into the reorder buffer
				cmd=md2_GL_command()
				index=best_st[command_counter]
				#calc and put S/T coords in the structure
				s=md2.tex_coords[index].u
				t=md2.tex_coords[index].v
				s=(s+0.5)/md2.skin_width
				t=(t+0.5)/md2.skin_height
				cmd.s=s
				cmd.t=t
				cmd.vert_index=best_vert[command_counter]
				temp_cmdlist.cmd_list.append(cmd)
				num_commands+=3
			md2.GL_commands.append(temp_cmdlist)
	
	#end of list
	temp_cmdlist=md2_GL_cmd_list()	
	temp_cmdlist.num=0
	md2.GL_commands.append(temp_cmdlist)  
	num_commands+=1
	
	#cleanup and return
	used=best_vert=best_st=best_tris=strip_vert=strip_st=strip_tris=0
	return num_commands

######################################################
# Save MD2 Format
######################################################
def save_md2(filename):
	md2=md2_obj()  #blank md2 object to save

	#get the object
	mesh_objs = Blender.Object.GetSelected()

	#check there is a blender object selected
	if len(mesh_objs)==0:
		print "Fatal Error: Must select a mesh to output as MD2"
		print "Found nothing"
		result=Blender.Draw.PupMenu("Must select an object to export%t|OK")
		return

	mesh_obj=mesh_objs[0] #this gets the first object (should be only one)

	#check if it's a mesh object
	if mesh_obj.getType()!="Mesh":
		print "Fatal Error: Must select a mesh to output as MD2"
		print "Found: ", mesh_obj.getType()
		result=Blender.Draw.PupMenu("Selected Object must be a mesh to output as MD2%t|OK")
		return

	ok=validation(mesh_obj)
	if ok==False:
		return
	
	fill_md2(md2, mesh_obj)
	md2.dump()
	
	#actually write it to disk
	file=open(filename,"wb")
	md2.save(file)
	file.close()
	
	#cleanup
	md2=0
	
	print "Closed the file"