1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
21 # Script copyright (C) 2006-2012, assimp team
22 # Script copyright (C) 2013 Blender Foundation
31 from struct
import unpack
35 from . import data_types
37 # at the end of each nested block, there is a NUL record to indicate
38 # that the sub-scope exists (i.e. to distinguish between P: and P : {})
39 # this NUL record is 13 bytes long.
40 _BLOCK_SENTINEL_LENGTH
= 13
41 _BLOCK_SENTINEL_DATA
= (b
'\0' * _BLOCK_SENTINEL_LENGTH
)
42 _IS_BIG_ENDIAN
= (__import__("sys").byteorder
!= 'little')
43 _HEAD_MAGIC
= b
'Kaydara FBX Binary\x20\x20\x00\x1a\x00'
44 from collections
import namedtuple
45 FBXElem
= namedtuple("FBXElem", ("id", "props", "props_type", "elems"))
50 return unpack(b
'<I', read(4))[0]
54 return unpack(b
'B', read(1))[0]
57 def read_string_ubyte(read
):
58 size
= read_ubyte(read
)
63 def unpack_array(read
, array_type
, array_stride
, array_byteswap
):
64 length
= read_uint(read
)
65 encoding
= read_uint(read
)
66 comp_len
= read_uint(read
)
73 data
= zlib
.decompress(data
)
75 assert(length
* array_stride
== len(data
))
77 data_array
= array
.array(array_type
, data
)
78 if array_byteswap
and _IS_BIG_ENDIAN
:
84 b
'Y'[0]: lambda read
: unpack(b
'<h', read(2))[0], # 16 bit int
85 b
'C'[0]: lambda read
: unpack(b
'?', read(1))[0], # 1 bit bool (yes/no)
86 b
'I'[0]: lambda read
: unpack(b
'<i', read(4))[0], # 32 bit int
87 b
'F'[0]: lambda read
: unpack(b
'<f', read(4))[0], # 32 bit float
88 b
'D'[0]: lambda read
: unpack(b
'<d', read(8))[0], # 64 bit float
89 b
'L'[0]: lambda read
: unpack(b
'<q', read(8))[0], # 64 bit int
90 b
'R'[0]: lambda read
: read(read_uint(read
)), # binary data
91 b
'S'[0]: lambda read
: read(read_uint(read
)), # string data
92 b
'f'[0]: lambda read
: unpack_array(read
, data_types
.ARRAY_FLOAT32
, 4, False), # array (float)
93 b
'i'[0]: lambda read
: unpack_array(read
, data_types
.ARRAY_INT32
, 4, True), # array (int)
94 b
'd'[0]: lambda read
: unpack_array(read
, data_types
.ARRAY_FLOAT64
, 8, False), # array (double)
95 b
'l'[0]: lambda read
: unpack_array(read
, data_types
.ARRAY_INT64
, 8, True), # array (long)
96 b
'b'[0]: lambda read
: unpack_array(read
, data_types
.ARRAY_BOOL
, 1, False), # array (bool)
97 b
'c'[0]: lambda read
: unpack_array(read
, data_types
.ARRAY_BYTE
, 1, False), # array (ubyte)
101 def read_elem(read
, tell
, use_namedtuple
):
102 # [0] the offset at which this block ends
103 # [1] the number of properties in the scope
104 # [2] the length of the property list
105 end_offset
= read_uint(read
)
109 prop_count
= read_uint(read
)
110 prop_length
= read_uint(read
)
112 elem_id
= read_string_ubyte(read
) # elem name of the scope/key
113 elem_props_type
= bytearray(prop_count
) # elem property types
114 elem_props_data
= [None] * prop_count
# elem properties (if any)
115 elem_subtree
= [] # elem children (if any)
117 for i
in range(prop_count
):
118 data_type
= read(1)[0]
119 elem_props_data
[i
] = read_data_dict
[data_type
](read
)
120 elem_props_type
[i
] = data_type
122 if tell() < end_offset
:
123 while tell() < (end_offset
- _BLOCK_SENTINEL_LENGTH
):
124 elem_subtree
.append(read_elem(read
, tell
, use_namedtuple
))
126 if read(_BLOCK_SENTINEL_LENGTH
) != _BLOCK_SENTINEL_DATA
:
127 raise IOError("failed to read nested block sentinel, "
128 "expected all bytes to be 0")
130 if tell() != end_offset
:
131 raise IOError("scope length not reached, something is wrong")
133 args
= (elem_id
, elem_props_data
, elem_props_type
, elem_subtree
)
134 return FBXElem(*args
) if use_namedtuple
else args
137 def parse_version(fn
):
139 Return the FBX version,
140 if the file isn't a binary FBX return zero.
142 with
open(fn
, 'rb') as f
:
145 if read(len(_HEAD_MAGIC
)) != _HEAD_MAGIC
:
148 return read_uint(read
)
151 def parse(fn
, use_namedtuple
=True):
154 with
open(fn
, 'rb') as f
:
158 if read(len(_HEAD_MAGIC
)) != _HEAD_MAGIC
:
159 raise IOError("Invalid header")
161 fbx_version
= read_uint(read
)
164 elem
= read_elem(read
, tell
, use_namedtuple
)
167 root_elems
.append(elem
)
169 args
= (b
'', [], bytearray(0), root_elems
)
170 return FBXElem(*args
) if use_namedtuple
else args
, fbx_version