-
Notifications
You must be signed in to change notification settings - Fork 5
/
XPlaneImportPlane.py
4162 lines (3866 loc) · 144 KB
/
XPlaneImportPlane.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
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
#!BPY
""" Registration info for Blender menus:
Name: 'X-Plane Plane or Weapon (.acf, .wpn)...'
Blender: 243
Group: 'Import'
Tooltip: 'Import an X-Plane airplane (.acf) or weapon (.wpn)'
"""
__author__ = "Jonathan Harris"
__email__ = "Jonathan Harris, Jonathan Harris <x-plane:marginal*org*uk>"
__url__ = "XPlane2Blender, http://marginal.org.uk/x-planescenery/"
__version__ = "3.11"
__bpydoc__ = """\
This script imports X-Plane v7-v10 airplanes and weapons into Blender,
so that they can be exported as X-Plane scenery objects.
"""
#
# Copyright (c) 2004-2013 Jonathan Harris
#
# This code is licensed under version 2 of the GNU General Public License.
# http://www.gnu.org/licenses/gpl-2.0.html
#
# See ReadMe-XPlane2Blender.html for usage.
#
import sys
import Blender
from Blender import Object, NMesh, Lamp, Image, Material, Window, Mathutils
from Blender.Mathutils import Vector, Matrix, RotationMatrix, ScaleMatrix, TranslationMatrix, Quaternion
from struct import unpack
from math import hypot, pi, sin, cos, atan, radians
from os import listdir
from os.path import basename, dirname, join, splitext
from XPlaneUtils import Vertex, UV, findTex
from XPlaneImport_util import OBJimport
class ParseError(Exception):
def __init__(self, msg):
self.msg=msg
#------------------------------------------------------------------------
#-- ACFimport --
#------------------------------------------------------------------------
class ACFimport:
LAYER1=1
LAYER2=2
LAYER3=4
LAYERS=[LAYER1,LAYER2,LAYER3]
LAYER1MKR=' H'
LAYER2MKR=' M'
LAYER3MKR=' L'
MARKERS=[LAYER1MKR,LAYER2MKR,LAYER3MKR]
IMAGE2MKR='*' # Marker for parts with non-primary texture
# bodies smaller than this [m] skipped
THRESH1=1.0
THRESH2=2.5
THRESH3=7.0
#------------------------------------------------------------------------
def __init__(self, filename, isscenery, relocate=False):
self.debug=0 # 1: extra debug info in console. 2: also dump txt file
self.isscenery=isscenery
if self.isscenery:
self.scale=0.3048 # foot->metre constant
else:
self.scale=1.0
if Blender.sys.dirsep=='\\':
# Lowercase Windows drive lettter
self.filename=filename[0].lower()+filename[1:]
else:
self.filename=filename
self.acf=ACF(self.filename, self.debug)
self.scene = Blender.Scene.GetCurrent()
self.navloc = Vertex(0.0, 0.0, 0.0)
self.tailloc = Vertex(0.0, 0.0, 0.0)
self.wingc = {}
self.image=0
self.image2=0
self.mm=Matrix([ self.scale, 0.0, 0.0, 0.0],
[0.0, -self.scale, 0.0, 0.0],
[0.0, 0.0, -self.scale, 0.0],
[0.0, 0.0, 0.0, 1.0])
self.meshcache={}
cur=Window.GetCursorPos()
self.offset=Vertex(cur[0], cur[1], cur[2])
if self.acf.HEADER_version in [1,800]:
# Importing weapon
if relocate:
self.offset+=Vertex(0, -self.acf.cgY, -self.acf.cgZ, self.mm)
return # Texture handled elsewhere
elif self.acf.HEADER_version>=1000:
if relocate:
self.offset+=Vertex(0, -self.acf.cgY, -self.acf.cgZ, self.mm)
else:
if relocate:
self.offset+=Vertex(0, -self.acf.WB_cgY, -self.acf.WB_cgZ, self.mm)
texfilename=self.filename[:self.filename.rindex('.')]+'_paint'
for extension in ['.dds', '.DDS', '.png', '.PNG', '.bmp', '.BMP']:
try:
file = open(texfilename+extension, "rb")
except IOError:
pass
else:
for extension2 in ['.dds', '.DDS', '.png', '.PNG', '.bmp', '.BMP']:
try:
self.image2 = Image.Load(texfilename+'2'+extension2)
except IOError:
pass
# Handle spaces in primary texture filename
if 0: #Blender.sys.basename(texfilename).find(" ") != -1:
basefilename=Blender.sys.basename(texfilename)
newfilename=""
for i in range(len(basefilename)):
if basefilename[i]==" ":
newfilename+="_"
else:
newfilename+=basefilename[i]
print "Info:\tCreated new texture file \"%s\"" % (
newfilename+extension)
newfilename=(Blender.sys.dirname(texfilename)+
Blender.sys.dirsep+newfilename)
newfile=open(newfilename+extension, "wb")
newfile.write(file.read())
newfile.close()
texfilename=newfilename
file.close()
self.image = Image.Load(texfilename+extension)
return
print "Warn:\tNo texture file found"
#------------------------------------------------------------------------
def doImport(self):
layers=self.scene.layers
self.scene.layers=[1,2,3] # otherwise object centres not updated
# Hack! Just importing weapon
if self.acf.HEADER_version in [1,800]:
Window.DrawProgressBar(0.5, "Importing weapon ...")
self.doBody(basename(self.filename), 0, False)
self.scene.layers=layers
return
if self.isscenery:
if self.acf.HEADER_version>=1000:
n=len(self.acf.part)+len(self.acf.wpna)+len(self.acf.gear)+len(self.acf.door)+len(self.acf.obja)+len(self.acf.lite)+1
else:
n=DEFfmt.partDIM+DEFfmt.wattDIM+DEFfmt.gearDIM+DEFfmt.doorDIM+DEFfmt.objsDIM+len(DEFfmt.lites)+1
else:
n=DEFfmt.partDIM
i=0
for (name, p) in DEFfmt.parts:
i=i+1
Window.DrawProgressBar(0.25+0.75*i/n, "Importing bodies ...")
self.doBody(name, p, False)
if not self.isscenery:
self.scene.layers=layers
return
for p in range(DEFfmt.engnDIM):
i=i+1
Window.DrawProgressBar(0.25+0.75*i/n, "Importing props ...")
self.doProp(p)
# Need to do wings in two passes
for (name, p) in DEFfmt.wings:
self.doWing1(name, p)
for (name, p) in DEFfmt.wings:
i=i+1
Window.DrawProgressBar(0.25+0.75*i/n, "Importing wings ...")
self.doWing2(name, p)
for p in range(DEFfmt.gearDIM):
i=i+1
Window.DrawProgressBar(0.25+0.75*i/n, "Importing gear ...")
self.doGear(p)
for p in range(DEFfmt.wattDIM):
i=i+1
Window.DrawProgressBar(0.25+0.75*i/n, "Importing weapons ...")
self.doBody(None, p, True)
for p in range(DEFfmt.doorDIM): # Skip speedbrakes
i=i+1
Window.DrawProgressBar(0.25+0.75*i/n, "Importing doors ...")
self.doDoor(p)
if self.acf.HEADER_version>=1000:
for p in range(len(self.acf.obja)):
i=i+1
Window.DrawProgressBar(0.25+0.75*i/n, "Importing OBJs ...")
self.doObjs(p)
elif hasattr(self.acf,'objs'): # New in 8.40
for p in range(DEFfmt.objsDIM):
i=i+1
Window.DrawProgressBar(0.25+0.75*i/n, "Importing OBJs ...")
self.doObjs(p)
if self.acf.HEADER_version>=1000:
for p in range(len(self.acf.lite)):
i=i+1
Window.DrawProgressBar(0.25+0.75*i/n, "Importing lights ...")
self.doLight10(p)
else:
for p in range(len(DEFfmt.lites)):
i=i+1
Window.DrawProgressBar(0.25+0.75*i/n, "Importing lights ...")
self.doLight(p)
if self.acf.VIEW_has_navlites:
# uses values computed during wings
self.addLamp("airplane_nav_left", 1.0, 0.0, 0.0, # was Nav Left
self.offset+Vertex(-(self.navloc.x+0.05), self.navloc.y, self.navloc.z))
self.addLamp("airplane_strobe", 1.0, 1.0, 1.0, # was Strobe Left
self.offset+Vertex(-(self.navloc.x+0.05), self.navloc.y-0.1, self.navloc.z))
self.addLamp("airplane_nav_right", 0.0, 1.0, 0.0, # was Nav Right
self.offset+Vertex(self.navloc.x + 0.05, self.navloc.y, self.navloc.z))
self.addLamp("airplane_strobe", 1.0, 1.0, 1.0, # wasStrobe Right
self.offset+Vertex(self.navloc.x + 0.05, self.navloc.y-0.1, self.navloc.z))
if self.acf.HEADER_version<800: # v7
self.addLamp("airplane_beacon", 1.0, 0.0, 0.0,# was Tail pulse or Nav Pulse
self.offset+Vertex(self.tailloc.x, self.tailloc.y, self.tailloc.z + 0.05))
self.scene.layers=layers
#------------------------------------------------------------------------
def doProp(self, p):
# Arbitrary constant
twist=pi*(30.0/180.0)
engn=self.acf.engn[p]
prop=self.acf.engn[p] # Was here pre v10
part=self.acf.part[p]
wing=self.acf.wing[p]
if self.acf.HEADER_version>=1000:
if not getattr(self.acf.prop[p], 'num_blades', 0) or getattr(part, 'part_specs_invis', 0):
return
prop=self.acf.prop[p]
elif (p>=self.acf.ENGINE_num_thrustpoints or
engn.engn_type not in [0,1,2,8] or
not engn.num_blades or
not wing.semilen_SEG):
return
# texture
if part.part_tex==0:
imagemkr=ACFimport.LAYER1MKR
image=self.image
else:
imagemkr=ACFimport.LAYER1MKR+ACFimport.IMAGE2MKR
image=self.image2
mesh=NMesh.New("Prop %s%s" % ((p+1), imagemkr))
if self.acf.HEADER_version>=1000:
mm=TranslationMatrix((Vertex(part.part_x,
part.part_y+self.acf.vectarmY,
part.part_z+self.acf.vectarmZ,
self.mm)+self.offset).toVector(4))
mm=RotationMatrix(part.part_the, 4, 'x')*mm
mm=RotationMatrix(-part.part_psi, 4, 'z')*mm
if self.acf.vect_EQ and wing.inc_vect[0]: # bizarre
mm=RotationMatrix(self.acf.vect_min_disc, 4, 'x')*mm
else:
mm=TranslationMatrix((Vertex(part.part_x,
part.part_y+self.acf.VTOL_vectarmY,
part.part_z+self.acf.VTOL_vectarmZ,
self.mm)+self.offset).toVector(4))
mm=RotationMatrix(engn.vert_init, 4, 'x')*mm
mm=RotationMatrix(-engn.side_init, 4, 'z')*mm
if self.acf.VTOL_vect_EQ and wing.inc_vect[0]: # bizarre
mm=RotationMatrix(self.acf.VTOL_vect_min_disc, 4, 'x')*mm
v=[Vertex(0,
sin(twist)*
wing.Croot*self.scale/4,
-cos(twist)*prop.prop_dir*
wing.Croot*self.scale/4),
Vertex(0,
-sin(twist)*
wing.Croot*self.scale*3/4,
cos(twist)*prop.prop_dir*
wing.Croot*self.scale*3/4),
Vertex(wing.semilen_SEG*self.scale,
0,
prop.prop_dir*
wing.Ctip*self.scale*3/4),
Vertex(wing.semilen_SEG*self.scale,
0,
-prop.prop_dir*
wing.Ctip*self.scale/4)]
ruv=[UV(part.top_s1,part.top_t1),
UV(part.top_s2,part.top_t1),
UV(part.top_s2,part.top_t2),
UV(part.top_s1,part.top_t2)]
luv=[UV(part.bot_s1,part.bot_t2),
UV(part.bot_s2,part.bot_t2),
UV(part.bot_s2,part.bot_t1),
UV(part.bot_s1,part.bot_t1)]
for i in range(int(prop.num_blades)):
a=(1+i*2)*pi/prop.num_blades
fv=[]
for v1 in v:
fv.append(Vertex(cos(a)*v1.x - sin(a)*v1.z,
v1.y,
sin(a)*v1.x + cos(a)*v1.z))
self.addFace(mesh, fv, ruv, image)
self.addFace(mesh,
[fv[3], fv[2], fv[1], fv[0]],
luv, image)
self.addMesh(mesh.name, mesh, ACFimport.LAYER1, mm)
#------------------------------------------------------------------------
def doWing1(self, name, p):
part=self.acf.part[p]
wing=self.acf.wing[p]
if not (getattr(part, 'part_eq', 0) or getattr(part, 'part_specs_eq', 0)) or not wing.semilen_SEG:
return
centre=Vertex(part.part_x, part.part_y, part.part_z, self.mm)
if self.acf.HEADER_version>=1000:
tip=centre+Vertex(RotationMatrix(wing.is_right_mult*wing.dihed_design, 3, 'y') *
(RotationMatrix(wing.is_right_mult*wing.sweep_design, 3, 'z') *
Vector([wing.is_right_mult*wing.semilen_SEG*self.scale,0,0])))
else:
tip=centre+Vertex(RotationMatrix(wing.lat_sign*wing.dihed1, 3, 'y') *
(RotationMatrix(wing.lat_sign*wing.sweep1, 3, 'z') *
Vector([wing.lat_sign*wing.semilen_SEG*self.scale,0,0])))
# Maybe nav light location - at least in 8.40 only main wings count
if p in DEFfmt.partMainWings:
if tip.x>self.navloc.x:
self.navloc=tip
if tip.z>self.tailloc.z:
self.tailloc=tip
self.wingc[p]=((centre, tip))
if self.debug:
print "%s \t[%s] [%s]" % (name, centre, tip)
#------------------------------------------------------------------------
def doWing2(self, name, p):
part=self.acf.part[p]
wing=self.acf.wing[p]
if not (getattr(part, 'part_eq', 0) or getattr(part, 'part_specs_eq', 0)) or getattr(part, 'part_specs_invis', 0) or not wing.semilen_SEG:
return
if self.acf.HEADER_version>=1000:
dihed=wing.dihed_design
sweep=wing.sweep_design
sign=wing.is_right_mult
else:
dihed=wing.dihed1
sweep=wing.sweep1
sign=wing.lat_sign
# Arbitrary constants for symmetrical and lifting wings
sym_width=0.09
lift_width=0.10
chord1=0.125
chord2=0.450
max_dihed=20.0 # Wings with d greater than this treated as Sym
tip_fudge=0.2 # wings considered joined if closer [ft]
if self.debug:
print "%s \t" % name,
# Is this a wing tip?
istip=True
(centre, tip) = self.wingc[p]
for p2, (c2, t2) in self.wingc.iteritems():
if (p2 != p and
tip.equals(c2, tip_fudge) and
abs(wing.Ctip-self.acf.wing[p2].Croot) < tip_fudge and
abs(dihed-(getattr(self.acf.wing[p2],'dihed1',0) or getattr(self.acf.wing[p2],'dihed_design',0))) < max_dihed):
istip=False
child=p2
break
if self.debug:
if istip:
print "Tip",
else:
print "child=%s" % child,
# Find parent in segment and root of segment
rootp=p # part number of root
c=centre # centre of root
considered=[rootp]
if not istip: considered.append(child)
while 1:
for p2, (c2, t2) in self.wingc.iteritems():
if (p2 not in considered and
c.equals(t2, tip_fudge) and
abs(self.acf.wing[p2].Ctip-self.acf.wing[rootp].Croot) < tip_fudge and
abs((getattr(self.acf.wing[p2],'dihed1',0) or getattr(self.acf.wing[p2],'dihed_design',0)) -
(getattr(self.acf.wing[rootp],'dihed1',0) or getattr(self.acf.wing[rootp],'dihed_design',0))) < max_dihed):
rootp=p2
c=c2
considered.append(rootp)
break
else:
break
if self.debug:
if p==rootp:
print "Root"
else:
print "Root=%s" % rootp
# texture
if part.part_tex==0:
imagemkr=''
image=self.image
else:
imagemkr=ACFimport.IMAGE2MKR
image=self.image2
mm=TranslationMatrix((self.offset+centre).toVector(4))
mm=RotationMatrix(-sign*dihed, 4, 'y')*mm
# Re-use existing meshes
crs=[DEFfmt.partMainWings,DEFfmt.partMiscWings,DEFfmt.partPylons]
for cr in crs:
if not p in cr: continue
for p2 in cr:
part2=self.acf.part[p2]
wing2=self.acf.wing[p2]
if (p2>=p or
not (getattr(part2, 'part_eq', 0) or getattr(part2, 'part_specs_eq', 0)) or
part.part_tex!=part2.part_tex or
part.top_s1!=part2.top_s1 or
part.top_s2!=part2.top_s2 or
part.top_t1!=part2.top_t1 or
part.top_t2!=part2.top_t2 or
wing.semilen_SEG!=wing2.semilen_SEG or
wing.Ctip!=wing2.Ctip or
wing.Croot!=wing2.Croot or
getattr(wing,'dihed1',0)!=getattr(wing2,'dihed1',0) or
getattr(wing,'dihed_design',0)!=getattr(wing2,'dihed_design',0) or
getattr(wing,'sweep1',0)!=getattr(wing2,'sweep1',0) or
getattr(wing,'sweep_design',0)!=getattr(wing2,'sweep_design',0) or
getattr(wing,'Rafl0',None)!=getattr(wing2,'Rafl0',None) or
getattr(wing,'afl_file_R0',None)!=getattr(wing2,'afl_file_R0',None) or
getattr(wing,'Tafl0',None)!=getattr(wing2,'Tafl0',None) or
getattr(wing,'afl_file_T0',None)!=getattr(wing2,'afl_file_T0',None)):
continue
meshes=self.meshcache[p2]
if sign*(getattr(self.acf.wing[p2],'lat_sign',0) or getattr(self.acf.wing[p2],'is_right_mult',0)) < 0:
mm=ScaleMatrix(-1, 4, Vector(1, 0, 0))*mm
for i in range(len(meshes)):
if i==2: # layer 3
if not istip: continue
if p!=rootp:
(root, foo) = self.wingc[rootp]
mm=TranslationMatrix((self.offset + root).toVector(4))
if sign*(getattr(self.acf.wing[p2],'lat_sign',0) or getattr(self.acf.wing[p2],'is_right_mult',0)) < 0:
mm=ScaleMatrix(-1, 4, Vector(1, 0, 0))*mm
ob=self.addMesh(name+ACFimport.MARKERS[i]+imagemkr,
meshes[i], ACFimport.LAYERS[i], mm)
return
# No matching wing
self.meshcache[p]=[]
# Find four points - leading root & tip, trailing tip & root
rootinc=RotationMatrix(wing.incidence[0]*sign, 3, 'x')
if istip:
# Don't want to rotate to find wing sweep. So find tip manually.
tip=Vertex(RotationMatrix(sign*sweep, 3, 'z') *
Vector([sign*wing.semilen_SEG*self.scale,0,0]))
tiplen=wing.Ctip
tipinc=RotationMatrix(wing.incidence[int(wing.els)-1]*sign, 3, 'x')
v=[Vertex(rootinc*Vector([0.0, wing.Croot*self.scale/4, 0.0])),
Vertex( tipinc*Vector([0.0, wing.Ctip *self.scale/4, 0.0]))+tip,
Vertex( tipinc*Vector([0.0, -wing.Ctip *self.scale*3/4, 0.0]))+tip,
Vertex(rootinc*Vector([0.0, -wing.Croot*self.scale*3/4, 0.0]))]
else:
# Get tip from child's root so segments line-up exactly
# Todo: Make mid-chord vertices line-up also
(tip,t2)=self.wingc[child]
tip=(tip-centre).toVector(4) * RotationMatrix(sign*dihed, 4, 'y')
tiplen=self.acf.wing[child].Croot
tipinc=RotationMatrix(self.acf.wing[child].incidence[0]*(getattr(self.acf.wing[child],'lat_sign',0) or getattr(self.acf.wing[child],'is_right_mult',0)), 3, 'x')
v=[Vertex(rootinc*Vector([0.0, wing.Croot*self.scale/4, 0.0])),
Vertex( tipinc*Vector([0.0, self.acf.wing[child].Croot*self.scale/4, 0.0]))+tip,
Vertex( tipinc*Vector([0.0, -self.acf.wing[child].Croot*self.scale*3/4, 0.0]))+tip,
Vertex(rootinc*Vector([0.0, -wing.Croot*self.scale*3/4, 0.0]))]
rv=v
lv=[v[3], v[2], v[1], v[0]]
if self.debug:
for q in v:
print "[%5.1f %5.1f %5.1f]" % (q.x, q.y, q.z),
# Corresponding texture points
miny=max(v[0].y,v[1].y) # leading edge
maxy=min(v[2].y,v[3].y) # trailing edge
if getattr(wing, 'is_left', 0) or getattr(wing, 'is_right_mult', 0)<0:
rys=(part.top_s2-part.top_s1)/(miny-maxy)
ruv=[UV(part.top_s1+(miny-v[0].y)*rys, part.top_t1),
UV(part.top_s1+(miny-v[1].y)*rys, part.top_t2),
UV(part.top_s1+(miny-v[2].y)*rys, part.top_t2),
UV(part.top_s1+(miny-v[3].y)*rys, part.top_t1)]
lys=(part.bot_s2-part.bot_s1)/(miny-maxy)
luv=[UV(part.bot_s1+(miny-v[3].y)*lys, part.bot_t1),
UV(part.bot_s1+(miny-v[2].y)*lys, part.bot_t2),
UV(part.bot_s1+(miny-v[1].y)*lys, part.bot_t2),
UV(part.bot_s1+(miny-v[0].y)*lys, part.bot_t1)]
else:
rys=(part.bot_s2-part.bot_s1)/(miny-maxy)
ruv=[UV(part.bot_s1+(miny-v[0].y)*rys, part.bot_t1),
UV(part.bot_s1+(miny-v[1].y)*rys, part.bot_t2),
UV(part.bot_s1+(miny-v[2].y)*rys, part.bot_t2),
UV(part.bot_s1+(miny-v[3].y)*rys, part.bot_t1)]
lys=(part.top_s2-part.top_s1)/(miny-maxy)
luv=[UV(part.top_s1+(miny-v[3].y)*lys, part.top_t1),
UV(part.top_s1+(miny-v[2].y)*lys, part.top_t2),
UV(part.top_s1+(miny-v[1].y)*lys, part.top_t2),
UV(part.top_s1+(miny-v[0].y)*lys, part.top_t1)]
# Type of wing to draw
if (wing.semilen_SEG*self.scale < ACFimport.THRESH1 and
wing.Croot*self.scale < ACFimport.THRESH1/2 and
istip and p==rootp):
# Small and not part of a segment - draw as thin
iscrappy=True
else:
iscrappy=False
# Orientation
if abs(dihed) >= max_dihed:
orient=0 # Verticalish
rwidth=sym_width/2
twidth=sym_width/2
else:
if ((dihed+90)*sign < 0):
orient=-1 # Left side
else:
orient=1 # Right side
rwidth=lift_width/2
twidth=lift_width/2
w=self.afl(getattr(wing,'Rafl0',None) or getattr(wing,'afl_file_R0',None))
if w:
rwidth=w/2
twidth=w/2
w=self.afl(getattr(wing,'Tafl0',None) or getattr(wing,'afl_file_T0',None))
if w:
twidth=w/2
rwidth=sign*rwidth
twidth=sign*twidth
# Layer 1
mesh=NMesh.New(name+ACFimport.LAYER1MKR+imagemkr)
if iscrappy:
# Not worth toggling culling just for this, so repeat the face
self.addFace(mesh, rv, ruv, image, False)
self.addFace(mesh, lv, luv, image, False)
else:
self.addFacePart(mesh, rv, ruv, 0, chord1, rwidth, twidth,
image)
self.addFacePart(mesh, rv, ruv, chord1, chord2, rwidth, twidth,
image)
self.addFacePart(mesh, rv, ruv, chord2, 1, rwidth, twidth,
image)
self.addFacePart(mesh, lv, luv, 0, 1-chord2, rwidth, twidth,
image, False) # sharp edge required
self.addFacePart(mesh, lv, luv, 1-chord2, 1-chord1, rwidth, twidth,
image)
self.addFacePart(mesh, lv, luv, 1-chord1, 1, rwidth, twidth,
image)
if istip:
# Add end cap
ctip=rv[2].y-rv[1].y
ntip=ctip*twidth
self.addFace(mesh,
[rv[1],
rv[1]+Vertex(0, ctip*chord1, -ntip),
rv[1]+Vertex(0, ctip*chord1, ntip)],
[ruv[1],
UV(ruv[1].s+(ruv[2].s-ruv[1].s)*chord1,ruv[1].t),
UV(ruv[1].s+(ruv[2].s-ruv[1].s)*chord1,ruv[1].t)],
image)
self.addFace(mesh,
[rv[1]+Vertex(0, ctip*chord1, ntip),
rv[1]+Vertex(0, ctip*chord1, -ntip),
rv[1]+Vertex(0, ctip*chord2, -ntip),
rv[1]+Vertex(0, ctip*chord2, ntip)],
[UV(ruv[1].s+(ruv[2].s-ruv[1].s)*chord1,ruv[1].t),
UV(ruv[1].s+(ruv[2].s-ruv[1].s)*chord1,ruv[1].t),
UV(ruv[1].s+(ruv[2].s-ruv[1].s)*chord2,ruv[1].t),
UV(ruv[1].s+(ruv[2].s-ruv[1].s)*chord2,ruv[1].t)],
image)
self.addFace(mesh,
[rv[2],
rv[1]+Vertex(0, ctip*chord2, ntip),
rv[1]+Vertex(0, ctip*chord2, -ntip)],
[ruv[2],
UV(ruv[1].s+(ruv[2].s-ruv[1].s)*chord2,ruv[1].t),
UV(ruv[1].s+(ruv[2].s-ruv[1].s)*chord2,ruv[1].t)],
image)
self.meshcache[p].append(mesh)
self.addMesh(mesh.name, mesh, ACFimport.LAYER1, mm)
# Layer 2
if iscrappy:
return
mesh=NMesh.New(name+ACFimport.LAYER2MKR+imagemkr)
self.addFace(mesh, rv, ruv, image, False)
self.addFace(mesh, lv, luv, image, False)
self.meshcache[p].append(mesh)
self.addMesh(mesh.name, mesh, ACFimport.LAYER2, mm)
# Layer 3
if not istip:
return # Only do wing tips
if p==rootp:
if wing.semilen_SEG*self.scale < ACFimport.THRESH3:
return
else:
(foo, tip) = self.wingc[p]
(root, foo) = self.wingc[rootp]
tip=tip-root # tip relative to root
if (tip.x*tip.x + tip.y*tip.y + tip.z*tip.z <
ACFimport.THRESH3 * ACFimport.THRESH3):
return
rootwing=self.acf.wing[rootp]
rootinc=RotationMatrix(rootwing.incidence[0]*(getattr(rootwing,'lat_sign',0) or getattr(rootwing,'is_right_mult',0)), 3, 'x')
tipinc=RotationMatrix(wing.incidence[int(wing.els)-1]*sign, 3, 'x')
rv=[Vertex(rootinc*Vector([0.0, rootwing.Croot*self.scale/4, 0.0])),
Vertex( tipinc*Vector([0.0, wing.Ctip *self.scale/4, 0.0]))+tip,
Vertex( tipinc*Vector([0.0, -wing.Ctip *self.scale*3/4, 0.0]))+tip,
Vertex(rootinc*Vector([0.0, -rootwing.Croot*self.scale*3/4, 0.0]))]
lv=[rv[3], rv[2], rv[1], rv[0]]
mm=TranslationMatrix((self.offset + root).toVector(4))
mesh=NMesh.New(name+ACFimport.LAYER3MKR+imagemkr)
self.addFace(mesh, rv, ruv, image, False)
self.addFace(mesh, lv, luv, image, False)
self.meshcache[p].append(mesh)
self.addMesh(mesh.name, mesh, ACFimport.LAYER3, mm)
#------------------------------------------------------------------------
def doBody(self, name, p, is_wpn):
if is_wpn:
# Weapon locations are special
if name: # Importing stand-alone weapon
watt=None
wpnname=name
(obname,foo)=splitext(wpnname)
meshname=obname
elif self.acf.HEADER_version>=1000: # Get weapon details from weapon attach structure
watt=self.acf.wpna[p]
wpnname=getattr(watt, 'v10_att_file_stl', None)
if not wpnname: return
(meshname,foo)=splitext(wpnname)
obname="W%02d %s" % (p+1, meshname)
else: # Get weapon details from weapon attach structure
watt=self.acf.watt[p]
wpnname=watt.watt_name
if not wpnname: return
(meshname,foo)=splitext(wpnname)
obname="W%02d %s" % (p+1, meshname)
wpn=self.wpn(wpnname)
if not wpn: return
part=wpn.part
(texname,foo)=splitext(wpnname)
image=findTex(self.filename, texname, ['Weapons'])
imagemkr=''
if watt:
if self.acf.HEADER_version>=1000:
mm=TranslationMatrix((Vertex(watt.v10_att_x_acf_prt_ref, watt.v10_att_y_acf_prt_ref, watt.v10_att_z_acf_prt_ref, self.mm)+self.offset).toVector(4))
mm=self.rotate(int(watt.v10_att_part), False, watt.v10_att_psi_ref, watt.v10_att_the_ref, watt.v10_att_phi_ref)*mm
else:
mm=TranslationMatrix((Vertex(watt.watt_x, watt.watt_y, watt.watt_z, self.mm)+self.offset).toVector(4))
mm=self.rotate(watt.watt_con, False, watt.watt_psi, watt.watt_the, watt.watt_phi)*mm
else:
mm=TranslationMatrix(self.offset.toVector(4))
mm=TranslationMatrix(Vertex(-part.part_x, -part.part_y, -part.part_z, self.mm).toVector(4))*mm
# Re-use existing meshes
if wpnname in self.meshcache:
meshes=self.meshcache[wpnname]
for i in range(len(meshes)):
self.addMesh(obname+ACFimport.MARKERS[i],
meshes[i], ACFimport.LAYERS[i], mm)
return
else:
self.meshcache[wpnname]=[]
else:
# Normal bodies
obname=meshname=name
part=self.acf.part[p]
if not (getattr(part, 'part_eq', 0) or getattr(part, 'part_specs_eq', 0)) or getattr(part, 'part_specs_invis', 0):
return
if part.part_tex==0:
imagemkr=''
image=self.image
else:
imagemkr=ACFimport.IMAGE2MKR
image=self.image2
if self.acf.HEADER_version<1000 and p in DEFfmt.partFairings:
# Fairings take location but not rotation from wheels
part.patt_con=0 # appears to be random in acf
gear=self.acf.gear[p-DEFfmt.partFair1]
a=RotationMatrix(gear.latE, 3, 'y')
a=RotationMatrix(-gear.lonE, 3, 'x')*a
mm=TranslationMatrix((Vertex(gear.gear_x,
gear.gear_y,
gear.gear_z,
self.mm)+
Vertex(a * Vector([0,0,-gear.leg_len*self.scale]))+
self.offset).toVector(4))
else:
mm=TranslationMatrix((Vertex(part.part_x,
part.part_y,
part.part_z,
self.mm)+self.offset).toVector(4))
mm=self.rotate(int(part.patt_con), True,
part.part_psi, part.part_the, part.part_phi)*mm
if p in DEFfmt.partNacelles:
# Nacelles also affected by engine cant and vector.
wing=self.acf.wing[p-DEFfmt.partNace1]
if self.acf.HEADER_version>=1000:
prop=self.acf.part[p-DEFfmt.partNace1] # re-uses prop part
mm=RotationMatrix(prop.part_the, 4, 'x')*mm
mm=RotationMatrix(-prop.part_psi, 4, 'z')*mm
if self.acf.vect_EQ and wing.inc_vect[0]: # bizarre
mm=RotationMatrix(self.acf.vect_min_disc, 4, 'x')*mm
else:
engn=self.acf.engn[p-DEFfmt.partNace1]
mm=RotationMatrix(engn.vert_init, 4, 'x')*mm
mm=RotationMatrix(-engn.side_init, 4, 'z')*mm
if self.acf.VTOL_vect_EQ and wing.inc_vect[0]: # bizarre
mm=RotationMatrix(self.acf.VTOL_vect_min_disc, 4, 'x')*mm
# Re-use existing meshes
crs=[DEFfmt.partMisc,DEFfmt.partNacelles,DEFfmt.partFairings]
for cr in crs:
if not p in cr: continue
for p2 in cr:
part2=self.acf.part[p2]
if (p2>=p or
not (getattr(part2, 'part_eq', 0) or getattr(part2, 'part_specs_eq', 0)) or
part.s_dim!=part2.s_dim or
part.r_dim!=part2.r_dim or
part.part_tex!=part2.part_tex or
part.top_s1!=part2.top_s1 or
part.top_s2!=part2.top_s2 or
part.top_t1!=part2.top_t1 or
part.top_t2!=part2.top_t2): continue
for i in range(int(part.s_dim)):
# Assume symmetrical
for j in range(int((1+part.r_dim)/2)):
if part.geo_xyz[i][j]!=part2.geo_xyz[i][j]:
break
else:
continue
break
else:
# Matching part
meshes=self.meshcache[p2]
for i in range(len(meshes)):
self.addMesh(obname+ACFimport.MARKERS[i]+imagemkr,
meshes[i], ACFimport.LAYERS[i], mm)
return
# No matching part
self.meshcache[p]=[]
if self.debug: print obname
# Get vertex data in 2D array
v=[]
# locate the data in the array and skip duplicates
rdim=int(part.r_dim/2)*2-2 # Must be even
seq=range(rdim/2)
seq.extend(range((rdim+2)/2,rdim+1))
for i in range(int(part.s_dim)):
if (i==12 and
Vertex(part.geo_xyz[i][0]).equals(Vertex(0.0, 0.0, 0.0))):
# Special case: Plane-Maker<7.30 leaves these parts as 0
if self.debug: print "Stopping at 12"
break
# Special case: Plane-Maker>=7.30 replicates part 11 offset 0.001'
if i==12: # was >11
for j in seq:
if not Vertex(part.geo_xyz[i][j]).equals(
Vertex(part.geo_xyz[i-1][j]), 0.001):
break
else:
if self.debug: print "Stopping at %s" % i
break
if self.debug: print i
w=[]
for j in seq:
q=Vertex(part.geo_xyz[i][j], self.mm)
w.append(q)
if self.debug: print "[%5.1f %5.1f %5.1f]" % (q.x, q.y, q.z),
if self.debug: print
v.append(w)
sdim=len(v) # We now have up to 20 segments (maybe 12 or 8 or less)
rdim=len(v[0]) # with 16 or fewer (but even) vertices/segment
# Seriously fucked up algorithm for determining textures for
# half (rdim/2+1) the body from load_plane_geo(), hl_drplane.cpp.
rsem=rdim/2+1
y_ctr=0.0
for r in range(rdim):
# Hack: Only use the first 12 stations for the fuse centreline
# to keep the same centreline loc before and after 730, where
# the number of fuselage stations changed from 12 to 20.
for s in range (min(sdim,12)):
y_ctr+=v[s][r].z/(rdim*sdim)
uv=[]
for s in range(sdim):
uv.append([])
for R in range(rsem):
# R is the point we are finding the s/t coordinate for
uv[s].append(UV(0,0))
point_above_ctr=(v[s][R].z>y_ctr)
point_below_ctr=not point_above_ctr
if point_above_ctr:
r1=R
r2=rsem
else:
r1=0
r2=R
for r in range(r1,r2): # remember we go to r+1!
# r is simply a counter to build up the coordinate for R
tlen=hypot(v[s][r+1].x-v[s][r].x, v[s][r+1].z-v[s][r].z)
if (point_above_ctr and v[s][r].z>y_ctr and
v[s][r+1].z>y_ctr):
uv[s][R].t+=tlen
if (point_below_ctr and v[s][r].z<y_ctr and
v[s][r+1].z<y_ctr):
uv[s][R].t-=tlen
if (point_above_ctr and v[s][r].z!=v[s][r+1].z and
v[s][r].z>=y_ctr and v[s][r+1].z<=y_ctr):
uv[s][R].t+=tlen*(v[s][r ].z-y_ctr)/(v[s][r].z-v[s][r+1].z)
if (point_below_ctr and v[s][r].z!=v[s][r+1].z and
v[s][r].z>=y_ctr and v[s][r+1].z<=y_ctr):
uv[s][R].t-=tlen*(y_ctr-v[s][r+1].z)/(v[s][r].z-v[s][r+1].z)
if v[s][rsem].z>=y_ctr:
uv[s][R].t+=(v[s][rsem].z-y_ctr)
if v[s][0 ].z<=y_ctr:
uv[s][R].t+=(v[s][0 ].z-y_ctr)
lo_y= 99999.0
lo_z= 99999.0
hi_y=-99999.0
hi_z=-99999.0
# find extreme points for scale
for s in range(sdim):
for r in range(rsem):
if uv[s][r].t>hi_y:
hi_y=uv[s][r].t
if uv[s][r].t<lo_y:
lo_y=uv[s][r].t
if v[s][r].y>hi_z:
hi_z=v[s][r].y
if v[s][r].y<lo_z:
lo_z=v[s][r].y
# scale all data 0-1
for s in range(sdim):
for r in range(rsem):
uv[s][r].t=(uv[s][r].t-lo_y)/(hi_y-lo_y)
if (is_wpn or
((p in DEFfmt.partNacelles) and
(getattr(self.acf.engn[p-DEFfmt.partNace1], 'engn_type', 0) in [4,5] or
getattr(self.acf.engn[p-DEFfmt.partNace1], 'type', '').startswith('JET')))):
# do LINE-LENGTH for jet nacelles and weapons
line_length_now =0.0
line_length_tot =0.0
for s in range(sdim-1):
line_length_tot+=hypot(v[s+1][0].z-v[s][0].z,
v[s+1][0].y-v[s][0].y)
for s in range(sdim):
for r in range(rsem):
uv[s][r].s=line_length_now/line_length_tot
if s<sdim-1:
line_length_now+=hypot(v[s+1][0].z-v[s][0].z,
v[s+1][0].y-v[s][0].y)
elif hi_z!=lo_z:
# do long-location
for s in range(sdim):
for r in range(rsem):
uv[s][r].s=(hi_z-v[s][r].y)/(hi_z-lo_z)
else:
for s in range(sdim):
for r in range(rsem):
uv[s][r].s=0
# Scale
r=UV(part.top_s1,part.top_t1)
l=UV(part.bot_s1,part.bot_t1)
rs=UV(part.top_s2-part.top_s1,part.top_t2-part.top_t1)
ls=UV(part.bot_s2-part.bot_s1,part.bot_t2-part.bot_t1)
ruv=[]
luv=[]
for i in range(sdim):
ruv.append([])
luv.append([])
for j in range(rsem):
ruv[i].append(r+rs*uv[i][j])
luv[i].append(l+ls*uv[i][j])
# Dodgy LOD heuristics
# offsets less than this (or neg) make body blunt
blunt_front=0.1
isblunt=0
point1=0
miny=99999
maxy=-99999
for i in range(sdim):
if v[i][0].y > maxy: maxy=v[i][0].y
if v[i][0].y < miny: miny=v[i][0].y
length=maxy-miny
for i in range(sdim/2,0,-1):
if v[i][0].y>=v[i-1][0].y-blunt_front:
isblunt=1
point1=i
break
if isblunt:
if self.debug: print "Blunt front %s," % point1,
body_pt2=0.33
body_pt3=0.50
else:
if self.debug: print "Sharp front,",
body_pt2=0.30
body_pt3=0.67
# Find front of cabin
point2=point1+1
for i in range(point1,sdim-1):
if v[i][0].y<=v[0][0].y-length*body_pt2:
point2=i
break
point2=point2-1
# Find ends of main body
point3=point2+1
for i in range(point3,sdim-1):
if v[i][0].y<=v[0][0].y-length*body_pt3:
point3=i
break
for i in range(point3,sdim-1):
if v[i][0].y<v[i+1][0].y:
point3=i
if self.debug: print "Blunt end %s" % point3
break