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

ms3d_spec.py « io_scene_ms3d - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d5eadbee2ff79fb0fafe55afc5e3558ad38d7501 (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
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

# <pep8 compliant>

###############################################################################
#234567890123456789012345678901234567890123456789012345678901234567890123456789
#--------1---------2---------3---------4---------5---------6---------7---------


# ##### BEGIN COPYRIGHT BLOCK #####
#
# initial script copyright (c)2011-2013 Alexander Nussbaumer
#
# ##### END COPYRIGHT BLOCK #####


from struct import (
        pack,
        unpack,
        )
from sys import (
        exc_info,
        )
from codecs import (
        register_error,
        )

###############################################################################
#
# MilkShape 3D 1.8.5 File Format Specification
#
# all specifications were taken from SDK 1.8.5
#
# some additional specifications were taken from
# MilkShape 3D Viewer v2.0 (Nov 06 2007) - msMesh.h
#


###############################################################################
#
# sizes
#

class Ms3dSpec:
    ###########################################################################
    #
    # max values
    #
    MAX_VERTICES = 65534 # 0..65533; note: (65534???, 65535???)
    MAX_TRIANGLES = 65534 # 0..65533; note: (65534???, 65535???)
    MAX_GROUPS = 255 # 1..255; note: (0 default group)
    MAX_MATERIALS = 128 # 0..127; note: (-1 no material)
    MAX_JOINTS = 128 # 0..127; note: (-1 no joint)
    MAX_SMOOTH_GROUP = 32 # 0..32; note: (0 no smoothing group)
    MAX_TEXTURE_FILENAME_SIZE = 128

    ###########################################################################
    #
    # flags
    #
    FLAG_NONE = 0
    FLAG_SELECTED = 1
    FLAG_HIDDEN = 2
    FLAG_SELECTED2 = 4
    FLAG_DIRTY = 8
    FLAG_ISKEY = 16 # additional spec from [2]
    FLAG_NEWLYCREATED = 32 # additional spec from [2]
    FLAG_MARKED = 64 # additional spec from [2]

    FLAG_TEXTURE_NONE = 0x00
    FLAG_TEXTURE_COMBINE_ALPHA = 0x20
    FLAG_TEXTURE_HAS_ALPHA = 0x40
    FLAG_TEXTURE_SPHERE_MAP = 0x80

    MODE_TRANSPARENCY_SIMPLE = 0
    MODE_TRANSPARENCY_DEPTH_BUFFERED_WITH_ALPHA_REF = 1
    MODE_TRANSPARENCY_DEPTH_SORTED_TRIANGLES = 2


    ###########################################################################
    #
    # values
    #
    HEADER = "MS3D000000"


    ## TEST_STR = 'START@€@µ@²@³@©@®@¶@ÿ@A@END.bmp'
    ## TEST_RAW = b'START@\x80@\xb5@\xb2@\xb3@\xa9@\xae@\xb6@\xff@A@END.bmp\x00'
    ##
    STRING_MS3D_REPLACE = 'use_ms3d_replace'
    STRING_ENCODING = "ascii" # wrong encoding (too limited), but there is an UnicodeEncodeError issue, that prevent using the correct one for the moment
    ##STRING_ENCODING = "cp437" # US, wrong encoding and shows UnicodeEncodeError
    ##STRING_ENCODING = "cp858" # Europe + €, wrong encoding and shows UnicodeEncodeError
    ##STRING_ENCODING = "cp1252" # WIN EU, this would be the better codepage, but shows UnicodeEncodeError, on print on system console and writing to file
    STRING_ERROR = STRING_MS3D_REPLACE
    ##STRING_ERROR = 'replace'
    ##STRING_ERROR = 'ignore'
    ##STRING_ERROR = 'surrogateescape'
    STRING_TERMINATION = b'\x00'
    STRING_REPLACE = u'_'


    ###########################################################################
    #
    # min, max, default values
    #
    NONE_VERTEX_BONE_ID = -1
    NONE_GROUP_MATERIAL_INDEX = -1

    DEFAULT_HEADER = HEADER
    DEFAULT_HEADER_VERSION = 4
    DEFAULT_VERTEX_BONE_ID = NONE_VERTEX_BONE_ID
    DEFAULT_TRIANGLE_SMOOTHING_GROUP = 0
    DEFAULT_TRIANGLE_GROUP = 0
    DEFAULT_MATERIAL_MODE = FLAG_TEXTURE_NONE
    DEFAULT_GROUP_MATERIAL_INDEX = NONE_GROUP_MATERIAL_INDEX
    DEFAULT_MODEL_JOINT_SIZE = 1.0
    DEFAULT_MODEL_TRANSPARENCY_MODE = MODE_TRANSPARENCY_SIMPLE
    DEFAULT_MODEL_ANIMATION_FPS = 25.0
    DEFAULT_MODEL_SUB_VERSION_COMMENTS = 1
    DEFAULT_MODEL_SUB_VERSION_VERTEX_EXTRA = 2
    DEFAULT_MODEL_SUB_VERSION_JOINT_EXTRA = 1
    DEFAULT_MODEL_SUB_VERSION_MODEL_EXTRA = 1
    DEFAULT_FLAGS = FLAG_NONE
    MAX_MATERIAL_SHININESS = 128

    # blender default / OpenGL default
    DEFAULT_MATERIAL_AMBIENT = (0.2, 0.2, 0.2, 1.0)
    DEFAULT_MATERIAL_DIFFUSE = (0.8, 0.8, 0.8, 1.0)
    DEFAULT_MATERIAL_SPECULAR = (1.0, 1.0, 1.0, 1.0)
    DEFAULT_MATERIAL_EMISSIVE = (0.0, 0.0, 0.0, 1.0)
    DEFAULT_MATERIAL_SHININESS = 12.5

    DEFAULT_JOINT_COLOR = (0.8, 0.8, 0.8)

###############################################################################
#
# helper class for basic raw io
#
class Ms3dIo:
    # sizes for IO
    SIZE_BYTE = 1
    SIZE_SBYTE = 1
    SIZE_WORD = 2
    SIZE_DWORD = 4
    SIZE_FLOAT = 4
    LENGTH_ID = 10
    LENGTH_NAME = 32
    LENGTH_FILENAME = 128

    PRECISION = 4

    @staticmethod
    def read_byte(raw_io):
        """ read a single byte from raw_io """
        buffer = raw_io.read(Ms3dIo.SIZE_BYTE)
        if not buffer:
            raise EOFError()
        value = unpack('<B', buffer)[0]
        return value

    @staticmethod
    def write_byte(raw_io, value):
        """ write a single byte to raw_io """
        raw_io.write(pack('<B', value))

    @staticmethod
    def read_sbyte(raw_io):
        """ read a single signed byte from raw_io """
        buffer = raw_io.read(Ms3dIo.SIZE_BYTE)
        if not buffer:
            raise EOFError()
        value = unpack('<b', buffer)[0]
        return value

    @staticmethod
    def write_sbyte(raw_io, value):
        """ write a single signed byte to raw_io """
        raw_io.write(pack('<b', value))

    @staticmethod
    def read_word(raw_io):
        """ read a word from raw_io """
        buffer = raw_io.read(Ms3dIo.SIZE_WORD)
        if not buffer:
            raise EOFError()
        value = unpack('<H', buffer)[0]
        return value

    @staticmethod
    def write_word(raw_io, value):
        """ write a word to raw_io """
        raw_io.write(pack('<H', value))

    @staticmethod
    def read_dword(raw_io):
        """ read a double word from raw_io """
        buffer = raw_io.read(Ms3dIo.SIZE_DWORD)
        if not buffer:
            raise EOFError()
        value = unpack('<I', buffer)[0]
        return value

    @staticmethod
    def write_dword(raw_io, value):
        """ write a double word to raw_io """
        raw_io.write(pack('<I', value))

    @staticmethod
    def read_float(raw_io):
        """ read a float from raw_io """
        buffer = raw_io.read(Ms3dIo.SIZE_FLOAT)
        if not buffer:
            raise EOFError()
        value = unpack('<f', buffer)[0]
        return value

    @staticmethod
    def write_float(raw_io, value):
        """ write a float to raw_io """
        raw_io.write(pack('<f', value))

    @staticmethod
    def read_array(raw_io, itemReader, count):
        """ read an array[count] of objects from raw_io, by using a itemReader """
        value = []
        for i in range(count):
            itemValue = itemReader(raw_io)
            value.append(itemValue)
        return tuple(value)

    @staticmethod
    def write_array(raw_io, itemWriter, count, value):
        """ write an array[count] of objects to raw_io, by using a itemWriter """
        for i in range(count):
            itemValue = value[i]
            itemWriter(raw_io, itemValue)

    @staticmethod
    def read_array2(raw_io, itemReader, count, count2):
        """ read an array[count][count2] of objects from raw_io,
            by using a itemReader """
        value = []
        for i in range(count):
            itemValue = Ms3dIo.read_array(raw_io, itemReader, count2)
            value.append(tuple(itemValue))
        return value

    @staticmethod
    def write_array2(raw_io, itemWriter, count, count2, value):
        """ write an array[count][count2] of objects to raw_io,
            by using a itemWriter """
        for i in range(count):
            itemValue = value[i]
            Ms3dIo.write_array(raw_io, itemWriter, count2, itemValue)


    @staticmethod
    def ms3d_replace(exc):
        """ http://www.python.org/dev/peps/pep-0293/ """
        if isinstance(exc, UnicodeEncodeError):
            return ((exc.end-exc.start)*Ms3dSpec.STRING_REPLACE, exc.end)
        elif isinstance(exc, UnicodeDecodeError):
            return (Ms3dSpec.STRING_REPLACE, exc.end)
        elif isinstance(exc, UnicodeTranslateError):
            return ((exc.end-exc.start)*Ms3dSpec.STRING_REPLACE, exc.end)
        else:
            raise TypeError("can't handle %s" % exc.__name__)

    @staticmethod
    def read_string(raw_io, length):
        """ read a string of a specific length from raw_io """
        buffer = raw_io.read(length)
        if not buffer:
            raise EOFError()
        eol = buffer.find(Ms3dSpec.STRING_TERMINATION)
        if eol < 0:
            eol = len(buffer)
        register_error(Ms3dSpec.STRING_MS3D_REPLACE, Ms3dIo.ms3d_replace)
        s = buffer[:eol].decode(encoding=Ms3dSpec.STRING_ENCODING, errors=Ms3dSpec.STRING_ERROR)
        return s

    @staticmethod
    def write_string(raw_io, length, value):
        """ write a string of a specific length to raw_io """
        register_error(Ms3dSpec.STRING_MS3D_REPLACE, Ms3dIo.ms3d_replace)
        buffer = value.encode(encoding=Ms3dSpec.STRING_ENCODING, errors=Ms3dSpec.STRING_ERROR)
        if not buffer:
            buffer = bytes()
        raw_io.write(pack('<{}s'.format(length), buffer))
        return


###############################################################################
#
# multi complex types
#

###############################################################################
class Ms3dHeader:
    """ Ms3dHeader """
    __slots__ = (
            'id',
            'version',
            )

    def __init__(
            self,
            default_id=Ms3dSpec.DEFAULT_HEADER,
            default_version=Ms3dSpec.DEFAULT_HEADER_VERSION
            ):
        self.id = default_id
        self.version = default_version

    def __repr__(self):
        return "\n<id='{}', version={}>".format(
            self.id,
            self.version
            )

    def __hash__(self):
        return hash(self.id) ^ hash(self.version)

    def __eq__(self, other):
        return ((self is not None) and (other is not None)
                and (self.id == other.id)
                and (self.version == other.version))

    def read(self, raw_io):
        self.id = Ms3dIo.read_string(raw_io, Ms3dIo.LENGTH_ID)
        self.version = Ms3dIo.read_dword(raw_io)
        return self

    def write(self, raw_io):
        Ms3dIo.write_string(raw_io, Ms3dIo.LENGTH_ID, self.id)
        Ms3dIo.write_dword(raw_io, self.version)

    class HeaderError(Exception):
        pass



###############################################################################
class Ms3dVertex:
    """ Ms3dVertex """
    """
    __slots__ was taking out,
    to be able to inject additional attributes during runtime
    __slots__ = (
            'flags',
            'bone_id',
            'reference_count',
            '_vertex',
            '_vertex_ex_object', # Ms3dVertexEx
            )
    """

    def __init__(
            self,
            default_flags=Ms3dSpec.DEFAULT_FLAGS,
            default_vertex=(0.0, 0.0, 0.0),
            default_bone_id=Ms3dSpec.DEFAULT_VERTEX_BONE_ID,
            default_reference_count=0,
            default_vertex_ex_object=None, # Ms3dVertexEx
            ):
        self.flags = default_flags
        self._vertex = default_vertex
        self.bone_id = default_bone_id
        self.reference_count = default_reference_count

        if default_vertex_ex_object is None:
            default_vertex_ex_object = Ms3dVertexEx2()
            # Ms3dSpec.DEFAULT_MODEL_SUB_VERSION_VERTEX_EXTRA = 2
        self._vertex_ex_object = default_vertex_ex_object
        # Ms3dVertexEx

    def __repr__(self):
        return "\n<flags={}, vertex=({:.{p}f}, {:.{p}f}, {:.{p}f}), bone_id={},"\
                " reference_count={}>".format(
                self.flags,
                self._vertex[0],
                self._vertex[1],
                self._vertex[2],
                self.bone_id,
                self.reference_count,
                p=Ms3dIo.PRECISION
                )

    def __hash__(self):
        return (hash(self.vertex)
                #^ hash(self.flags)
                #^ hash(self.bone_id)
                #^ hash(self.reference_count)
                )

    def __eq__(self, other):
        return ((self.vertex == other.vertex)
                #and (self.flags == other.flags)
                #and (self.bone_id == other.bone_id)
                #and (self.reference_count == other.reference_count)
                )


    @property
    def vertex(self):
        return self._vertex

    @property
    def vertex_ex_object(self):
        return self._vertex_ex_object


    def read(self, raw_io):
        self.flags = Ms3dIo.read_byte(raw_io)
        self._vertex = Ms3dIo.read_array(raw_io, Ms3dIo.read_float, 3)
        self.bone_id = Ms3dIo.read_sbyte(raw_io)
        self.reference_count = Ms3dIo.read_byte(raw_io)
        return self

    def write(self, raw_io):
        Ms3dIo.write_byte(raw_io, self.flags)
        Ms3dIo.write_array(raw_io, Ms3dIo.write_float, 3, self.vertex)
        Ms3dIo.write_sbyte(raw_io, self.bone_id)
        Ms3dIo.write_byte(raw_io, self.reference_count)


###############################################################################
class Ms3dTriangle:
    """ Ms3dTriangle """
    """
    __slots__ was taking out,
    to be able to inject additional attributes during runtime
    __slots__ = (
            'flags',
            'smoothing_group',
            'group_index',
            '_vertex_indices',
            '_vertex_normals',
            '_s',
            '_t',
            )
    """

    def __init__(
            self,
            default_flags=Ms3dSpec.DEFAULT_FLAGS,
            default_vertex_indices=(0, 0, 0),
            default_vertex_normals=(
                    (0.0, 0.0, 0.0),
                    (0.0, 0.0, 0.0),
                    (0.0, 0.0, 0.0)),
            default_s=(0.0, 0.0, 0.0),
            default_t=(0.0, 0.0, 0.0),
            default_smoothing_group=Ms3dSpec.DEFAULT_TRIANGLE_SMOOTHING_GROUP,
            default_group_index=Ms3dSpec.DEFAULT_TRIANGLE_GROUP
            ):
        self.flags = default_flags
        self._vertex_indices = default_vertex_indices
        self._vertex_normals = default_vertex_normals
        self._s = default_s
        self._t = default_t
        self.smoothing_group = default_smoothing_group
        self.group_index = default_group_index

    def __repr__(self):
        return "\n<flags={}, vertex_indices={}, vertex_normals=(({:.{p}f}, "\
                "{:.{p}f}, {:.{p}f}), ({:.{p}f}, {:.{p}f}, {:.{p}f}), ({:.{p}f}, "\
                "{:.{p}f}, {:.{p}f})), s=({:.{p}f}, {:.{p}f}, {:.{p}f}), "\
                "t=({:.{p}f}, {:.{p}f}, {:.{p}f}), smoothing_group={}, "\
                "group_index={}>".format(
                self.flags,
                self.vertex_indices,
                self.vertex_normals[0][0],
                self.vertex_normals[0][1],
                self.vertex_normals[0][2],
                self.vertex_normals[1][0],
                self.vertex_normals[1][1],
                self.vertex_normals[1][2],
                self.vertex_normals[2][0],
                self.vertex_normals[2][1],
                self.vertex_normals[2][2],
                self.s[0],
                self.s[1],
                self.s[2],
                self.t[0],
                self.t[1],
                self.t[2],
                self.smoothing_group,
                self.group_index,
                p=Ms3dIo.PRECISION
                )


    @property
    def vertex_indices(self):
        return self._vertex_indices

    @property
    def vertex_normals(self):
        return self._vertex_normals

    @property
    def s(self):
        return self._s

    @property
    def t(self):
        return self._t


    def read(self, raw_io):
        self.flags = Ms3dIo.read_word(raw_io)
        self._vertex_indices = Ms3dIo.read_array(raw_io, Ms3dIo.read_word, 3)
        self._vertex_normals = Ms3dIo.read_array2(raw_io, Ms3dIo.read_float, 3, 3)
        self._s = Ms3dIo.read_array(raw_io, Ms3dIo.read_float, 3)
        self._t = Ms3dIo.read_array(raw_io, Ms3dIo.read_float, 3)
        self.smoothing_group = Ms3dIo.read_byte(raw_io)
        self.group_index = Ms3dIo.read_byte(raw_io)
        return self

    def write(self, raw_io):
        Ms3dIo.write_word(raw_io, self.flags)
        Ms3dIo.write_array(raw_io, Ms3dIo.write_word, 3, self.vertex_indices)
        Ms3dIo.write_array2(raw_io, Ms3dIo.write_float, 3, 3, self.vertex_normals)
        Ms3dIo.write_array(raw_io, Ms3dIo.write_float, 3, self.s)
        Ms3dIo.write_array(raw_io, Ms3dIo.write_float, 3, self.t)
        Ms3dIo.write_byte(raw_io, self.smoothing_group)
        Ms3dIo.write_byte(raw_io, self.group_index)


###############################################################################
class Ms3dGroup:
    """ Ms3dGroup """
    """
    __slots__ was taking out,
    to be able to inject additional attributes during runtime
    __slots__ = (
            'flags',
            'name',
            'material_index',
            '_triangle_indices',
            '_comment_object', # Ms3dComment
            )
    """

    def __init__(
            self,
            default_flags=Ms3dSpec.DEFAULT_FLAGS,
            default_name="",
            default_triangle_indices=None,
            default_material_index=Ms3dSpec.DEFAULT_GROUP_MATERIAL_INDEX,
            default_comment_object=None, # Ms3dComment
            ):
        if (default_name is None):
            default_name = ""

        if (default_triangle_indices is None):
            default_triangle_indices = []

        self.flags = default_flags
        self.name = default_name
        self._triangle_indices = default_triangle_indices
        self.material_index = default_material_index

        if default_comment_object is None:
            default_comment_object = Ms3dCommentEx()
        self._comment_object = default_comment_object # Ms3dComment

    def __repr__(self):
        return "\n<flags={}, name='{}', number_triangles={},"\
                " triangle_indices={}, material_index={}>".format(
                self.flags,
                self.name,
                self.number_triangles,
                self.triangle_indices,
                self.material_index
                )


    @property
    def number_triangles(self):
        if self.triangle_indices is None:
            return 0
        return len(self.triangle_indices)

    @property
    def triangle_indices(self):
        return self._triangle_indices

    @property
    def comment_object(self):
        return self._comment_object


    def read(self, raw_io):
        self.flags = Ms3dIo.read_byte(raw_io)
        self.name = Ms3dIo.read_string(raw_io, Ms3dIo.LENGTH_NAME)
        _number_triangles = Ms3dIo.read_word(raw_io)
        self._triangle_indices = Ms3dIo.read_array(
                raw_io, Ms3dIo.read_word, _number_triangles)
        self.material_index = Ms3dIo.read_sbyte(raw_io)
        return self

    def write(self, raw_io):
        Ms3dIo.write_byte(raw_io, self.flags)
        Ms3dIo.write_string(raw_io, Ms3dIo.LENGTH_NAME, self.name)
        Ms3dIo.write_word(raw_io, self.number_triangles)
        Ms3dIo.write_array(
                raw_io, Ms3dIo.write_word, self.number_triangles,
                self.triangle_indices)
        Ms3dIo.write_sbyte(raw_io, self.material_index)


###############################################################################
class Ms3dMaterial:
    """ Ms3dMaterial """
    """
    __slots__ was taking out,
    to be able to inject additional attributes during runtime
    __slots__ = (
            'name',
            'shininess',
            'transparency',
            'mode',
            'texture',
            'alphamap',
            '_ambient',
            '_diffuse',
            '_specular',
            '_emissive',
            '_comment_object', # Ms3dComment
            )
    """

    def __init__(
            self,
            default_name="",
            default_ambient=list(Ms3dSpec.DEFAULT_MATERIAL_AMBIENT),
            default_diffuse=list(Ms3dSpec.DEFAULT_MATERIAL_DIFFUSE),
            default_specular=list(Ms3dSpec.DEFAULT_MATERIAL_SPECULAR),
            default_emissive=list(Ms3dSpec.DEFAULT_MATERIAL_EMISSIVE),
            default_shininess=Ms3dSpec.DEFAULT_MATERIAL_SHININESS,
            default_transparency=0.0,
            default_mode=Ms3dSpec.DEFAULT_MATERIAL_MODE,
            default_texture="",
            default_alphamap="",
            default_comment_object=None, # Ms3dComment
            ):
        if (default_name is None):
            default_name = ""

        if (default_texture is None):
            default_texture = ""

        if (default_alphamap is None):
            default_alphamap = ""

        self.name = default_name
        self._ambient = default_ambient
        self._diffuse = default_diffuse
        self._specular = default_specular
        self._emissive = default_emissive
        self.shininess = default_shininess
        self.transparency = default_transparency
        self.mode = default_mode
        self.texture = default_texture
        self.alphamap = default_alphamap

        if default_comment_object is None:
            default_comment_object = Ms3dCommentEx()
        self._comment_object = default_comment_object # Ms3dComment

    def __repr__(self):
        return "\n<name='{}', ambient=({:.{p}f}, {:.{p}f}, {:.{p}f}, {:.{p}f}), "\
                "diffuse=({:.{p}f}, {:.{p}f}, {:.{p}f}, {:.{p}f}), specular=("\
                "{:.{p}f}, {:.{p}f}, {:.{p}f}, {:.{p}f}), emissive=({:.{p}f}, "\
                "{:.{p}f}, {:.{p}f}, {:.{p}f}), shininess={:.{p}f}, transparency="\
                "{:.{p}f}, mode={}, texture='{}', alphamap='{}'>".format(
                self.name,
                self.ambient[0],
                self.ambient[1],
                self.ambient[2],
                self.ambient[3],
                self.diffuse[0],
                self.diffuse[1],
                self.diffuse[2],
                self.diffuse[3],
                self.specular[0],
                self.specular[1],
                self.specular[2],
                self.specular[3],
                self.emissive[0],
                self.emissive[1],
                self.emissive[2],
                self.emissive[3],
                self.shininess,
                self.transparency,
                self.mode,
                self.texture,
                self.alphamap,
                p=Ms3dIo.PRECISION
                )

    def __hash__(self):
        return (hash(self.name)

                ^ hash(self.ambient)
                ^ hash(self.diffuse)
                ^ hash(self.specular)
                ^ hash(self.emissive)

                ^ hash(self.shininess)
                ^ hash(self.transparency)
                ^ hash(self.mode)

                ^ hash(self.texture)
                ^ hash(self.alphamap)
                )

    def __eq__(self, other):
        return ((self.name == other.name)

                and (self.ambient == other.ambient)
                and (self.diffuse == other.diffuse)
                and (self.specular == other.specular)
                and (self.emissive == other.emissive)

                and (self.shininess == other.shininess)
                and (self.transparency == other.transparency)
                and (self.mode == other.mode)

                #and (self.texture == other.texture)
                #and (self.alphamap == other.alphamap)
                )


    @property
    def ambient(self):
        return self._ambient

    @property
    def diffuse(self):
        return self._diffuse

    @property
    def specular(self):
        return self._specular

    @property
    def emissive(self):
        return self._emissive

    @property
    def comment_object(self):
        return self._comment_object


    def read(self, raw_io):
        self.name = Ms3dIo.read_string(raw_io, Ms3dIo.LENGTH_NAME)
        self._ambient = Ms3dIo.read_array(raw_io, Ms3dIo.read_float, 4)
        self._diffuse = Ms3dIo.read_array(raw_io, Ms3dIo.read_float, 4)
        self._specular = Ms3dIo.read_array(raw_io, Ms3dIo.read_float, 4)
        self._emissive = Ms3dIo.read_array(raw_io, Ms3dIo.read_float, 4)
        self.shininess = Ms3dIo.read_float(raw_io)
        self.transparency = Ms3dIo.read_float(raw_io)
        self.mode = Ms3dIo.read_byte(raw_io)
        self.texture = Ms3dIo.read_string(raw_io, Ms3dIo.LENGTH_FILENAME)
        self.alphamap = Ms3dIo.read_string(raw_io, Ms3dIo.LENGTH_FILENAME)
        return self

    def write(self, raw_io):
        Ms3dIo.write_string(raw_io, Ms3dIo.LENGTH_NAME, self.name)
        Ms3dIo.write_array(raw_io, Ms3dIo.write_float, 4, self.ambient)
        Ms3dIo.write_array(raw_io, Ms3dIo.write_float, 4, self.diffuse)
        Ms3dIo.write_array(raw_io, Ms3dIo.write_float, 4, self.specular)
        Ms3dIo.write_array(raw_io, Ms3dIo.write_float, 4, self.emissive)
        Ms3dIo.write_float(raw_io, self.shininess)
        Ms3dIo.write_float(raw_io, self.transparency)
        Ms3dIo.write_byte(raw_io, self.mode)
        Ms3dIo.write_string(raw_io, Ms3dIo.LENGTH_FILENAME, self.texture)
        Ms3dIo.write_string(raw_io, Ms3dIo.LENGTH_FILENAME, self.alphamap)


###############################################################################
class Ms3dRotationKeyframe:
    """ Ms3dRotationKeyframe """
    __slots__ = (
            'time',
            '_rotation',
            )

    def __init__(
            self,
            default_time=0.0,
            default_rotation=(0.0, 0.0, 0.0)
            ):
        self.time = default_time
        self._rotation = default_rotation

    def __repr__(self):
        return "\n<time={:.{p}f}, rotation=({:.{p}f}, {:.{p}f}, {:.{p}f})>".format(
                self.time,
                self.rotation[0],
                self.rotation[1],
                self.rotation[2],
                p=Ms3dIo.PRECISION
                )


    @property
    def rotation(self):
        return self._rotation


    def read(self, raw_io):
        self.time = Ms3dIo.read_float(raw_io)
        self._rotation = Ms3dIo.read_array(raw_io, Ms3dIo.read_float, 3)
        return self

    def write(self, raw_io):
        Ms3dIo.write_float(raw_io, self.time)
        Ms3dIo.write_array(raw_io, Ms3dIo.write_float, 3, self.rotation)


###############################################################################
class Ms3dTranslationKeyframe:
    """ Ms3dTranslationKeyframe """
    __slots__ = (
            'time',
            '_position',
            )

    def __init__(
            self,
            default_time=0.0,
            default_position=(0.0, 0.0, 0.0)
            ):
        self.time = default_time
        self._position = default_position

    def __repr__(self):
        return "\n<time={:.{p}f}, position=({:.{p}f}, {:.{p}f}, {:.{p}f})>".format(
                self.time,
                self.position[0],
                self.position[1],
                self.position[2],
                p=Ms3dIo.PRECISION
                )


    @property
    def position(self):
        return self._position


    def read(self, raw_io):
        self.time = Ms3dIo.read_float(raw_io)
        self._position = Ms3dIo.read_array(raw_io, Ms3dIo.read_float, 3)
        return self

    def write(self, raw_io):
        Ms3dIo.write_float(raw_io, self.time)
        Ms3dIo.write_array(raw_io, Ms3dIo.write_float, 3, self.position)


###############################################################################
class Ms3dJoint:
    """ Ms3dJoint """
    """
    __slots__ was taking out,
    to be able to inject additional attributes during runtime
    __slots__ = (
            'flags',
            'name',
            'parent_name',
            '_rotation',
            '_position',
            '_rotation_keyframes',
            '_translation_keyframes',
            '_joint_ex_object', # Ms3dJointEx
            '_comment_object', # Ms3dComment
            )
    """

    def __init__(
            self,
            default_flags=Ms3dSpec.DEFAULT_FLAGS,
            default_name="",
            default_parent_name="",
            default_rotation=(0.0, 0.0, 0.0),
            default_position=(0.0, 0.0, 0.0),
            default_rotation_keyframes=None,
            default_translation_keyframes=None,
            default_joint_ex_object=None, # Ms3dJointEx
            default_comment_object=None, # Ms3dComment
            ):
        if (default_name is None):
            default_name = ""

        if (default_parent_name is None):
            default_parent_name = ""

        if (default_rotation_keyframes is None):
            default_rotation_keyframes = [] #Ms3dRotationKeyframe()

        if (default_translation_keyframes is None):
            default_translation_keyframes = [] #Ms3dTranslationKeyframe()

        self.flags = default_flags
        self.name = default_name
        self.parent_name = default_parent_name
        self._rotation = default_rotation
        self._position = default_position
        self._rotation_keyframes = default_rotation_keyframes
        self._translation_keyframes = default_translation_keyframes

        if default_comment_object is None:
            default_comment_object = Ms3dCommentEx()
        self._comment_object = default_comment_object # Ms3dComment

        if default_joint_ex_object is None:
            default_joint_ex_object = Ms3dJointEx()
        self._joint_ex_object = default_joint_ex_object # Ms3dJointEx

    def __repr__(self):
        return "\n<flags={}, name='{}', parent_name='{}', rotation=({:.{p}f}, "\
                "{:.{p}f}, {:.{p}f}), position=({:.{p}f}, {:.{p}f}, {:.{p}f}), "\
                "number_rotation_keyframes={}, number_translation_keyframes={},"\
                " rotation_key_frames={}, translation_key_frames={}>".format(
                self.flags,
                self.name,
                self.parent_name,
                self.rotation[0],
                self.rotation[1],
                self.rotation[2],
                self.position[0],
                self.position[1],
                self.position[2],
                self.number_rotation_keyframes,
                self.number_translation_keyframes,
                self.rotation_key_frames,
                self.translation_key_frames,
                p=Ms3dIo.PRECISION
                )


    @property
    def rotation(self):
        return self._rotation

    @property
    def position(self):
        return self._position

    @property
    def number_rotation_keyframes(self):
        if self.rotation_key_frames is None:
            return 0
        return len(self.rotation_key_frames)

    @property
    def number_translation_keyframes(self):
        if self.translation_key_frames is None:
            return 0
        return len(self.translation_key_frames)

    @property
    def rotation_key_frames(self):
        return self._rotation_keyframes

    @property
    def translation_key_frames(self):
        return self._translation_keyframes


    @property
    def joint_ex_object(self):
        return self._joint_ex_object


    @property
    def comment_object(self):
        return self._comment_object


    def read(self, raw_io):
        self.flags = Ms3dIo.read_byte(raw_io)
        self.name = Ms3dIo.read_string(raw_io, Ms3dIo.LENGTH_NAME)
        self.parent_name = Ms3dIo.read_string(raw_io, Ms3dIo.LENGTH_NAME)
        self._rotation = Ms3dIo.read_array(raw_io, Ms3dIo.read_float, 3)
        self._position = Ms3dIo.read_array(raw_io, Ms3dIo.read_float, 3)
        _number_rotation_keyframes = Ms3dIo.read_word(raw_io)
        _number_translation_keyframes = Ms3dIo.read_word(raw_io)
        self._rotation_keyframes = []
        for i in range(_number_rotation_keyframes):
            self.rotation_key_frames.append(Ms3dRotationKeyframe().read(raw_io))
        self._translation_keyframes = []
        for i in range(_number_translation_keyframes):
            self.translation_key_frames.append(
                    Ms3dTranslationKeyframe().read(raw_io))
        return self

    def write(self, raw_io):
        Ms3dIo.write_byte(raw_io, self.flags)
        Ms3dIo.write_string(raw_io, Ms3dIo.LENGTH_NAME, self.name)
        Ms3dIo.write_string(raw_io, Ms3dIo.LENGTH_NAME, self.parent_name)
        Ms3dIo.write_array(raw_io, Ms3dIo.write_float, 3, self.rotation)
        Ms3dIo.write_array(raw_io, Ms3dIo.write_float, 3, self.position)
        Ms3dIo.write_word(raw_io, self.number_rotation_keyframes)
        Ms3dIo.write_word(raw_io, self.number_translation_keyframes)
        for i in range(self.number_rotation_keyframes):
            self.rotation_key_frames[i].write(raw_io)
        for i in range(self.number_translation_keyframes):
            self.translation_key_frames[i].write(raw_io)


###############################################################################
class Ms3dCommentEx:
    """ Ms3dCommentEx """
    __slots__ = (
            'index',
            'comment',
            )

    def __init__(
            self,
            default_index=0,
            default_comment=""
            ):
        if (default_comment is None):
            default_comment = ""

        self.index = default_index
        self.comment = default_comment

    def __repr__(self):
        return "\n<index={}, comment_length={}, comment='{}'>".format(
                self.index,
                self.comment_length,
                self.comment
                )


    @property
    def comment_length(self):
        if self.comment is None:
            return 0
        return len(self.comment)


    def read(self, raw_io):
        self.index = Ms3dIo.read_dword(raw_io)
        _comment_length = Ms3dIo.read_dword(raw_io)
        self.comment = Ms3dIo.read_string(raw_io, _comment_length)
        return self

    def write(self, raw_io):
        Ms3dIo.write_dword(raw_io, self.index)
        Ms3dIo.write_dword(raw_io, self.comment_length)
        Ms3dIo.write_string(raw_io, self.comment_length, self.comment)


###############################################################################
class Ms3dComment:
    """ Ms3dComment """
    __slots__ = (
            'comment',
            )

    def __init__(
            self,
            default_comment=""
            ):
        if (default_comment is None):
            default_comment = ""

        self.comment = default_comment

    def __repr__(self):
        return "\n<comment_length={}, comment='{}'>".format(
                self.comment_length,
                self.comment
                )


    @property
    def comment_length(self):
        if self.comment is None:
            return 0
        return len(self.comment)


    def read(self, raw_io):
        _comment_length = Ms3dIo.read_dword(raw_io)
        self.comment = Ms3dIo.read_string(raw_io, _comment_length)
        return self

    def write(self, raw_io):
        Ms3dIo.write_dword(raw_io, self.comment_length)
        Ms3dIo.write_string(raw_io, self.comment_length, self.comment)


###############################################################################
class Ms3dVertexEx1:
    """ Ms3dVertexEx1 """
    __slots__ = (
            '_bone_ids',
            '_weights',
            )

    def __init__(
            self,
            default_bone_ids=(
                    Ms3dSpec.DEFAULT_VERTEX_BONE_ID,
                    Ms3dSpec.DEFAULT_VERTEX_BONE_ID,
                    Ms3dSpec.DEFAULT_VERTEX_BONE_ID),
            default_weights=(100, 0, 0)
            ):
        self._bone_ids = default_bone_ids
        self._weights = default_weights

    def __repr__(self):
        return "\n<bone_ids={}, weights={}>".format(
                self.bone_ids,
                self.weights
                )


    @property
    def bone_ids(self):
        return self._bone_ids

    @property
    def weights(self):
        return self._weights


    @property
    def weight_bone_id(self):
        if self._weights[0] or self._weights[1] or self._weights[2]:
            return self._weights
        return 100

    @property
    def weight_bone_id0(self):
        if self._weights[0] or self._weights[1] or self._weights[2]:
            return self._weights[0]
        return 0

    @property
    def weight_bone_id1(self):
        if self._weights[0] or self._weights[1] or self._weights[2]:
            return self._weights[1]
        return 0

    @property
    def weight_bone_id2(self):
        if self._weights[0] or self._weights[1] or self._weights[2]:
            return 100 - (self._weights[0] + self._weights[1] \
                    + self._weights[2])
        return 0


    def read(self, raw_io):
        self._bone_ids = Ms3dIo.read_array(raw_io, Ms3dIo.read_sbyte, 3)
        self._weights = Ms3dIo.read_array(raw_io, Ms3dIo.read_byte, 3)
        return self

    def write(self, raw_io):
        Ms3dIo.write_array(raw_io, Ms3dIo.write_sbyte, 3, self.bone_ids)
        Ms3dIo.write_array(raw_io, Ms3dIo.write_byte, 3, self.weights)


###############################################################################
class Ms3dVertexEx2:
    """ Ms3dVertexEx2 """
    __slots__ = (
            'extra',
            '_bone_ids',
            '_weights',
            )

    def __init__(
            self,
            default_bone_ids=(
                    Ms3dSpec.DEFAULT_VERTEX_BONE_ID,
                    Ms3dSpec.DEFAULT_VERTEX_BONE_ID,
                    Ms3dSpec.DEFAULT_VERTEX_BONE_ID),
            default_weights=(100, 0, 0),
            default_extra=0
            ):
        self._bone_ids = default_bone_ids
        self._weights = default_weights
        self.extra = default_extra

    def __repr__(self):
        return "\n<bone_ids={}, weights={}, extra={}>".format(
                self.bone_ids,
                self.weights,
                self.extra
                )


    @property
    def bone_ids(self):
        return self._bone_ids

    @property
    def weights(self):
        return self._weights


    @property
    def weight_bone_id(self):
        if self._weights[0] or self._weights[1] or self._weights[2]:
            return self._weights
        return 100

    @property
    def weight_bone_id0(self):
        if self._weights[0] or self._weights[1] or self._weights[2]:
            return self._weights[0]
        return 0

    @property
    def weight_bone_id1(self):
        if self._weights[0] or self._weights[1] or self._weights[2]:
            return self._weights[1]
        return 0

    @property
    def weight_bone_id2(self):
        if self._weights[0] or self._weights[1] or self._weights[2]:
            return 100 - (self._weights[0] + self._weights[1] \
                    + self._weights[2])
        return 0


    def read(self, raw_io):
        self._bone_ids = Ms3dIo.read_array(raw_io, Ms3dIo.read_sbyte, 3)
        self._weights = Ms3dIo.read_array(raw_io, Ms3dIo.read_byte, 3)
        self.extra = Ms3dIo.read_dword(raw_io)
        return self

    def write(self, raw_io):
        Ms3dIo.write_array(raw_io, Ms3dIo.write_sbyte, 3, self.bone_ids)
        Ms3dIo.write_array(raw_io, Ms3dIo.write_byte, 3, self.weights)
        Ms3dIo.write_dword(raw_io, self.extra)


###############################################################################
class Ms3dVertexEx3:
    """ Ms3dVertexEx3 """
    #char bone_ids[3]; // index of joint or -1, if -1, then that weight is
    #    ignored, since subVersion 1
    #byte weights[3]; // vertex weight ranging from 0 - 100, last weight is
    #    computed by 1.0 - sum(all weights), since subVersion 1
    #// weight[0] is the weight for bone_id in Ms3dVertex
    #// weight[1] is the weight for bone_ids[0]
    #// weight[2] is the weight for bone_ids[1]
    #// 1.0f - weight[0] - weight[1] - weight[2] is the weight for bone_ids[2]
    #unsigned int extra; // vertex extra, which can be used as color or
    #    anything else, since subVersion 2
    __slots__ = (
            'extra',
            '_bone_ids',
            '_weights',
            )

    def __init__(
            self,
            default_bone_ids=(
                    Ms3dSpec.DEFAULT_VERTEX_BONE_ID,
                    Ms3dSpec.DEFAULT_VERTEX_BONE_ID,
                    Ms3dSpec.DEFAULT_VERTEX_BONE_ID),
            default_weights=(100, 0, 0),
            default_extra=0
            ):
        self._bone_ids = default_bone_ids
        self._weights = default_weights
        self.extra = default_extra

    def __repr__(self):
        return "\n<bone_ids={}, weights={}, extra={}>".format(
                self.bone_ids,
                self.weights,
                self.extra
                )


    @property
    def bone_ids(self):
        return self._bone_ids

    @property
    def weights(self):
        return self._weights


    @property
    def weight_bone_id(self):
        if self._weights[0] or self._weights[1] or self._weights[2]:
            return self._weights
        return 100

    @property
    def weight_bone_id0(self):
        if self._weights[0] or self._weights[1] or self._weights[2]:
            return self._weights[0]
        return 0

    @property
    def weight_bone_id1(self):
        if self._weights[0] or self._weights[1] or self._weights[2]:
            return self._weights[1]
        return 0

    @property
    def weight_bone_id2(self):
        if self._weights[0] or self._weights[1] or self._weights[2]:
            return 100 - (self._weights[0] + self._weights[1] \
                    + self._weights[2])
        return 0


    def read(self, raw_io):
        self._bone_ids = Ms3dIo.read_array(raw_io, Ms3dIo.read_sbyte, 3)
        self._weights = Ms3dIo.read_array(raw_io, Ms3dIo.read_byte, 3)
        self.extra = Ms3dIo.read_dword(raw_io)
        return self

    def write(self, raw_io):
        Ms3dIo.write_array(raw_io, Ms3dIo.write_sbyte, 3, self.bone_ids)
        Ms3dIo.write_array(raw_io, Ms3dIo.write_byte, 3, self.weights)
        Ms3dIo.write_dword(raw_io, self.extra)


###############################################################################
class Ms3dJointEx:
    """ Ms3dJointEx """
    __slots__ = (
            '_color',
            )

    def __init__(
            self,
            default_color=Ms3dSpec.DEFAULT_JOINT_COLOR
            ):
        self._color = default_color

    def __repr__(self):
        return "\n<color=({:.{p}f}, {:.{p}f}, {:.{p}f})>".format(
                self.color[0],
                self.color[1],
                self.color[2],
                p=Ms3dIo.PRECISION
                )


    @property
    def color(self):
        return self._color


    def read(self, raw_io):
        self._color = Ms3dIo.read_array(raw_io, Ms3dIo.read_float, 3)
        return self

    def write(self, raw_io):
        Ms3dIo.write_array(raw_io, Ms3dIo.write_float, 3, self.color)


###############################################################################
class Ms3dModelEx:
    """ Ms3dModelEx """
    __slots__ = (
            'joint_size',
            'transparency_mode',
            'alpha_ref',
            )

    def __init__(
            self,
            default_joint_size=Ms3dSpec.DEFAULT_MODEL_JOINT_SIZE,
            default_transparency_mode\
                    =Ms3dSpec.DEFAULT_MODEL_TRANSPARENCY_MODE,
            default_alpha_ref=0.0
            ):
        self.joint_size = default_joint_size
        self.transparency_mode = default_transparency_mode
        self.alpha_ref = default_alpha_ref

    def __repr__(self):
        return "\n<joint_size={:.{p}f}, transparency_mode={}, alpha_ref={:.{p}f}>".format(
                self.joint_size,
                self.transparency_mode,
                self.alpha_ref,
                p=Ms3dIo.PRECISION
                )

    def read(self, raw_io):
        self.joint_size = Ms3dIo.read_float(raw_io)
        self.transparency_mode = Ms3dIo.read_dword(raw_io)
        self.alpha_ref = Ms3dIo.read_float(raw_io)
        return self

    def write(self, raw_io):
        Ms3dIo.write_float(raw_io, self.joint_size)
        Ms3dIo.write_dword(raw_io, self.transparency_mode)
        Ms3dIo.write_float(raw_io, self.alpha_ref)


###############################################################################
#
# file format
#
###############################################################################
class Ms3dModel:
    """ Ms3dModel """
    __slot__ = (
            'header',
            'animation_fps',
            'current_time',
            'number_total_frames',
            'sub_version_comments',
            'sub_version_vertex_extra',
            'sub_version_joint_extra',
            'sub_version_model_extra',
            'name',
            '_vertices',
            '_triangles',
            '_groups',
            '_materials',
            '_joints',
            '_has_model_comment',
            '_comment_object', # Ms3dComment
            '_model_ex_object', # Ms3dModelEx
            )

    def __init__(
            self,
            default_name=""
            ):
        if (default_name is None):
            default_name = ""

        self.name = default_name

        self.animation_fps = Ms3dSpec.DEFAULT_MODEL_ANIMATION_FPS
        self.current_time = 0.0
        self.number_total_frames = 0
        self.sub_version_comments \
                = Ms3dSpec.DEFAULT_MODEL_SUB_VERSION_COMMENTS
        self.sub_version_vertex_extra \
                = Ms3dSpec.DEFAULT_MODEL_SUB_VERSION_VERTEX_EXTRA
        self.sub_version_joint_extra \
                = Ms3dSpec.DEFAULT_MODEL_SUB_VERSION_JOINT_EXTRA
        self.sub_version_model_extra \
                = Ms3dSpec.DEFAULT_MODEL_SUB_VERSION_MODEL_EXTRA

        self._vertices = [] #Ms3dVertex()
        self._triangles = [] #Ms3dTriangle()
        self._groups = [] #Ms3dGroup()
        self._materials = [] #Ms3dMaterial()
        self._joints = [] #Ms3dJoint()

        self.header = Ms3dHeader()
        self._model_ex_object = Ms3dModelEx()
        self._comment_object = None #Ms3dComment()


    @property
    def number_vertices(self):
        if self.vertices is None:
            return 0
        return len(self.vertices)

    @property
    def vertices(self):
        return self._vertices


    @property
    def number_triangles(self):
        if self.triangles is None:
            return 0
        return len(self.triangles)

    @property
    def triangles(self):
        return self._triangles


    @property
    def number_groups(self):
        if self.groups is None:
            return 0
        return len(self.groups)

    @property
    def groups(self):
        return self._groups


    @property
    def number_materials(self):
        if self.materials is None:
            return 0
        return len(self.materials)

    @property
    def materials(self):
        return self._materials


    @property
    def number_joints(self):
        if self.joints is None:
            return 0
        return len(self.joints)

    @property
    def joints(self):
        return self._joints


    @property
    def number_group_comments(self):
        if self.groups is None:
            return 0
        number = 0
        for item in self.groups:
            if item.comment_object is not None and item.comment_object.comment:
                number += 1
        return number

    @property
    def group_comments(self):
        if self.groups is None:
            return None
        items = []
        for item in self.groups:
            if item.comment_object is not None and item.comment_object.comment:
                items.append(item)
        return items


    @property
    def number_material_comments(self):
        if self.materials is None:
            return 0
        number = 0
        for item in self.materials:
            if item.comment_object is not None and item.comment_object.comment:
                number += 1
        return number

    @property
    def material_comments(self):
        if self.materials is None:
            return None
        items = []
        for item in self.materials:
            if item.comment_object is not None and item.comment_object.comment:
                items.append(item)
        return items


    @property
    def number_joint_comments(self):
        if self.joints is None:
            return 0
        number = 0
        for item in self.joints:
            if item.comment_object is not None and item.comment_object.comment:
                number += 1
        return number

    @property
    def joint_comments(self):
        if self.joints is None:
            return None
        items = []
        for item in self.joints:
            if item.comment_object is not None and item.comment_object.comment:
                items.append(item)
        return items


    @property
    def has_model_comment(self):
        if self.comment_object is not None and self.comment_object.comment:
            return 1
        return 0

    @property
    def comment_object(self):
        return self._comment_object


    @property
    def vertex_ex(self):
        if not self.sub_version_vertex_extra:
            return None
        return [item.vertex_ex_object for item in self.vertices]

    @property
    def joint_ex(self):
        if not self.sub_version_joint_extra:
            return None
        return [item.joint_ex_object for item in self.joints]

    @property
    def model_ex_object(self):
        if not self.sub_version_model_extra:
            return None
        return self._model_ex_object


    def print_internal(self):
        print()
        print("##############################################################")
        print("## the internal data of Ms3dModel object...")
        print("##")

        print("header={}".format(self.header))

        print("number_vertices={}".format(self.number_vertices))
        print("vertices=[", end="")
        if self.vertices:
            for obj in self.vertices:
                print("{}".format(obj), end="")
        print("]")

        print("number_triangles={}".format(self.number_triangles))
        print("triangles=[", end="")
        if self.triangles:
            for obj in self.triangles:
                print("{}".format(obj), end="")
        print("]")

        print("number_groups={}".format(self.number_groups))
        print("groups=[", end="")
        if self.groups:
            for obj in self.groups:
                print("{}".format(obj), end="")
        print("]")

        print("number_materials={}".format(self.number_materials))
        print("materials=[", end="")
        if self.materials:
            for obj in self.materials:
                print("{}".format(obj), end="")
        print("]")

        print("animation_fps={}".format(self.animation_fps))
        print("current_time={}".format(self.current_time))
        print("number_total_frames={}".format(self.number_total_frames))

        print("number_joints={}".format(self.number_joints))
        print("joints=[", end="")
        if self.joints:
            for obj in self.joints:
                print("{}".format(obj), end="")
        print("]")

        print("sub_version_comments={}".format(self.sub_version_comments))

        print("number_group_comments={}".format(self.number_group_comments))
        print("group_comments=[", end="")
        if self.group_comments:
            for obj in self.group_comments:
                print("{}".format(obj.comment_object), end="")
        print("]")

        print("number_material_comments={}".format(
                self.number_material_comments))
        print("material_comments=[", end="")
        if self.material_comments:
            for obj in self.material_comments:
                print("{}".format(obj.comment_object), end="")
        print("]")

        print("number_joint_comments={}".format(self.number_joint_comments))
        print("joint_comments=[", end="")
        if self.joint_comments:
            for obj in self.joint_comments:
                print("{}".format(obj.comment_object), end="")
        print("]")

        print("has_model_comment={}".format(self.has_model_comment))
        print("model_comment={}".format(self.comment_object))

        print("sub_version_vertex_extra={}".format(
                self.sub_version_vertex_extra))
        print("vertex_ex=[", end="")
        if self.vertex_ex:
            for obj in self.vertex_ex:
                print("{}".format(obj), end="")
        print("]")

        print("sub_version_joint_extra={}".format(
                self.sub_version_joint_extra))
        print("joint_ex=[", end="")
        if self.joint_ex:
            for obj in self.joint_ex:
                print("{}".format(obj), end="")
        print("]")

        print("sub_version_model_extra={}".format(
                self.sub_version_model_extra))
        print("model_ex={}".format(self.model_ex_object))

        print("##")
        print("## ...end")
        print("##############################################################")
        print()


    def read(self, raw_io):
        """
        opens, reads and pars MS3D file.
        add content to blender scene
        """

        debug_out = []

        self.header.read(raw_io)
        if (self.header != Ms3dHeader()):
            debug_out.append("\nwarning, invalid file header\n")
            raise Ms3dHeader.HeaderError

        _number_vertices = Ms3dIo.read_word(raw_io)
        if (_number_vertices > Ms3dSpec.MAX_VERTICES):
            debug_out.append("\nwarning, invalid count: number_vertices: {}\n".format(
                    _number_vertices))
        self._vertices = []
        for i in range(_number_vertices):
            self.vertices.append(Ms3dVertex().read(raw_io))

        _number_triangles = Ms3dIo.read_word(raw_io)
        if (_number_triangles > Ms3dSpec.MAX_TRIANGLES):
            debug_out.append("\nwarning, invalid count: number_triangles: {}\n".format(
                    _number_triangles))
        self._triangles = []
        for i in range(_number_triangles):
            self.triangles.append(Ms3dTriangle().read(raw_io))

        _number_groups = Ms3dIo.read_word(raw_io)
        if (_number_groups > Ms3dSpec.MAX_GROUPS):
            debug_out.append("\nwarning, invalid count: number_groups: {}\n".format(
                    _number_groups))
        self._groups = []
        for i in range(_number_groups):
            self.groups.append(Ms3dGroup().read(raw_io))

        _number_materials = Ms3dIo.read_word(raw_io)
        if (_number_materials > Ms3dSpec.MAX_MATERIALS):
            debug_out.append("\nwarning, invalid count: number_materials: {}\n".format(
                    _number_materials))
        self._materials = []
        for i in range(_number_materials):
            self.materials.append(Ms3dMaterial().read(raw_io))

        self.animation_fps = Ms3dIo.read_float(raw_io)
        self.current_time = Ms3dIo.read_float(raw_io)
        self.number_total_frames = Ms3dIo.read_dword(raw_io)

        _progress = set()

        try:
            # optional data
            # doesn't matter if doesn't existing.

            _number_joints = Ms3dIo.read_word(raw_io)
            _progress.add('NUMBER_JOINTS')
            if (_number_joints > Ms3dSpec.MAX_JOINTS):
                debug_out.append("\nwarning, invalid count: number_joints: {}\n".format(
                        _number_joints))
            self._joints = []
            for i in range(_number_joints):
                self.joints.append(Ms3dJoint().read(raw_io))
            _progress.add('JOINTS')

            self.sub_version_comments = Ms3dIo.read_dword(raw_io)
            _progress.add('SUB_VERSION_COMMENTS')
            _number_group_comments = Ms3dIo.read_dword(raw_io)
            _progress.add('NUMBER_GROUP_COMMENTS')
            if (_number_group_comments > Ms3dSpec.MAX_GROUPS):
                debug_out.append("\nwarning, invalid count:"\
                        " number_group_comments: {}\n".format(
                        _number_group_comments))
            if _number_group_comments > _number_groups:
                debug_out.append("\nwarning, invalid count:"\
                        " number_group_comments: {}, number_groups: {}\n".format(
                        _number_group_comments, _number_groups))
            for i in range(_number_group_comments):
                item = Ms3dCommentEx().read(raw_io)
                if item.index >= 0 and item.index < _number_groups:
                    self.groups[item.index]._comment_object = item
                else:
                    debug_out.append("\nwarning, invalid index:"\
                            " group_index: {}, number_groups: {}\n".format(
                            item.index, _number_groups))
            _progress.add('GROUP_COMMENTS')

            _number_material_comments = Ms3dIo.read_dword(raw_io)
            _progress.add('NUMBER_MATERIAL_COMMENTS')
            if (_number_material_comments > Ms3dSpec.MAX_MATERIALS):
                debug_out.append("\nwarning, invalid count:"\
                        " number_material_comments: {}\n".format(
                        _number_material_comments))
            if _number_material_comments > _number_materials:
                debug_out.append("\nwarning, invalid count:"\
                        " number_material_comments:"\
                        " {}, number_materials: {}\n".format(
                        _number_material_comments, _number_materials))
            for i in range(_number_material_comments):
                item = Ms3dCommentEx().read(raw_io)
                if item.index >= 0 and item.index < _number_materials:
                    self.materials[item.index]._comment_object = item
                else:
                    debug_out.append("\nwarning, invalid index:"\
                            " material_index: {}, number_materials:"\
                            " {}\n".format(item.index, _number_materials))
            _progress.add('MATERIAL_COMMENTS')

            _number_joint_comments = Ms3dIo.read_dword(raw_io)
            _progress.add('NUMBER_JOINT_COMMENTS')
            if (_number_joint_comments > Ms3dSpec.MAX_JOINTS):
                debug_out.append("\nwarning, invalid count:"\
                        " number_joint_comments: {}\n".format(
                        _number_joint_comments))
            if _number_joint_comments > _number_joints:
                debug_out.append("\nwarning, invalid count:"\
                        " number_joint_comments: {}, number_joints: {}\n".format(
                        _number_joint_comments, _number_joints))
            for i in range(_number_joint_comments):
                item = Ms3dCommentEx().read(raw_io)
                if item.index >= 0 and item.index < _number_joints:
                    self.joints[item.index]._comment_object = item
                else:
                    debug_out.append("\nwarning, invalid index:"\
                            " joint_index: {}, number_joints: {}\n".format(
                            item.index, _number_joints))
            _progress.add('JOINT_COMMENTS')

            _has_model_comment = Ms3dIo.read_dword(raw_io)
            _progress.add('HAS_MODEL_COMMENTS')
            if (_has_model_comment != 0):
                self._comment_object = Ms3dComment().read(raw_io)
            else:
                self._comment_object = None
            _progress.add('MODEL_COMMENTS')

            self.sub_version_vertex_extra = Ms3dIo.read_dword(raw_io)
            _progress.add('SUB_VERSION_VERTEX_EXTRA')
            if self.sub_version_vertex_extra > 0:
                length = len(self.joints)
                for i in range(_number_vertices):
                    if self.sub_version_vertex_extra == 1:
                        item = Ms3dVertexEx1()
                    elif self.sub_version_vertex_extra == 2:
                        item = Ms3dVertexEx2()
                    elif self.sub_version_vertex_extra == 3:
                        item = Ms3dVertexEx3()
                    else:
                        debug_out.append("\nwarning, invalid version:"\
                                " sub_version_vertex_extra: {}\n".format(
                                sub_version_vertex_extra))
                        continue
                    self.vertices[i]._vertex_ex_object = item.read(raw_io)
            _progress.add('VERTEX_EXTRA')

            self.sub_version_joint_extra = Ms3dIo.read_dword(raw_io)
            _progress.add('SUB_VERSION_JOINT_EXTRA')
            if self.sub_version_joint_extra > 0:
                for i in range(_number_joints):
                    self.joints[i]._joint_ex_object = Ms3dJointEx().read(raw_io)
            _progress.add('JOINT_EXTRA')

            self.sub_version_model_extra = Ms3dIo.read_dword(raw_io)
            _progress.add('SUB_VERSION_MODEL_EXTRA')
            if self.sub_version_model_extra > 0:
                self._model_ex_object.read(raw_io)
            _progress.add('MODEL_EXTRA')

        except EOFError:
            # reached end of optional data.
            debug_out.append("Ms3dModel.read - optional data read: {}\n".format(_progress))
            pass

        except Exception:
            type, value, traceback = exc_info()
            debug_out.append("Ms3dModel.read - exception in optional try block,"
                    " _progress={0}\n  type: '{1}'\n  value: '{2}'\n".format(
                    _progress, type, value, traceback))

        else:
            pass

        # try best to continue far as possible
        if not 'JOINTS' in _progress:
            _number_joints = 0
            self._joints = []

        if not 'GROUP_COMMENTS' in _progress:
            self.sub_version_comments = 0
            _number_group_comments = 0

        if not 'MATERIAL_COMMENTS' in _progress:
            _number_material_comments = 0

        if not 'JOINT_COMMENTS' in _progress:
            _number_joint_comments = 0

        if not 'MODEL_COMMENTS' in _progress:
            _has_model_comment = 0
            self._comment_object = None # Ms3dComment()

        if not 'VERTEX_EXTRA' in _progress:
            self.sub_version_vertex_extra = 0

        if not 'JOINT_EXTRA' in _progress:
            self.sub_version_joint_extra = 0

        if not 'MODEL_EXTRA' in _progress:
            self.sub_version_model_extra = 0
            self._model_ex_object = Ms3dModelEx()

        return "".join(debug_out)


    def write(self, raw_io):
        """
        add blender scene content to MS3D
        creates, writes MS3D file.
        """

        debug_out = []

        self.header.write(raw_io)

        Ms3dIo.write_word(raw_io, self.number_vertices)
        for i in range(self.number_vertices):
            self.vertices[i].write(raw_io)

        Ms3dIo.write_word(raw_io, self.number_triangles)
        for i in range(self.number_triangles):
            self.triangles[i].write(raw_io)

        Ms3dIo.write_word(raw_io, self.number_groups)
        for i in range(self.number_groups):
            self.groups[i].write(raw_io)

        Ms3dIo.write_word(raw_io, self.number_materials)
        for i in range(self.number_materials):
            self.materials[i].write(raw_io)

        Ms3dIo.write_float(raw_io, self.animation_fps)
        Ms3dIo.write_float(raw_io, self.current_time)
        Ms3dIo.write_dword(raw_io, self.number_total_frames)

        try:
            # optional part
            # doesn't matter if it doesn't complete.
            Ms3dIo.write_word(raw_io, self.number_joints)
            for i in range(self.number_joints):
                self.joints[i].write(raw_io)

            Ms3dIo.write_dword(raw_io, self.sub_version_comments)

            Ms3dIo.write_dword(raw_io, self.number_group_comments)
            for i in range(self.number_group_comments):
                self.group_comments[i].comment_object.write(raw_io)

            Ms3dIo.write_dword(raw_io, self.number_material_comments)
            for i in range(self.number_material_comments):
                self.material_comments[i].comment_object.write(raw_io)

            Ms3dIo.write_dword(raw_io, self.number_joint_comments)
            for i in range(self.number_joint_comments):
                self.joint_comments[i].comment_object.write(raw_io)

            Ms3dIo.write_dword(raw_io, self.has_model_comment)
            if (self.has_model_comment != 0):
                self.comment_object.write(raw_io)

            Ms3dIo.write_dword(raw_io, self.sub_version_vertex_extra)
            if (self.sub_version_vertex_extra in {1, 2, 3}):
                for i in range(self.number_vertices):
                    self.vertex_ex[i].write(raw_io)

            Ms3dIo.write_dword(raw_io, self.sub_version_joint_extra)
            for i in range(self.number_joints):
                self.joint_ex[i].write(raw_io)

            Ms3dIo.write_dword(raw_io, self.sub_version_model_extra)
            self.model_ex_object.write(raw_io)

        except Exception:
            type, value, traceback = exc_info()
            debug_out.append("Ms3dModel.write - exception in optional try block"
                    "\n  type: '{0}'\n  value: '{1}'\n".format(
                    type, value, traceback))
            pass

        else:
            pass

        return "".join(debug_out)


    def is_valid(self):
        valid = True
        result = []

        format1 = "\n  number of {0}: {1}"
        format2 = " limit exceeded! (limit is {0})"

        result.append("MS3D statistics:")
        result.append(format1.format("vertices ........",
                self.number_vertices))
        if (self.number_vertices > Ms3dSpec.MAX_VERTICES):
            result.append(format2.format(Ms3dSpec.MAX_VERTICES))
            valid &= False

        result.append(format1.format("triangles .......",
                self.number_triangles))
        if (self.number_triangles > Ms3dSpec.MAX_TRIANGLES):
            result.append(format2.format(Ms3dSpec.MAX_TRIANGLES))
            valid &= False

        result.append(format1.format("groups ..........",
                self.number_groups))
        if (self.number_groups > Ms3dSpec.MAX_GROUPS):
            result.append(format2.format(Ms3dSpec.MAX_GROUPS))
            valid &= False

        result.append(format1.format("materials .......",
                self.number_materials))
        if (self.number_materials > Ms3dSpec.MAX_MATERIALS):
            result.append(format2.format(Ms3dSpec.MAX_MATERIALS))
            valid &= False

        result.append(format1.format("joints ..........",
                self.number_joints))
        if (self.number_joints > Ms3dSpec.MAX_JOINTS):
            result.append(format2.format(Ms3dSpec.MAX_JOINTS))
            valid &= False

        result.append(format1.format("model comments ..",
                self.has_model_comment))
        result.append(format1.format("group comments ..",
                self.number_group_comments))
        result.append(format1.format("material comments",
                self.number_material_comments))
        result.append(format1.format("joint comments ..",
                self.number_joint_comments))

        #if (not valid):
        #    result.append("\n\nthe data may be corrupted.")

        return (valid, ("".join(result)))


###############################################################################
#234567890123456789012345678901234567890123456789012345678901234567890123456789
#--------1---------2---------3---------4---------5---------6---------7---------
# ##### END OF FILE #####