-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCRExport.ms
More file actions
2852 lines (2473 loc) · 78.6 KB
/
Copy pathCRExport.ms
File metadata and controls
2852 lines (2473 loc) · 78.6 KB
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
fn writeCNTheader f =
(
writebyte f 69 #unsigned
writebyte f 35 #unsigned
writebyte f 0 #unsigned
writebyte f 4 #unsigned
)
fn writeMDLheader f =
(
writebyte f 69 #unsigned
writebyte f 35 #unsigned
writebyte f 2 #unsigned
writebyte f 6 #unsigned
)
fn AdjEdgeSortByRefB e1 e2 =
(
case of
(
(e1.refb < e2.refb): -1
(e1.refb > e2.refb): 1
default: 0
)
)
fn AdjEdgeSortByRefA e1 e2 =
(
case of
(
(e1.refa < e2.refa): -1
(e1.refa > e2.refa): 1
default: 0
)
)
fn AdjEdgeSortByFace e1 e2 =
(
case of
(
(e1.face < e2.face): -1
(e1.face > e2.face): 1
default: 0
)
)
octree = undefined
fn AdjEdgeSortAll &arrayToSort =
(
sorted = #()
--print "Sorting Edge Array"
--format "\nOriginal array size: %\n" arrayToSort.count
--format "Original array:\n%\n" arrayToSort
firstPass = deepcopy arrayToSort
qsort firstPass AdjEdgeSortByRefb
--format "\nFirst Pass size: %\n" firstPass.count
--format "First Pass:\n%\n" firstPass
-- PrintArrayInFull firstPass "First Pass"
secondPass = #()
currentRefB = firstPass[1]
tmp = #()
for i=1 to firstPass.count do
(
if currentRefB == firstPass[i].refb then
(
append tmp firstPass[i]
)
else
(
qsort tmp AdjEdgeSortByRefA
join secondPass tmp
tmp=#()
append tmp firstPass[i]
currentRefB = firstPass[i].refb
)
)
qsort tmp AdjEdgeSortByRefA
join secondPass tmp
tmp=#()
--format "\nSecond Pass: %\n" secondPass.count
--format "Second Pass:\n%\n" secondPass
-- PrintArrayInFull secondPass "Second Pass"
currentRefB = secondPass[1].refb
currentRefA = secondPass[1].refa
for i=1 to secondPass.count do
(
if currentRefB == secondPass[i].refb and currentRefA == secondPass[i].refa then
(
append tmp secondPass[i]
)
else
(
qsort tmp AdjEdgeSortByFace
join sorted tmp
tmp=#()
append tmp secondPass[i]
currentRefB = secondPass[i].refb
currentRefA = secondPass[i].refa
)
)
qsort tmp AdjEdgeSortByFace
join sorted tmp
free tmp
free firstPass
free secondPass
--gc()
--format "\nThird Pass: %\n" sorted.count
--format "Third Pass:\n%\n" sorted
-- PrintArrayInFull sorted "Final Pass"
sorted
)
fn FindAdjacentTriEdge tri v1 v2 =
(
result = 255
if tri.ref[1] == v1 and tri.ref[2] == v2 do result= 1
if tri.ref[1] == v2 and tri.ref[2] == v1 do result=1
if tri.ref[1] == v1 and tri.ref[3] == v2 do result=2
if tri.ref[1] == v2 and tri.ref[3] == v1 do result=2
if tri.ref[2] == v1 and tri.ref[3] == v2 do result=3
if tri.ref[2] == v2 and tri.ref[3] == v1 do result=3
result
)
fn FindOppositeTriVert tri v1 v2 =
(
result=-1
if tri.ref[1] == v1 and tri.ref[2] == v2 do result= 3
if tri.ref[1] == v2 and tri.ref[2] == v1 do result= 3
if tri.ref[1] == v1 and tri.ref[3] == v2 do result= 2
if tri.ref[1] == v2 and tri.ref[3] == v1 do result= 2
if tri.ref[2] == v1 and tri.ref[3] == v2 do result= 1
if tri.ref[2] == v2 and tri.ref[3] == v1 do result= 1
result
)
fn CreateAdjacencyDB faceCount &indexBuffer =
(
global AdjTris
global AdjEdges
if AdjTris == undefined then AdjTris = #()
else
(
free AdjTris
AdjTris = #()
)
if AdjEdges == undefined then AdjEdges = #()
else
(
free AdjEdges
AdjEdges = #()
)
--gc()
currentFace = 1
currentEdge = 1
edgeCount = 0
indexBufferCount = indexBuffer.count
--format "IndexBufferCount: %\n" indexBufferCount
--format "FaceCount: %\n" faceCount
for i=0 to (faceCount-1) do
(
ind1 = i * 3 + 1
ind2 = i * 3 + 3
ind3 = i * 3 + 2
tri1 = -1
tri2 = -1
tri3 = -1
vind1 = indexBuffer[ind1]
vind2 = indexBuffer[ind2]
vind3 = indexBuffer[ind3]
--if ind1 > indexBufferCount or ind2 > indexBufferCount or ind3 > indexBufferCount do exit
newTri = AdjacentTriangle ref: #(vind1, vind2, vind3) tri: #(tri1, tri2, tri3)
--format "newTri: %\n" newTri
append AdjTris newTri
if vind1 < vind2 then
(
newEdge = AdjacentEdge refa: vind1 refb: vind2 face: currentFace
currentEdge = currentEdge + 1
edgeCount = edgeCount + 1
append AdjEdges newEdge
)
else
(
newEdge = AdjacentEdge refa: vind2 refb: vind1 face: currentFace
currentEdge = currentEdge + 1
edgeCount = edgeCount + 1
append AdjEdges newEdge
)
if vind1 < vind3 then
(
newEdge = AdjacentEdge refa: vind1 refb: vind3 face: currentFace
currentEdge = currentEdge + 1
edgeCount = edgeCount + 1
append AdjEdges newEdge
)
else
(
newEdge = AdjacentEdge refa: vind3 refb: vind1 face: currentFace
currentEdge = currentEdge + 1
edgeCount = edgeCount + 1
append AdjEdges newEdge
)
if vind2 < vind3 then
(
newEdge = AdjacentEdge refa: vind2 refb: vind3 face: currentFace
currentEdge = currentEdge + 1
edgeCount = edgeCount + 1
append AdjEdges newEdge
)
else
(
newEdge = AdjacentEdge refa: vind3 refb: vind2 face: currentFace
currentEdge = currentEdge + 1
edgeCount = edgeCount + 1
append AdjEdges newEdge
)
currentFace = currentFace + 1
)
Sorted = AdjEdgeSortAll &AdjEdges
--qSort Sorted AdjEdgeSortByRefB
--qSort Sorted AdjEdgeSortByRefA
--qSort Sorted AdjEdgeSortByFace
--format "AdjEdges: \n%\n" AdjEdges
--format "Sorted: \n%\n" Sorted
local lastRefA = Sorted[1].refa
local lastRefB = Sorted[1].refb
--PrintArrayInFull AdjEdges "AdjEdges"
--PrintArrayInFull Sorted "Sorted"
local count = 0
tmp = #()
--print "Starting Edge Looping"
for i=1 to edgeCount do
(
local face = Sorted[i].face
local refa = Sorted[i].refa
local refb = Sorted[i].refb
--format "%:\tface: %\t\trefa: %\t\trefb: %\n" i face refa refb
--format "\t\t\tlastrefa: %\tlastrefb: %\n" lastRefA lastRefB
--print "\n-------\n"
--format "Loop: % Face: %\n" i face
--format "Refa: % Refb: %\n" refa refb
--format "lastRefA: % lastRefB: %\n" lastRefA lastRefB
--format "Count: % \n" count
if refa == lastRefA and refb == lastRefB then
(
--print "found matching edge"
count = count+1
tmp[count] = face
)
else
(
--print "not matching edge"
if count == 2 then
(
--print "count is 2 so collecting"
triInd1 = tmp[1]
triInd2 = tmp[2]
--format "Edge #% is connected to edge #%, setting connection between face % and face %\n" (i-2) (i-1) triInd1 triInd2
tri1 = AdjTris[triInd1]
tri2 = AdjTris[triInd2]
--format "triInd1: % triInd2: %\n" triInd1 triInd2
--format "tri1: % tri2: %\n" tri1 tri2
edge1 = FindAdjacentTriEdge tri1 lastRefA lastRefB
edge2 = FindAdjacentTriEdge tri2 lastRefA lastRefB
--format "edge1: % edge2: %" edge1 edge2
AdjTris[triInd1].tri[edge1] = triInd2
AdjTris[triInd2].tri[edge2] = triInd1
)
--else format "Edge #% has no connection, skipping\n" (i-1)
count = 0
count = count+1
tmp[count] = face
lastRefA = refa
lastRefB = refb
)
)
if count == 2 then
(
--format "Edge #% is connected to edge #%, setting connection\n" (edgeCount-1) edgeCount
triInd1 = tmp[1]
triInd2 = tmp[2]
tri1 = AdjTris[triInd1]
tri2 = AdjTris[triInd2]
edge1 = FindAdjacentTriEdge tri1 lastRefA lastRefB
edge2 = FindAdjacentTriEdge tri2 lastRefA lastRefB
AdjTris[triInd1].tri[edge1] = triInd2
AdjTris[triInd2].tri[edge2] = triInd1
--format "edge1: %\tedge2: %\ttriInd1: %\ttriInd2: %\n\ttri1: %\ttri2: %\n-----\n" edge1 edge2 triInd1 triInd2 tri1 tri2
)
--else format "Edge #% has no connection, skipping\n" edgeCount
--free tmp
--gc()
--PrintArrayInFull AdjTris "AdjTris"
--print "AdjacencyDB Done!"
)
fn TrackStrip face oldest middle &strip &faces tags offset =
(
global AdjTris
len = 2
faceCount = 1
--format "TrackStrip:\tface: %\toldest: %\tmiddle:%\n" face oldest middle
if 1 + offset <= strip.count then
(
strip[1 + offset] = oldest
)
else
(
append strip oldest
)
if 2 + offset <= strip.count then
(
strip[2 + offset] = middle
)
else
(
append strip middle
)
--format "TrackStrip:\tface: %\toldest: %\tmiddle:%\n" face oldest middle
bDoTheStrip = true
oldFace = face
itteration = 0
while bDoTheStrip do
(
itteration=itteration+1
newestIndex = FindOppositeTriVert (AdjTris[face]) oldest middle
newest = AdjTris[face].ref[newestIndex]
--format "TrackStrip Itteration %:\n\toldest: %\t\tmiddle: %\t\tnewest: %\t\tnewesstIndex: %\n" itteration oldest middle newest newestIndex
len = len + 1
if len + offset <= strip.count then
(
strip[len+offset] = newest
)
else append strip newest
if faceCount + offset <= faces.count then
(
faces[faceCount+offset] = face
)
else append faces face
faceCount = faceCount + 1
tags[face] = true
curEdge = FindAdjacentTriEdge AdjTris[face] middle newest
local tri = AdjTris[face]
triLink = AdjTris[face].tri[curEdge]
if triLink == -1 then
(
--format "Trilink = -1, stopping stipper\n"
bDoTheStrip = false
)
else
(
oldFace=face
face = triLink
if tags[face] == true do bDoTheStrip = false
)
oldest = middle
middle = newest
)
len
)
fn ComputeBestStrip face faceCount indices &tags &strips =
(
--print "Computing Best Strip"
strip = #()
strip[1] = #()
strip[2] = #()
strip[3] = #()
faces = #()
faces[1] = #()
faces[2] = #()
faces[3] = #()
len = #()
firstLen = #()
refs1 = #()
refs2 = #()
--PrintArrayInFull AdjTris "AdjTris"
refs1[1] = AdjTris[face].ref[1]
refs2[1] = AdjTris[face].ref[2]
refs1[2] = AdjTris[face].ref[3]
refs2[2] = AdjTris[face].ref[1]
refs1[3] = AdjTris[face].ref[2]
refs2[3] = AdjTris[face].ref[3]
--PrintArrayInFull refs1 "refs1"
--PrintArrayInFull refs2 "refs2"
for j=1 to 3 do
(
--format "\n\nComputeBestStrip loop #%\n" j
strip[j] = #()
faces[j] = #()
ltags = deepcopy tags
--format "Calling TrackStrip on\tface: %\trefs1[%]: %\trefs2[%]: %\n\n" face j refs1[j] j refs2[j]
len[j] = TrackStrip face refs1[j] refs2[j] &strip[j] &faces[j] ltags 0
--format "\n strip % length: %\n" j len[j]
firstLen[j] = len[j]
--PrintArrayInFull strip[j] "Strip for this loop"
--PrintArrayInFull faces[j] "Faces for this loop"
--format "FirstLen: % \tLen: %\n" firstLen[j] len[j]
for i=1 to (len[j] / 2) do
(
t = strip[j][i]
k = len[j] - (i-1)
strip[j][i] = strip[j][k]
strip[j][k] = t
)
for i=1 to ((len[j] - 2) / 2) do
(
t = faces[j][i]
k = len[j] - (i-1) - 2
faces[j][i] = faces[j][k]
faces[j][k] = t
)
--PrintArrayInFull strip[j] "Strip for this loop after swapping"
--PrintArrayInFull faces[j] "Faces for this loop after swapping"
newRef1 = strip[j][len[j] - 2]
newRef2 = strip[j][len[j] - 1]
--format "\nCalling TrackStrip again on\tface: %\tnewRef1: %\tnewRef2: %\t\toffset: %\n\n" face newRef1 newRef2 (len[j]-3)
extraLen = TrackStrip face newRef1 newRef2 &strip[j] &faces[j] ltags (len[j] - 3)
--format "ComputeBestStrip itteration % extraLen: %\n" j extraLen
len[j] = len[j] + extraLen - 3
--PrintArrayInFull strip[j] "Strip for this loop after second TrackStrip"
--PrintArrayInFull faces[j] "Faces for this loop after second TrackStrip"
--format "ExtraLen: % \tLen: %\n" extraLen len[j]
)
bestLen = len[1]
best = 1
if bestLen < len[2] do
(
bestLen = len[2]
best = 2
)
if bestLen < len[3] do
(
bestLen = len[3]
best = 3
)
--PrintArrayInFull len "len array"
nFaces = bestLen - 2
--print "------\nThis is the tags array before setting faces to true:"
--PrintArrayInFull tags "Tags:" ignoreUndefined:true
--PrintArrayInFull strip[1] "Strip[1]"
--PrintArrayInFull strip[2] "Strip[2]"
--PrintArrayInFull strip[3] "Strip[3]"
for j=1 to bestLen-2 do
(
tags[faces[best][j]] = true
)
--print "------\nAnd after:"
--PrintArrayInFull tags "Tags:" ignoreUndefined:true
--print "------\n"
if (bit.and firstLen[best] 1) == 1 do
(
if bestLen == 3 or bestLen == 4 then
(
t = strip[best][2]
strip[best][2] = strip[best][3]
strip[best][3] = t
--strip[best][2] = bit.xor strip[best][2] strip[best][3]
--strip[best][3] = bit.xor strip[best][3] strip[best][2]
--strip[best][2] = bit.xor strip[best][2] strip[best][3]
)
else
(
for i=1 to bestLen / 2 do
(
t = strip[best][i]
strip[best][i] = strip[best][bestLen - (i-1)]
strip[best][len[best] - (i-1) ] = t
)
if (bit.and (bestLen - firstLen[best]) 1) == 1 do
(
insertItem strip[best][1] strip[best] 1
bestLen = bestLen + 1
)
/*t = strip[best][1]
strip[best][1] = strip[best][2]
strip[best][2] = t*/
)
)
append strips (deepcopy strip[best])
--free strip
--free faces
--free len
--free firstLen
--free refs1
--free refs2
--gc()
--format "nFaces from this strip: %\n" nFaces
nFaces
)
fn MergeStrips &strips =
(
--print "Merge Strips"
bFirst = true
previousStrip = 1
mergedStrip = #()
removeOldStrip = #()
for k=1 to strips.count do
(
if strips[k].count == 3 do
(
removeOldStrip[k] = false
continue
)
if bFirst==false do
(
lastRef = strips[previousStrip][strips[previousStrip].count]
firstRef = strips[k][1]
append mergedStrip lastRef
append mergedStrip firstRef
if (bit.and mergedStrip.count 1) == 1 do
(
secondRef = strips[k][2]
if firstRef != secondRef then
(
append mergedStrip firstRef
)
else
(
deleteItem strips[k] 1
)
)
)
join mergedStrip strips[k]
bFirst = false
previousStrip = k
removeOldStrip[k]= true
)
--format "strips.count = % \n removeOldStrip.count = %\n" strips.count removeOldStrip.count
for i=strips.count to 2 by -1 do
(
if removeOldStrip[i] do
(
deleteItem strips i
)
)
insertItem mergedStrip strips 1
--free mergedStrip
--free removeOldStrip
--gc()
)
fn StripperShakeItBaby faceCount &indices =
(
--print "Shake It Baby!"
strips = #()
CreateAdjacencyDB faceCount &indices
tags = #()
connectivity = #()
for i=1 to faceCount do
(
connectivity[i] = i
)
nStrips = 0
totalFaces = 0
index = 1
loopnum=1
loopsWithoutChange = 0
while totalFaces < faceCount do
(
while tags[index] == true do
(
index=index+1
)
firstFace = connectivity[index]
--format "First Face of loop #% is: %\n" loopnum firstFace
bestStripFaces = ComputeBestStrip firstFace faceCount indices &tags &strips
if bestStripFaces <= 0 do loopsWithoutChange = loopsWithoutChange+1
totalFaces = totalFaces + bestStripFaces
nStrips = nStrips + 1
--format "TotalFaces on loop %: %\n" loopnum totalFaces
--format "FaceCount: %\n" faceCount
loopnum = loopnum + 1
if loopsWithoutChange > 15 do
(
print "OMG! Infinite Loop! Exiting!"
exit
)
)
--gc()
--for i =1 to strips.count do
--(
--stripname = stringstream""
--format "RawStrips[%]" i to:stripname
--PrintArrayInFull strips[i] (stripname as string)
--)
--format "Number Of Strips Before Merge: %\n" strips.count
MergeStrips &strips
--free tags
--free connectivity
--format "Number Of Strips After Merge: %\n" strips.count
--gc()
strips
)
fn ExportMTLMaterial mtlfilename mat = (
--Format "Exporting MTL to %\n" mtlfilename
if (custAttributes.get mat mtlmat_ca) != undefined then
(
mtl = MTLFile()
mtl.GetFromMaterial mat
mtl.Save mtlfilename
)
else (
Format "Error exporting MTL to %: CA doesn't exist!\n" mtlfilename
)
)
struct OtherTexturesDef (
TextureName,
Alias
)
fn ExportMaterial folder materialName bitmapName baseMaterial:"simple_base" diffuseAlias:"DiffuseColour" otherTextures:#() =
(
dotNet.loadAssembly "system.xml"
xmlDoc=dotNetObject "system.xml.xmlDocument"
--format "exporting mat %\\%\n.mt2" folder materialName
xmlRoot = xmlDoc.CreateElement "Material"
xmlDoc.AppendChild xmlRoot
xmlDecl = xmlDoc.CreateXmlDeclaration "1.0" "" ""
xmlDoc.InsertBefore xmlDecl xmlRoot
xmlBaseOff = xmlDoc.CreateElement "BasedOffOf"
xmlBaseOff.setAttribute "Name" baseMaterial
xmlRoot.AppendChild xmlBaseOff
xmlBaseOff = xmlDoc.CreateElement "DoubleSided"
xmlBaseOff.setAttribute "Value" "TRUE"
xmlRoot.AppendChild xmlBaseOff
xmlPass = xmlDoc.CreateElement "Pass"
xmlPass.SetAttribute "Number" "0"
--xmlRoot.AppendChild xmlPass
xmlTexture = xmlDoc.CreateElement "Texture"
xmlTexture.SetAttribute "Alias" diffuseAlias
if bitmapName != undefined and bitmapName != "" then textureFileName = getFilenameFile bitmapName
else textureFileName = "NOTEXTURE"
xmlTexture.SetAttribute "FileName" textureFileName
xmlRoot.AppendChild xmlTexture
if otherTextures != undefined then (
for otherTexture in otherTextures do (
xmlOtherTexture = xmlDoc.CreateElement "Texture"
xmlOtherTexture.SetAttribute "Alias" otherTexture.Alias
if otherTexture.TextureName != undefined and otherTexture.TextureName != "" then textureFileName = getFilenameFile otherTexture.TextureName
else textureFileName = "NOTEXTURE"
xmlOtherTexture.setAttribute "FileName" textureFileName
xmlRoot.AppendChild xmlOtherTexture
)
)
xmlDoc.Save ((dotnetclass "System.IO.Path").Combine folder (materialName+".MT2"))
free xmlDoc
free xmlRoot
free xmlDecl
free xmlDoubleSided
free xmlBaseOff
free xmlPass
free xmlTexture
--gc()
)
fn ExportMaterialsAndTextures outputFolder exportTextures:true exportMats:true funsizeExport:false onlyUsedByMesh:undefined baseMaterial:"simple_base" diffuseAlias:"DiffuseColour" transparentDiffuseAlias:"DiffuseColour" baseTransparentMaterial:"glass_base" alphaTextureName:"" alphaTextureIndex:0 forceAlphaReplacement:false =
(
global MaterialsToExport
if(MaterialsToExport==undefined) then (
MaterialsToExport = #()
)
bitmaps = #()
rawBitmaps = #()
rawAlphas = #()
--print MaterialsToExport
for i=1 to MaterialsToExport.count do
(
mtlName = MaterialsToExport[i][1]
baseMat = baseMaterial
diffAlias = diffuseAlias
actualMat = MaterialsToExport[i][2]
bitmapFileName = undefined
oppactiyMapFileName = undefined
if actualMat != undefined and actualMat.diffuseMap != undefined and (classof actualMat.diffuseMap) == bitmaptexture then
(
bitmapFileName = actualMat.diffuseMap.filename
)
if actualMat != undefined and actualMat.OpacityMap != undefined and (classof actualMat.OpacityMap) == bitmaptexture then
(
baseMat = baseTransparentMaterial
diffAlias = transparentDiffuseAlias
)
if bitmapFileName!=undefined and exportTextures do
(
append bitmaps bitmapFileName
append rawbitmaps bitmapFileName
if actualMat.opacityMap == undefined then (
append rawAlphas true
)
else (
append rawAlphas false
)
)
if exportMats do
(
--format "Exporting mat %" mtlName
if funsizeExport then (
--format " as MTL\n"
ExportMTLMaterial ((dotnetclass "System.IO.Path").Combine outputFolder (mtlName+".mtl")) actualMat
)
else (
--format " as MT2\n"
ExportMaterial outputFolder mtlName bitmapFileName baseMaterial:baseMat otherTextures:MaterialsToExport[i][3] diffuseAlias:diffAlias
)
)
)
if bitmaps.count > 0 then (
makeUniqueArray bitmaps
if funsizeExport then (
alphas = #()
for bmp in bitmaps do (
append alphas rawAlphas[(findItem rawBitmaps bmp)]
)
ConvertIMG = dotnetclass "IMGToolLib.IMGTool"
ConvertIMG.ConvertToIMG bitmaps outputFolder null
)
else (
for bitmapFilename in bitmaps do (
alphaTex = alphaTextureName
--format "converting texture % using % for alpha\n" bitmapFilename alphaTex
bmp = openBitMap bitmapFilename
if bmp.hasAlpha and forceAlphaReplacement == false then (
alphaTex = ""
--format "removing alpha image"
)
ConvertTDX = dotnetClass "Gibbed.Stainless.TDXConvertLib.ConvertTDX"
ConvertTDX.Convert bitmapFileName outputFolder 5 alphaTex alphaTextureIndex
)
)
)
--gc()
)
fn MaterialAlreadySetToExport matName =
(
result = false
global MaterialsToExport
if(MaterialsToExport == undefined) then
(
MaterialsToExport = #()
)
for i=1 to MaterialstoExport.count do
(
if MaterialsToExport[i][1] == matName do result = true
)
result
)
fn TestStripper mO =
(
m = snapshotasmesh mO
numFaces = getNumFaces m
vertInds = #()
for i=1 to numFaces do
(
inds = getFace m i
format "%: [%,%,%]\n" i inds.x inds.y inds.z
append vertInds (inds.x as integer)
append vertInds (inds.y as integer)
append vertInds (inds.z as integer)
)
format "vertinds:\n"
for i=1 to vertinds.count by 3 do
(
format "[%, %, %]\n " vertInds[i] vertInds[i + 1] vertInds[i + 2]
)
strips = StripperShakeItBaby numFaces &vertInds
for i=1 to strips.count do
(
format "\nStrip %\n" i
format "Strip count %\n" strips[i].count
format "Strip contents\n"
for p=1 to strips[i].count do
(
format "%\n " strips[i][p]
)
)
if strips[1].count > strips[2].count then tmp = strips[2].count
else tmp = strips[1].count
for i=1 to tmp do
(
asdf = "false"
if strips[1][i] == strips[2][i] then asdf = "true"
format "% \t\t %\t%\n" strips[1][i] strips[2][i] asdf
)
nm = mesh name:"stripped mesh"
setnumverts nm (getnumverts m)
nm
)
fn ExportMDLMesh folder_name model forcedFilename:"" exportMats:false exportTextures:false funsizeExport:false createOctree:false =
(
OctreeVerts = #()
PREPVerts = #()
PREPFaces = #()
USERVerts = #()
USERFaces = #()
USER2PREPvertConv = #()
USER2PREPfaceConv = #()
PREP2USERvertConv = #()
PREP2USERfaceConv = #()
mtl = #()
mtlconv = #()
mid = #()
meshes = #()
oldTransform = copy model.transform
oldScale = model.scale
model.transform = matrix3 1
if model.modifiers["CNT Hierarchy"] != undefined and model.modifiers["CNT Hierarchy"].BakeScaleIntoMesh == true then model.scale = oldScale
model_name = model.name
if IsObjectACNT model then model_name = model.modifiers["CNT Hierarchy"].FileName
if forcedFilename!="" then
(
mdl_name=forcedFilename
outputFolder = GetFilenamePath mdl_name
)
else
(
mdl_name = (dotnetclass "System.IO.Path").Combine folder_name (model_name+".mdl")
outputFolder = folder_name
)
format "Exporting % to %\n\t passed in folder_name: %\n\n" mdl_name model.name folder_name
mdl_file = openfile mdl_name mode:"w"
close mdl_file
select model
max modify mode
editMeshMod = undefined
if classof model == editable_poly or classof model == PolyMeshObject then (
editMeshMod = Edit_Mesh()
addModifier model editMeshMod
)
normalMod = Edit_Normals()
addModifier model normalMod
--if meshop.getNumMaps model > 2 and meshop.getNumMapVerts m 2 > 0 then meshop.buildMapFaces model 2 keep:true
m = snapshotasmesh model
if (getNumTVerts m) < 1 then (
setNumTVerts m ((getNumFaces m) * 3)
buildTVFaces m
)
meshop.buildMapFaces m 1 keep:true
numFaces = getNumFaces m
tv_count = getNumTVerts m
vertNormals = #()
meshHasVertColours = false
meshHasTextureVerts = false
if (getNumCPVVerts m) > 0 then
(
meshHasVertColours = true
)
if tv_count != 0 then
(
meshHasTextureVerts = true
)
stripOffset = 0
patchOffset = 0
numAxisNegativeScale = 0
mirroredTransformScale = [-1,1,1]
mirroredNormalTransformScale = [-1,1,1]
normOldScale = normalize oldScale
if normOldScale.x < 0 do
(
numAxisNegativeScale = numAxisNegativeScale + 1
mirroredNormalTransformScale.x = 1
)
if normOldScale.y < 0 do
(
numAxisNegativeScale = numAxisNegativeScale + 1
mirroredNormalTransformScale.y = -1
)
if normOldScale.z < 0 do
(
numAxisNegativeScale = numAxisNegativeScale + 1
mirroredNormalTransformScale.z = -1
)
PREPFaceVerts = #()
for i=1 to (getNumVerts m) do
(
if (MaxVersion())[1] >= 9000 do (dotnetClass "Application").doEvents()
pos = (getVert m i) * mirroredTransformScale
append USERVerts (mdlUserVert x:pos.x y:pos.y z:pos.z TimesUsed: 1)
)
pos = undefined
--gc()
vertNormals =#()
faceIndicesForNormals =#()
newVertsList = #()
faceNewVertInds = #()
faceIndicesForTVs =#()
facesForMatID = #()