Merge branch 'blender-v3.3-release'
[blender-addons.git] / io_scene_fbx / parse_fbx.py
blob96fd81044f530b22d677d93604fabeee9d998048
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # Script copyright (C) 2006-2012, assimp team
4 # Script copyright (C) 2013 Blender Foundation
6 __all__ = (
7 "parse",
8 "data_types",
9 "parse_version",
10 "FBXElem",
13 from struct import unpack
14 import array
15 import zlib
17 from . import data_types
19 # at the end of each nested block, there is a NUL record to indicate
20 # that the sub-scope exists (i.e. to distinguish between P: and P : {})
21 _BLOCK_SENTINEL_LENGTH = ...
22 _BLOCK_SENTINEL_DATA = ...
23 read_fbx_elem_uint = ...
24 _IS_BIG_ENDIAN = (__import__("sys").byteorder != 'little')
25 _HEAD_MAGIC = b'Kaydara FBX Binary\x20\x20\x00\x1a\x00'
26 from collections import namedtuple
27 FBXElem = namedtuple("FBXElem", ("id", "props", "props_type", "elems"))
28 del namedtuple
31 def read_uint(read):
32 return unpack(b'<I', read(4))[0]
35 def read_uint64(read):
36 return unpack(b'<Q', read(8))[0]
39 def read_ubyte(read):
40 return unpack(b'B', read(1))[0]
43 def read_string_ubyte(read):
44 size = read_ubyte(read)
45 data = read(size)
46 return data
49 def unpack_array(read, array_type, array_stride, array_byteswap):
50 length = read_uint(read)
51 encoding = read_uint(read)
52 comp_len = read_uint(read)
54 data = read(comp_len)
56 if encoding == 0:
57 pass
58 elif encoding == 1:
59 data = zlib.decompress(data)
61 assert(length * array_stride == len(data))
63 data_array = array.array(array_type, data)
64 if array_byteswap and _IS_BIG_ENDIAN:
65 data_array.byteswap()
66 return data_array
69 read_data_dict = {
70 b'Y'[0]: lambda read: unpack(b'<h', read(2))[0], # 16 bit int
71 b'C'[0]: lambda read: unpack(b'?', read(1))[0], # 1 bit bool (yes/no)
72 b'I'[0]: lambda read: unpack(b'<i', read(4))[0], # 32 bit int
73 b'F'[0]: lambda read: unpack(b'<f', read(4))[0], # 32 bit float
74 b'D'[0]: lambda read: unpack(b'<d', read(8))[0], # 64 bit float
75 b'L'[0]: lambda read: unpack(b'<q', read(8))[0], # 64 bit int
76 b'R'[0]: lambda read: read(read_uint(read)), # binary data
77 b'S'[0]: lambda read: read(read_uint(read)), # string data
78 b'f'[0]: lambda read: unpack_array(read, data_types.ARRAY_FLOAT32, 4, False), # array (float)
79 b'i'[0]: lambda read: unpack_array(read, data_types.ARRAY_INT32, 4, True), # array (int)
80 b'd'[0]: lambda read: unpack_array(read, data_types.ARRAY_FLOAT64, 8, False), # array (double)
81 b'l'[0]: lambda read: unpack_array(read, data_types.ARRAY_INT64, 8, True), # array (long)
82 b'b'[0]: lambda read: unpack_array(read, data_types.ARRAY_BOOL, 1, False), # array (bool)
83 b'c'[0]: lambda read: unpack_array(read, data_types.ARRAY_BYTE, 1, False), # array (ubyte)
87 # FBX 7500 (aka FBX2016) introduces incompatible changes at binary level:
88 # * The NULL block marking end of nested stuff switches from 13 bytes long to 25 bytes long.
89 # * The FBX element metadata (end_offset, prop_count and prop_length) switch from uint32 to uint64.
90 def init_version(fbx_version):
91 global _BLOCK_SENTINEL_LENGTH, _BLOCK_SENTINEL_DATA, read_fbx_elem_uint
93 _BLOCK_SENTINEL_LENGTH = ...
94 _BLOCK_SENTINEL_DATA = ...
95 read_fbx_elem_uint = ...
97 if fbx_version < 7500:
98 _BLOCK_SENTINEL_LENGTH = 13
99 read_fbx_elem_uint = read_uint
100 else:
101 _BLOCK_SENTINEL_LENGTH = 25
102 read_fbx_elem_uint = read_uint64
103 _BLOCK_SENTINEL_DATA = (b'\0' * _BLOCK_SENTINEL_LENGTH)
106 def read_elem(read, tell, use_namedtuple):
107 # [0] the offset at which this block ends
108 # [1] the number of properties in the scope
109 # [2] the length of the property list
110 end_offset = read_fbx_elem_uint(read)
111 if end_offset == 0:
112 return None
114 prop_count = read_fbx_elem_uint(read)
115 prop_length = read_fbx_elem_uint(read)
117 elem_id = read_string_ubyte(read) # elem name of the scope/key
118 elem_props_type = bytearray(prop_count) # elem property types
119 elem_props_data = [None] * prop_count # elem properties (if any)
120 elem_subtree = [] # elem children (if any)
122 for i in range(prop_count):
123 data_type = read(1)[0]
124 elem_props_data[i] = read_data_dict[data_type](read)
125 elem_props_type[i] = data_type
127 if tell() < end_offset:
128 while tell() < (end_offset - _BLOCK_SENTINEL_LENGTH):
129 elem_subtree.append(read_elem(read, tell, use_namedtuple))
131 if read(_BLOCK_SENTINEL_LENGTH) != _BLOCK_SENTINEL_DATA:
132 raise IOError("failed to read nested block sentinel, "
133 "expected all bytes to be 0")
135 if tell() != end_offset:
136 raise IOError("scope length not reached, something is wrong")
138 args = (elem_id, elem_props_data, elem_props_type, elem_subtree)
139 return FBXElem(*args) if use_namedtuple else args
142 def parse_version(fn):
144 Return the FBX version,
145 if the file isn't a binary FBX return zero.
147 with open(fn, 'rb') as f:
148 read = f.read
150 if read(len(_HEAD_MAGIC)) != _HEAD_MAGIC:
151 return 0
153 return read_uint(read)
156 def parse(fn, use_namedtuple=True):
157 root_elems = []
159 with open(fn, 'rb') as f:
160 read = f.read
161 tell = f.tell
163 if read(len(_HEAD_MAGIC)) != _HEAD_MAGIC:
164 raise IOError("Invalid header")
166 fbx_version = read_uint(read)
167 init_version(fbx_version)
169 while True:
170 elem = read_elem(read, tell, use_namedtuple)
171 if elem is None:
172 break
173 root_elems.append(elem)
175 args = (b'', [], bytearray(0), root_elems)
176 return FBXElem(*args) if use_namedtuple else args, fbx_version