-
Notifications
You must be signed in to change notification settings - Fork 11
/
importMAX.py
1155 lines (1039 loc) · 36.6 KB
/
importMAX.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
# -*- coding: utf8 -*-
__title__ = "FreeCAD Autodesk 3DS Max importer"
__author__ = "Jens M. Plonka"
__url__ = "https://www.github.com/jmplonka/Importer3D"
import FreeCAD, triangulate, numpy, zlib, sys, traceback
from importUtils import missingDependency, canImport, newObject, getValidName, getByte, getShorts, getShort, getInts, getInt, getFloats, getFloat, setEndianess, LITTLE_ENDIAN
from math import degrees
from struct import Struct, unpack
from BasicShapes import Shapes, ViewProviderShapes
try:
import olefile
except:
missingDependency("olefile")
UNPACK_BOX_DATA = Struct('<hihhbff').unpack_from # Index, int, short, short, byte, float, Length
DEBUG = False # Dump chunk content to console?
TYP_NAME = 0x0962
CLS_DATA = []
CONFIG = []
DLL_DIR_LIST = []
CLS_DIR3_LIST = []
VID_PST_QUE = []
SCENE_LIST = []
SKIPPABLE = {
0x0000000000001002: 'Camera',
0x0000000000001011: 'Omni',
0x0000000000001013: 'Free Direct',
0x0000000000001020: 'Camera Target',
0x0000000000001040: 'Line',
0x0000000000001065: 'Rectangle',
0x0000000000001097: 'Ellipse',
0x0000000000001999: 'Circle',
0x0000000000002013: 'Point',
0x0000000000009125: 'Biped Object',
0x0000000000876234: 'Dummy',
0x05622B0D69011E82: 'Compass',
0x12A822FB76A11646: 'CV Surface',
0x1EB3430074F93B07: 'Particle View',
0x2ECCA84028BF6E8D: 'Bone',
0x3BDB0E0C628140F6: 'VRayPlane',
0x4E9B599047DB14EF: 'Slider',
0x522E47057BF61478: 'Sky',
0x5FD602DF3C5575A1: 'VRayLight',
0x77566F65081F1DFC: 'Plane',
}
class AbstractChunk():
def __init__(self, type, size, level, number):
self.number = number
self.type = type
self.level = level
self.parent = None
self.previous = None
self.next = None
self.size = size
self.unknown = True
self.format = None
self.data = None
self.resolved = False
def __str__(self):
if (self.unknown == True):
return "%s[%4x] %04X: %s" %(" "*self.level, self.number, self.type, ":".join("%02x"%(c) for c in self.data))
return "%s[%4x] %04X: %s=%s" %(" "*self.level, self.number, self.type, self.format, self.data)
class ByteArrayChunk(AbstractChunk):
def __init__(self, type, data, level, number): AbstractChunk.__init__(self, type, data, level, number)
def set(self, data, name, format, start, end):
try:
self.data = unpack(format, data[start:end])
self.format = name
self.unknown = False
except Exception as e:
self.data = data
def setStr16(self, data):
try:
self.data = data.decode('UTF-16LE')
self.format = "Str16"
self.unknown = False
except:
self.data = data
def setLStr16(self, data):
try:
l, o = getInt(data, 0)
self.data = data[o:o+l*2].decode('utf-16-le')
if (self.data[-1] == b'\0'): self.data = self.data[0:-1]
self.format = "LStr16"
self.unknown = False
except:
self.data = data
def setData(self, data):
if (self.type in [0x0340, 0x4001, 0x0456, 0x0962]): self.setStr16(data)
elif (self.type in [0x2034, 0x2035]): self.set(data, "int{}", '<' + 'I'*int(len(data)/4), 0, len(data))
elif (self.type in [0x2501, 0x2503, 0x2504, 0x2505, 0x2511]): self.set(data, "float[]", '<' + 'f'*int(len(data)/4), 0, len(data))
elif (self.type == 0x2510): self.set(data, "struct", '<' + 'f'*int(len(data)/4 - 1) + 'i', 0, len(data))
elif (self.type == 0x0100): self.set(data, "float", '<f' , 0, len(data))
else:
self.unknown = True
self.data = data
try:
if (DEBUG): FreeCAD.Console.PrintMessage("%s\n" %(self))
except Exception as e:
self.format = None
self.unknown = True
self.data = ":".join("%02x"%(c) for c in data)
if (DEBUG): FreeCAD.Console.PrintMessage("%s\n" %(self))
class ClsDir3Chunk(ByteArrayChunk):
def __init__(self, type, data, level, number):
AbstractChunk.__init__(self, type, data, level, number)
self.dll = None
def setData(self, data):
if (self.type == 0x2042): self.setStr16(data) # ClsName
elif (self.type == 0x2060): self.set(data, "struct", '<IQI', 0, 16) # DllIndex, ID, SuperID
else:
self.unknown = False
self.data = ":".join("%02x"%(c) for c in data)
try:
if (DEBUG): FreeCAD.Console.PrintMessage("%s\n" %(self))
except:
self.format = None
self.unknown = False
self.data = data
if (DEBUG): FreeCAD.Console.PrintMessage("%s\n" %(self))
class DllDirChunk(ByteArrayChunk):
def __init__(self, type, data, level, number): AbstractChunk.__init__(self, type, data, level, number)
def setData(self, data):
if (self.type == 0x2039): self.setStr16(data)
elif (self.type == 0x2037): self.setStr16(data)
try:
if (DEBUG): FreeCAD.Console.PrintMessage("%s\n" %(self))
except:
self.format = None
self.unknown = False
self.data = ":".join("%02x"%(c) for c in data)
if (DEBUG): FreeCAD.Console.PrintMessage("%s\n" %(self))
class ContainerChunk(AbstractChunk):
def __init__(self, type, data, level, number, primitiveReader=ByteArrayChunk):
AbstractChunk.__init__(self, type, data, level, number)
self.primitiveReader = primitiveReader
def __str__(self):
if (self.unknown == True):
return "%s[%4x] %04X" %(" "*self.level, self.number, self.type)
return "%s[%4x] %04X: %s" %(" "*self.level, self.number, self.type, self.format)
def getFirst(self, type):
for child in self.children:
if (child.type == type): return child
return None
def setData(self, data):
previous = None
next = None
reader = ChunkReader()
if (DEBUG): FreeCAD.Console.PrintMessage("%s\n" %(self))
self.children = reader.getChunks(data, self.level + 1, ContainerChunk, self.primitiveReader)
class SceneChunk(ContainerChunk):
def __init__(self, type, data, level, number, primitiveReader=ByteArrayChunk):
AbstractChunk.__init__(self, type, data, level, number)
self.primitiveReader = primitiveReader
self.matrix = None
def __str__(self):
if (self.unknown == True):
return "%s[%4x] %s" %(" "*self.level, self.number, getClsName(self))
return "%s[%4x] %s: %s" %(" "*self.level, self.number, getClsName(self), self.format)
def setData(self, data):
previous = None
next = None
if (DEBUG): FreeCAD.Console.PrintMessage("%s\n" %(self))
reader = ChunkReader()
self.children = reader.getChunks(data, self.level + 1, SceneChunk, ByteArrayChunk)
class ChunkReader():
def __init__(self, name = None): self.name = name
def getChunks(self, data, level, containerReader, primitiveReader):
chunks = []
offset = 0
if (level == 0):
t, o = getShort(data, 0)
l, o = getInt(data, o)
if (t == 0x8B1F):
t, o = getInt(data, o)
if (t == 0x0B000000):
data = zlib.decompress(data, zlib.MAX_WBITS|32)
if (level==0):
progressbar = FreeCAD.Base.ProgressIndicator()
progressbar.start(" reading '%s'..."%self.name, len(data))
while offset < len(data):
old = offset
offset, chunk = self.getNextChunk(data, offset, level, len(chunks), containerReader, primitiveReader)
if (level==0):
for i in range(offset - old):
progressbar.next()
chunks.append(chunk)
if (level==0): progressbar.stop()
return chunks
def getNextChunk(self, data, offset, level, number, containerReader, primitiveReader):
header = 6
typ, siz, = unpack("<Hi", data[offset:offset+header])
chunkSize = siz & 0x7FFFFFFF
if (siz == 0):
siz, = unpack("<q", data[offset+header:offset+header+8])
header += 8
chunkSize = siz & 0x7FFFFFFFFFFFFFFF
if (siz < 0):
chunk = containerReader(typ, chunkSize, level, number, primitiveReader)
else:
chunk = primitiveReader(typ, chunkSize, level, number)
chunkData = data[offset + header:offset + chunkSize]
chunk.setData(chunkData)
return offset + chunkSize, chunk
class PointNi3s():
def __init__(self):
self.points = None
self.flags = 0
self.fH = 0
self.f1 = 0
self.f2 = 0
self.fA = []
def __str__(self):
return "[%s] - %X, %X, %X, [%s]" %('/'.join("%d" %p for p in self.points), self.fH, self.f1, self.f2, ','.join("%X" %f for f in self.fA))
class Material():
def __init__(self):
self.data = {}
def set(self, name, value): self.data[name] = value
def get(self, name, default=None):
value = None
if (name in self.data): value = self.data[name]
if (value is None): return default
return value
def getNode(index):
global SCENE_LIST
if (index < len(SCENE_LIST[0].children)):
return SCENE_LIST[0].children[index]
return None
def getNodeParent(node):
parent = None
if (node):
chunk = node.getFirst(0x0960)
if (chunk is not None):
idx, offset = getInt(chunk.data, 0)
parent = getNode(idx)
if (parent is None):
FreeCAD.Console.PrintError("parent index %X < %X!\n" %(idx, len(SCENE_LIST)))
return parent
def getNodeName(node):
if (node):
name = node.getFirst(TYP_NAME)
if (name): return name.data
return None
def getClass(chunk):
global CLS_DIR3_LIST
if (chunk.type < len(CLS_DIR3_LIST)):
return CLS_DIR3_LIST[chunk.type]
return None
def getDll(container):
global DLL_DIR_LIST
idx = container.getFirst(0x2060).data[0]
if (idx < len(DLL_DIR_LIST)):
return DLL_DIR_LIST[idx]
return None
def getGUID(chunk):
cls = getClass(chunk)
if (cls): return cls.getFirst(0x2060).data[1]
return chunk.type
def getSuperId(chunk):
cls = getClass(chunk)
if (cls): return cls.getFirst(0x2060).data[2]
return None
def getClsName(chunk):
cls = getClass(chunk)
if (cls):
clsName = cls.getFirst(0x2042).data
try:
return "'%s'" %(clsName)
except:
return "'%r'" %(clsName)
return u"%04X" %(chunk.type)
def getReferences(chunk):
references = []
refs = chunk.getFirst(0x2034)
if (refs):
references = [getNode(idx) for idx in refs.data]
return references
def getTypedRefernces(chunk):
references = {}
refs = chunk.getFirst(0x2035)
if (refs):
type = refs.data[0]
offset = 1
while offset < len(refs.data):
key = refs.data[offset]
offset += 1
idx = refs.data[offset]
offset += 1
references[key] = getNode(idx)
return references
def readChunks(ole, name, fileName, containerReader=ContainerChunk, primitiveReader=ByteArrayChunk):
with ole.openstream(name) as file:
scene = file.read()
# with open(fileName, 'wb') as file:
# file.write(scene)
reader = ChunkReader(name)
return reader.getChunks(scene, 0, containerReader, primitiveReader)
def readClassData(ole, fileName):
global CLS_DATA
CLS_DATA = readChunks(ole, 'ClassData', fileName+'.ClsDat.bin')
def readClassDirectory3(ole, fileName):
global CLS_DIR3_LIST
try:
CLS_DIR3_LIST = readChunks(ole, 'ClassDirectory3', fileName+'.ClsDir3.bin', ContainerChunk, ClsDir3Chunk)
except:
CLS_DIR3_LIST = readChunks(ole, 'ClassDirectory', fileName+'.ClsDir.bin', ContainerChunk, ClsDir3Chunk)
for clsDir in CLS_DIR3_LIST:
clsDir.dll = getDll(clsDir)
def readConfig(ole, fileName):
global CONFIG
CONFIG = readChunks(ole, 'Config', fileName+'.Cnf.bin')
def readDllDirectory(ole, fileName):
global DLL_DIR_LIST
DLL_DIR_LIST = readChunks(ole, 'DllDirectory', fileName+'.DllDir.bin', ContainerChunk, DllDirChunk)
def readVideoPostQueue(ole, fileName):
global VID_PST_QUE
VID_PST_QUE = readChunks(ole, 'VideoPostQueue', fileName+'.VidPstQue.bin')
def getPoint(float, default = 0.0):
uid = getGUID(float)
if (uid == 0x2007): # Bezier-Float
f = float.getFirst(0x7127)
if (f):
try:
return f.getFirst(0x2501).data[0]
except:
FreeCAD.Console.PrintWarning("SyntaxError: %s - assuming 0.0!\n" %(float))
return default
if (uid == 0x71F11549498702E7): # Float Wire
f = getReferences(float)[0]
return getPoint(f)
else:
FreeCAD.Console.PrintError("Unknown float type 0x%04X=%s!\n" %(uid, float))
return default
def getPoint3D(chunk, default=0.0):
floats = []
if (chunk):
refs = getReferences(chunk)
for float in refs:
f = getPoint(float, default)
if (f is not None):
floats.append(f)
return floats
def getPosition(pos):
mtx = numpy.identity(4, numpy.float32)
if (pos):
uid = getGUID(pos)
if (uid == 0xFFEE238A118F7E02): # => Position XYZ
pos = getPoint3D(pos)
elif (uid == 0x0000000000442312): # => TCB Position
pos = pos.getFirst(0x2503).data
elif (uid == 0x0000000000002008): # => Bezier Position
pos = pos.getFirst(0x2503).data
else:
pos = None
FreeCAD.Console.PrintError("Unknown position 0x%04X=%s!\n" %(uid, pos))
if (pos):
mtx[0,3] = pos[0]
mtx[1,3] = pos[1]
mtx[2,3] = pos[2]
return mtx
def getRotation(pos):
r = None
mtx = numpy.identity(4, numpy.float32)
if (pos):
uid = getGUID(pos)
if (uid == 0x2012): # => Euler XYZ
rot = getPoint3D(pos)
r = FreeCAD.Rotation(degrees(rot[2]), degrees(rot[1]), degrees(rot[0]))
elif (uid == 0x0000000000442313): #'TCB Rotation'
rot = pos.getFirst(0x2504).data
r = FreeCAD.Rotation(rot[0], rot[1], rot[2], rot[3])
elif (uid == 0x000000004B4B1003): #'Rotation List'
refs = getReferences(pos)
if (len(refs) > 3):
return getRotation(refs[0])
elif (uid == 0x3A90416731381913): #'Rotation Wire'
return getRotation(getReferences(pos)[0])
else:
FreeCAD.Console.PrintError("Unknown rotation 0x%04X=%s!\n" %(uid, pos))
if (r):
m = FreeCAD.Placement(FreeCAD.Vector(), r).toMatrix()
mtx = numpy.array([
[m.A11, m.A12, m.A13, m.A14],
[m.A21, m.A22, m.A23, m.A24],
[m.A31, m.A32, m.A33, m.A34],
[m.A41, m.A42, m.A43, m.A44]], numpy.float32)
return mtx
def getScale(pos):
mtx = numpy.identity(4, numpy.float32)
if (pos):
uid = getGUID(pos)
if (uid == 0x2010): # => Bezier Scale
scale = pos.getFirst(0x2501)
if (scale is None): scale = pos.getFirst(0x2505)
pos = scale.data
elif (uid == 0x0000000000442315): # 'TCB Zoom'
scale = pos.getFirst(0x2501)
if (scale is None): scale = pos.getFirst(0x2505)
pos = scale.data
elif (uid == 0xFEEE238B118F7C01): # 'ScaleXYZ'
pos = getPoint3D(pos, 1.0)
else:
FreeCAD.Console.PrintError("Unknown scale 0x%04X=%s!\n" %(uid, pos))
return mtx
mtx[0,0] = pos[0]
mtx[1,1] = pos[1]
mtx[2,2] = pos[2]
return mtx
def createMatrix(prc):
mtx = numpy.identity(4, numpy.float32)
uid = getGUID(prc)
scl = None
rot = None
pos = None
if (uid == 0x2005): # Position/Rotation/Scale
pos = getPosition(getReferences(prc)[0])
rot = getRotation(getReferences(prc)[1])
scl = getScale(getReferences(prc)[2])
elif (uid == 0x9154) : # BipSlave Control
bipedSubAnim = getReferences(prc)[2]
refs = getReferences(bipedSubAnim)
scl = getScale(getReferences(refs[1])[0])
rot = getRotation(getReferences(refs[2])[0])
pos = getPosition(getReferences(refs[3])[0])
if (pos is not None):
mtx = numpy.dot(mtx, pos)
if (rot is not None):
mtx = numpy.dot(mtx, rot)
if (scl is not None):
mtx = numpy.dot(mtx, scl)
return mtx
def getProperty(properties, idx):
for child in properties.children:
if (child.type == 0x100E):
if (getShort(child.data, 0)[0] == idx): return child
return None
def getColorMax(colors, idx):
prp = getProperty(colors, idx)
if (prp is not None):
c, o = getFloats(prp.data, 15, 3)
return (c[0], c[1], c[2])
return None
def getFloatMax(colors, idx):
prp = getProperty(colors, idx)
if (prp is not None):
f, o = getFloat(prp.data, 15)
return f
return None
def getMatStandard(refs):
material = None
try:
if (len(refs) > 2):
colors = refs[2]
parameters = getReferences(colors)[0] # ParameterBlock2
material = Material()
material.set('ambient', getColorMax(parameters, 0x00))
material.set('diffuse', getColorMax(parameters, 0x01))
material.set('specular', getColorMax(parameters, 0x02))
material.set('emissive', getColorMax(parameters, 0x08))
material.set('shinines', getFloatMax(parameters, 0x0A))
transparency = refs[4] # ParameterBlock2
material.set('transparency', getFloatMax(transparency, 0x02))
except:
FreeCAD.Console.PrintError(traceback.format_exc())
FreeCAD.Console.PrintError('\n')
return material
def getMatVRay(vry):
material = Material()
try:
material.set('diffuse', getColorMax(vry, 0x01))
material.set('ambient', getColorMax(vry, 0x02))
material.set('specular', getColorMax(vry, 0x05))
# material.set('emissive', getColorMax(vry, 0x05))
# material.set('shinines', getFloatMax(vry, 0x0B))
# material.set('transparency', getFloatMax(vry, 0x02))
except:
FreeCAD.Console.PrintError(traceback.format_exc())
FreeCAD.Console.PrintError('\n')
return material
def getMatArchDesign(ad):
material = Material()
try:
material.set('diffuse', getColorMax(ad, 0x1A))
# material.set('ambient', getColorMax(ad, 0x02))
# material.set('specular', getColorMax(ad, 0x05))
# material.set('emissive', getColorMax(ad, 0x05))
# material.set('shinines', getFloatMax(ad, 0x0B))
# material.set('transparency', getFloatMax(ad, 0x02))
except:
FreeCAD.Console.PrintError(traceback.format_exc())
FreeCAD.Console.PrintError('\n')
return material
def adjustMaterial(obj, mat):
material = None
if (mat is not None):
uid = getGUID(mat)
if (uid == 0x0002): # 'Standard'
refs = getReferences(mat)
material = getMatStandard(refs)
elif (uid == 0x0000000000000200): # 'Multi/Sub-Object'
refs = getReferences(mat)
material = adjustMaterial(obj, refs[-1])
elif (uid == 0x7034695C37BF3F2F): # 'VRayMtl'
refs = getTypedRefernces(mat)
material = getMatVRay(refs[1])
elif (uid == 0x4A16365470B05735): # 'Arch & Design'
refs = getReferences(mat)
material = getMatArchDesign(refs[0])
else:
FreeCAD.Console.PrintWarning("Unknown material GUID=%016X (%s) - skipped\n!" %(uid, getClsName(mat)))
if (obj is not None) and (material is not None):
obj.ViewObject.ShapeMaterial.AmbientColor = material.get('ambient', (0,0,0))
obj.ViewObject.ShapeMaterial.DiffuseColor = material.get('diffuse', (0.8,0.8,0.8))
#obj.ViewObject.ShapeMaterial.EmissiveColor = material.get('emissive', (0,0,0))
obj.ViewObject.ShapeMaterial.SpecularColor = material.get('specular', (0,0,0))
obj.ViewObject.ShapeMaterial.Shininess = material.get('shinines', 0.2)
obj.ViewObject.ShapeMaterial.Transparency = material.get('transparency', 0.0)
def createShape3d(doc, pts, indices, shape, key, prc, mat):
name = shape.getFirst(TYP_NAME).data
cnt = len(pts)
if (cnt > 0):
if (key is not None): name = "%s_%d" %(name, key)
mtx = createMatrix(prc)
# translate the points according to the transformation matrix
pt = numpy.ones((cnt, 4), numpy.float32)
pt[:,:3] = pts
tpt = numpy.transpose(numpy.dot(mtx, numpy.transpose(pt)))
data = []
for pol in indices:
if (len(pol) > 2): # skip points and lines!
try:
ngon = [tpt[idx][0:3] for idx in pol]
for triangle in triangulate.getTriangles(ngon):
data.append(triangle)
except:
pass
if (len(data) > 0):
obj = newObject(doc, name, data)
adjustMaterial(obj, mat)
return True
FreeCAD.Console.PrintWarning("no faces ... ")
return True
def calcCoordinates(data):
l, o = getInt(data, 0)
cnt = len(data) // 16
p = numpy.zeros((cnt, 3), numpy.float32)
i = 0
while (o < len(data)):
w, o = getInt(data, o)
f, o = getFloats(data, o, 3)
p[i,] = f
i += 1
return p
def calcCoordinatesI(data):
l, o = getInt(data, 0)
cnt = len(data) / 12
p = numpy.zeros((cnt, 3), numpy.float32)
i = 0
while (o < len(data)):
f, o = getFloats(data, o, 3)
p[i:0:3] = f
i += 1
return p
def getNGons4i(points):
vertex = {}
for point in points:
ngon = point.points
key = point.fH
if (key not in vertex):
vertex[key] = []
vertex[key].append(ngon)
return vertex
def getNGons5i(data):
count, o = getInt(data, 0)
ngons = []
while count > 0:
p, o = getInts(data, o, 3)
o += 8
ngons.append(p)
count -= 1
return ngons
def getNGons6i(data):
cnt, o = getInt(data, 0)
list = []
while (o < len(data)):
l, o = getInts(data, o, 6)
i = 5
while ((i > 3) and (l[i] < 0)):
i -= 1
if (i>2): list.append(l[1:i])
return list
def getNGonsNi(polys):
vertex = []
o = 0
while (o < len(polys)):
num = polys[o]
o += 1
ngon = []
k = 0
while (k < num):
p = points[polys[o]][1]
ngon.append(p)
o += 1
k += 1
vertex.append(ngon)
return vertex
def getNGonsInts(chunk):
o = 0
list = []
data = chunk.data
while (o < len(data)):
cnt, o = getInt(data, o)
points, o = getInts(data, o, cnt)
list.append(points)
return list
def calcPointNi3s(chunk):
data = chunk.data
cnt, o = getInt(data, 0)
list = []
try:
while (o < len(data)):
p = PointNi3s()
l, o = getInt(data, o)
p.points, o = getInts(data, o, l)
p.flags, o= getShort(data, o)
if ((p.flags & 0x01) != 0): p.f1, o = getInt(data, o)
if ((p.flags & 0x08) != 0): p.fH, o = getShort(data, o)
if ((p.flags & 0x10) != 0): p.f2, o = getInt(data, o)
if ((p.flags & 0x20) != 0): p.fA, o = getInts(data, o, 2 * (l - 3))
if (len(p.points) > 0):
list.append(p)
except Exception as e:
FreeCAD.Console.PrintError(traceback.format_exc())
FreeCAD.Console.PrintError('\n')
FreeCAD.Console.PrintError("%s: o = %d\n" %(e, o))
raise e
return list
def createDocObject(doc, name, creator):
obj = doc.addObject(creator, getValidName(name))
obj.Label = name
return obj
def createEditablePoly(doc, shape, msh, mat, mtx):
name = shape.getFirst(TYP_NAME).data
FreeCAD.Console.PrintMessage(" building Editible Poly '%s' ... " %(name))
ply = msh.getFirst(0x08FE)
indexList = [] # texture groups
coordListI = [] # texture coordinates
indicesList = [] # texture indices
point3i = None
point4i = None
point6i = None
pointNi = None
coords = None
created = False
if (ply):
for child in ply.children:
if (child.type == 0x0100): coords = calcCoordinates(child.data)# #, n x (g=uint16,x=float16,y=float16,z=float16)
elif (child.type == 0x0108): point6i = child.data
# elif (child.type == 0x010A): point3i = child.data
elif (child.type == 0x011A): point4i = calcPointNi3s(child)# comparable with 0x012B!!
# elif (child.type == 0x0120): pass # Number of groups+1
# elif (child.type == 0x0124): indexList.append(getInt(child.data, 0)[0])
# elif (child.type == 0x0128): coordListI.append(calcCoordinatesI(child.data))
# elif (child.type == 0x012B): indicesList.append(getNGonsInts(child))
# elif (child.type == 0x0130): pass # always 0
# elif (child.type == 0x0140): pass # always 0x40
# elif (child.type == 0x0150): pass
# elif (child.type == 0x0200): pass
# elif (child.type == 0x0210): pass # n, i * 1.0
# elif (child.type == 0x0240): pass
# elif (child.type == 0x0250): pass
elif (child.type == 0x0310): pointNi = child.data
# if (len(indexList) > 0):
# FreeCAD.Console.PrintMessage(" %s " %(str(indexList)))
# for i in range(len(indexList)):
# created |= createShape3d(doc, coords, indicesList[i], shape, indexList[i], mtx, mat)
# elif (point4i is not None):
if (point4i is not None):
vertex = getNGons4i(point4i)
if (len(vertex) > 0):
for key, ngons in vertex.items():
FreeCAD.Console.PrintMessage("[%d] " %(key))
created |= createShape3d(doc, coords, ngons, shape, key, mtx, mat)
else:
created = True
FreeCAD.Console.PrintWarning("no faces ... ")
elif (point6i is not None):
ngons = getNGons6i(point6i)
created = createShape3d(doc, coords, ngons, shape, None, mtx, mat)
elif (pointNi is not None):
ngons = getNGonsNi(pointNi)
created = createShape3d(doc, coords, ngons, shape, None, mtx, mat)
else:
FreeCAD.Console.PrintError("hugh? - no data found for %s?!?" %(ply))
return created
def getArrayPoint3f(values):
v = []
if len(values) >= 4:
count, offset = getInt(values, 0)
while (count > 0):
floats, offset = getFloats(values, offset, 3)
v.append(floats)
count -= 1
return v
def createEditableMesh(doc, shape, msh, mat, mtx):
name = shape.getFirst(TYP_NAME).data
FreeCAD.Console.PrintMessage(" building Editable Mesh '%s' ... "%(name))
ply = msh.getFirst(0x08FE)
created = False
if (ply):
vertexChunk = ply.getFirst(0x0914)
indexChunk = ply.getFirst(0x0912)
coords = getArrayPoint3f(vertexChunk.data)
ngons = getNGons5i(indexChunk.data)
created = createShape3d(doc, coords, ngons, shape, None, mtx, mat)
return created
def getMtxMshMatLyr(shape):
refs = getTypedRefernces(shape)
if (refs):
mtx = refs.get(0, None)
msh = refs.get(1, None)
mat = refs.get(3, None)
lyr = refs.get(6, None)
else:
refs = getReferences(shape)
mtx = refs[0]
msh = refs[1]
mat = refs[3]
lyr = None
if (len(refs) > 6):
lyr = refs[6]
return mtx, msh, mat, lyr
def adjustPlacement(obj, node):
mtx = createMatrix(node).flatten()
plc = FreeCAD.Placement(FreeCAD.Matrix(*mtx))
obj.Placement = plc
return plc
def createShell(doc, shape, shell, mat, mtx):
name = shape.getFirst(TYP_NAME).data
FreeCAD.Console.PrintMessage(" building Shell '%s' ... " %(name))
refs = getReferences(shell)
msh = refs[-1]
created, uid = createMesh(doc, shape, msh, mtx, mat)
if (not created):
FreeCAD.Console.PrintError("hugh? %016X: %s - " %(uid, getClsName(msh)))
return created
def createBox(doc, shape, box, mat, mtx):
name = shape.getFirst(TYP_NAME).data
FreeCAD.Console.PrintMessage(" building Box '%s' ... " %(name))
obj = createDocObject(doc, name, "Part::Box")
pBlock = getReferences(box)[0]
try:
obj.Length = pBlock.children[2].getFirst(0x0100).data[0]
obj.Width = pBlock.children[3].getFirst(0x0100).data[0]
h = pBlock.children[4].getFirst(0x0100).data[0]
except:
obj.Length = UNPACK_BOX_DATA(pBlock.children[1].data)[6]
obj.Width = UNPACK_BOX_DATA(pBlock.children[2].data)[6]
h = UNPACK_BOX_DATA(pBlock.children[3].data)[6]
if (h < 0):
obj.Height = -h
plc = adjustPlacement(obj, mtx)
try:
axis = obj.Shape.Faces[4].Surface.Axis * h
obj.Placement = FreeCAD.Placement(plc.Base + axis, plc.Rotation, FreeCAD.Vector(0, 0, 0))
except:
pass
else:
obj.Height = h
adjustPlacement(obj, mtx)
box.geometry = obj
return True
def createSphere(doc, shape, sphere, mat, mtx):
name = shape.getFirst(TYP_NAME).data
FreeCAD.Console.PrintMessage(" building Sphere '%s' ... "%(name))
obj = createDocObject(doc, name, "Part::Sphere")
pBlock = getReferences(sphere)[0]
try:
obj.Radius = pBlock.children[2].getFirst(0x0100).data[0]
except:
obj.Radius = UNPACK_BOX_DATA(pBlock.children[1].data)[6]
adjustPlacement(obj, mtx)
sphere.geometry = obj
return True
def createCylinder(doc, shape, cylinder, mat, mtx):
name = shape.getFirst(TYP_NAME).data
FreeCAD.Console.PrintMessage(" building Cylinder '%s' ... "%(name))
obj = createDocObject(doc, name, "Part::Cylinder")
pBlock = getReferences(cylinder)[0]
try:
r = pBlock.children[2].getFirst(0x0100).data[0]
h = pBlock.children[3].getFirst(0x0100).data[0]
except:
r = UNPACK_BOX_DATA(pBlock.children[1].data)[6]
h = UNPACK_BOX_DATA(pBlock.children[2].data)[6]
if (r < 0):
obj.Radius = -r
else:
obj.Radius = r
if (h < 0):
obj.Height = -h
plc = adjustPlacement(obj, mtx)
try:
axis = obj.Shape.Faces[0].Surface.Axis * h
obj.Placement = FreeCAD.Placement(plc.Base + axis, plc.Rotation, FreeCAD.Vector(0, 0, 0))
except:
pass
else:
obj.Height = h
adjustPlacement(obj, mtx)
cylinder.geometry = obj
return True
def createTorus(doc, shape, torus, mat, mtx):
name = shape.getFirst(TYP_NAME).data
FreeCAD.Console.PrintMessage(" building Torus '%s' ... "%(name))
obj = createDocObject(doc, name, 'Part::Torus')
pBlock = getReferences(torus)[0]
try:
obj.Radius1 = pBlock.children[2].getFirst(0x0100).data[0]
obj.Radius2 = pBlock.children[3].getFirst(0x0100).data[0]
except:
obj.Radius1 = UNPACK_BOX_DATA(pBlock.children[1].data)[6]
obj.Radius2 = UNPACK_BOX_DATA(pBlock.children[2].data)[6]
obj.Angle1 = -180.0
obj.Angle2 = 180.0
obj.Angle3 = 360.0
adjustPlacement(obj, mtx)
torus.geometry = obj
return True
def createTube(doc, shape, tube, mat, mtx):
name = shape.getFirst(TYP_NAME).data
FreeCAD.Console.PrintMessage(" building Tube '%s' ... "%(name))
obj = createDocObject(doc, name, 'Part::FeaturePython')
Shapes.TubeFeature(obj)
vp = ViewProviderShapes.ViewProviderTube(obj.ViewObject)
pBlock = getReferences(tube)[0]
try:
obj.InnerRadius = pBlock.children[2].getFirst(0x0100).data[0]
obj.OuterRadius = pBlock.children[3].getFirst(0x0100).data[0]
obj.Height = pBlock.children[4].getFirst(0x0100).data[0]
except:
obj.InnerRadius = UNPACK_BOX_DATA(pBlock.children[1].data)[6]
obj.OuterRadius = UNPACK_BOX_DATA(pBlock.children[2].data)[6]
obj.Height = UNPACK_BOX_DATA(pBlock.children[3].data)[6]
adjustPlacement(obj, mtx)
tube.geometry = obj
return True
def createCone(doc, shape, cone, mat, mtx):
name = shape.getFirst(TYP_NAME).data
FreeCAD.Console.PrintMessage(" building Cone '%s' ... "%(name))
obj = createDocObject(doc, name, 'Part::Cone')
pBlock = getReferences(cone)[0]
try:
obj.Radius2 = pBlock.children[2].getFirst(0x0100).data[0]
obj.Radius1 = pBlock.children[3].getFirst(0x0100).data[0]
h = pBlock.children[4].getFirst(0x0100).data[0]
except:
obj.Radius2 = UNPACK_BOX_DATA(pBlock.children[1].data)[6]
obj.Radius1 = UNPACK_BOX_DATA(pBlock.children[2].data)[6]
h = UNPACK_BOX_DATA(pBlock.children[3].data)[6]
obj.Angle = 360.0
if (h < 0):
obj.Height = -h
plc = adjustPlacement(obj, mtx)
try:
axis = obj.Shape.Faces[1].Surface.Axis * h
obj.Placement = FreeCAD.Placement(plc.Base + axis, plc.Rotation, FreeCAD.Vector(0, 0, 0))
except:
pass
else:
obj.Height = h
adjustPlacement(obj, mtx)
adjustPlacement(obj, mtx)
cone.geometry = obj
return True
def createGeoSphere(doc, shape, geo, mat, mtx):
name = shape.getFirst(TYP_NAME).data
FreeCAD.Console.PrintMessage(" building GeoSphere '%s' ... "%(name))
obj = createDocObject(doc, name, "Part::Sphere")
pBlock = getReferences(geo)[0]
try:
obj.Radius = pBlock.children[4].getFirst(0x0100).data[0]
except:
obj.Radius = UNPACK_BOX_DATA(pBlock.children[3].data)[6]
adjustPlacement(obj, mtx)
geo.geometry = obj
return True
def createTeapot(doc, shape, teapot, mat, mtx):
return createSkippable(doc, shape, teapot, mat, mtx, 'Teapot')
def createPlane(doc, shape, plane, mat, mtx):
name = shape.getFirst(TYP_NAME).data
FreeCAD.Console.PrintMessage(" building Plane '%s' ... "%(name))
obj = createDocObject(doc, name, 'Part::Plane')
pBlock = getReferences(plane)[0]
try:
obj.Length = pBlock.children[2].getFirst(0x0100).data[0]
obj.Width = pBlock.children[3].getFirst(0x0100).data[0]
except:
obj.Length = UNPACK_BOX_DATA(pBlock.children[1].data)[6]
obj.Width = UNPACK_BOX_DATA(pBlock.children[2].data)[6]
adjustPlacement(obj, mtx)