Cleanup: remove unused operator args
[blender-addons.git] / io_scene_fbx / fbx2json.py
blob68ceb5b2d07926400fd1d65c62c8995d612b3b65
1 #!/usr/bin/env python3
2 # ##### BEGIN GPL LICENSE BLOCK #####
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software Foundation,
16 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # ##### END GPL LICENSE BLOCK #####
20 # <pep8 compliant>
22 # Script copyright (C) 2006-2012, assimp team
23 # Script copyright (C) 2013 Blender Foundation
25 """
26 Usage
27 =====
29 fbx2json [FILES]...
31 This script will write a JSON file for each FBX argument given.
34 Output
35 ======
37 The JSON data is formatted into a list of nested lists of 4 items:
39 ``[id, [data, ...], "data_types", [subtree, ...]]``
41 Where each list may be empty, and the items in
42 the subtree are formatted the same way.
44 data_types is a string, aligned with data that spesifies a type
45 for each property.
47 The types are as follows:
49 * 'Y': - INT16
50 * 'C': - BOOL
51 * 'I': - INT32
52 * 'F': - FLOAT32
53 * 'D': - FLOAT64
54 * 'L': - INT64
55 * 'R': - BYTES
56 * 'S': - STRING
57 * 'f': - FLOAT32_ARRAY
58 * 'i': - INT32_ARRAY
59 * 'd': - FLOAT64_ARRAY
60 * 'l': - INT64_ARRAY
61 * 'b': - BOOL ARRAY
62 * 'c': - BYTE ARRAY
64 Note that key:value pairs aren't used since the id's are not
65 ensured to be unique.
66 """
69 # ----------------------------------------------------------------------------
70 # FBX Binary Parser
72 from struct import unpack
73 import array
74 import zlib
76 # at the end of each nested block, there is a NUL record to indicate
77 # that the sub-scope exists (i.e. to distinguish between P: and P : {})
78 # this NUL record is 13 bytes long.
79 _BLOCK_SENTINEL_LENGTH = 13
80 _BLOCK_SENTINEL_DATA = (b'\0' * _BLOCK_SENTINEL_LENGTH)
81 _IS_BIG_ENDIAN = (__import__("sys").byteorder != 'little')
82 _HEAD_MAGIC = b'Kaydara FBX Binary\x20\x20\x00\x1a\x00'
83 from collections import namedtuple
84 FBXElem = namedtuple("FBXElem", ("id", "props", "props_type", "elems"))
85 del namedtuple
88 def read_uint(read):
89 return unpack(b'<I', read(4))[0]
92 def read_ubyte(read):
93 return unpack(b'B', read(1))[0]
96 def read_string_ubyte(read):
97 size = read_ubyte(read)
98 data = read(size)
99 return data
102 def unpack_array(read, array_type, array_stride, array_byteswap):
103 length = read_uint(read)
104 encoding = read_uint(read)
105 comp_len = read_uint(read)
107 data = read(comp_len)
109 if encoding == 0:
110 pass
111 elif encoding == 1:
112 data = zlib.decompress(data)
114 assert(length * array_stride == len(data))
116 data_array = array.array(array_type, data)
117 if array_byteswap and _IS_BIG_ENDIAN:
118 data_array.byteswap()
119 return data_array
122 read_data_dict = {
123 b'Y'[0]: lambda read: unpack(b'<h', read(2))[0], # 16 bit int
124 b'C'[0]: lambda read: unpack(b'?', read(1))[0], # 1 bit bool (yes/no)
125 b'I'[0]: lambda read: unpack(b'<i', read(4))[0], # 32 bit int
126 b'F'[0]: lambda read: unpack(b'<f', read(4))[0], # 32 bit float
127 b'D'[0]: lambda read: unpack(b'<d', read(8))[0], # 64 bit float
128 b'L'[0]: lambda read: unpack(b'<q', read(8))[0], # 64 bit int
129 b'R'[0]: lambda read: read(read_uint(read)), # binary data
130 b'S'[0]: lambda read: read(read_uint(read)), # string data
131 b'f'[0]: lambda read: unpack_array(read, 'f', 4, False), # array (float)
132 b'i'[0]: lambda read: unpack_array(read, 'i', 4, True), # array (int)
133 b'd'[0]: lambda read: unpack_array(read, 'd', 8, False), # array (double)
134 b'l'[0]: lambda read: unpack_array(read, 'q', 8, True), # array (long)
135 b'b'[0]: lambda read: unpack_array(read, 'b', 1, False), # array (bool)
136 b'c'[0]: lambda read: unpack_array(read, 'B', 1, False), # array (ubyte)
140 def read_elem(read, tell, use_namedtuple):
141 # [0] the offset at which this block ends
142 # [1] the number of properties in the scope
143 # [2] the length of the property list
144 end_offset = read_uint(read)
145 if end_offset == 0:
146 return None
148 prop_count = read_uint(read)
149 prop_length = read_uint(read)
151 elem_id = read_string_ubyte(read) # elem name of the scope/key
152 elem_props_type = bytearray(prop_count) # elem property types
153 elem_props_data = [None] * prop_count # elem properties (if any)
154 elem_subtree = [] # elem children (if any)
156 for i in range(prop_count):
157 data_type = read(1)[0]
158 elem_props_data[i] = read_data_dict[data_type](read)
159 elem_props_type[i] = data_type
161 if tell() < end_offset:
162 while tell() < (end_offset - _BLOCK_SENTINEL_LENGTH):
163 elem_subtree.append(read_elem(read, tell, use_namedtuple))
165 if read(_BLOCK_SENTINEL_LENGTH) != _BLOCK_SENTINEL_DATA:
166 raise IOError("failed to read nested block sentinel, "
167 "expected all bytes to be 0")
169 if tell() != end_offset:
170 raise IOError("scope length not reached, something is wrong")
172 args = (elem_id, elem_props_data, elem_props_type, elem_subtree)
173 return FBXElem(*args) if use_namedtuple else args
176 def parse_version(fn):
178 Return the FBX version,
179 if the file isn't a binary FBX return zero.
181 with open(fn, 'rb') as f:
182 read = f.read
184 if read(len(_HEAD_MAGIC)) != _HEAD_MAGIC:
185 return 0
187 return read_uint(read)
190 def parse(fn, use_namedtuple=True):
191 root_elems = []
193 with open(fn, 'rb') as f:
194 read = f.read
195 tell = f.tell
197 if read(len(_HEAD_MAGIC)) != _HEAD_MAGIC:
198 raise IOError("Invalid header")
200 fbx_version = read_uint(read)
202 while True:
203 elem = read_elem(read, tell, use_namedtuple)
204 if elem is None:
205 break
206 root_elems.append(elem)
208 args = (b'', [], bytearray(0), root_elems)
209 return FBXElem(*args) if use_namedtuple else args, fbx_version
212 # ----------------------------------------------------------------------------
213 # Inline Modules
215 # pyfbx.data_types
216 data_types = type(array)("data_types")
217 data_types.__dict__.update(
218 dict(
219 INT16 = b'Y'[0],
220 BOOL = b'C'[0],
221 INT32 = b'I'[0],
222 FLOAT32 = b'F'[0],
223 FLOAT64 = b'D'[0],
224 INT64 = b'L'[0],
225 BYTES = b'R'[0],
226 STRING = b'S'[0],
227 FLOAT32_ARRAY = b'f'[0],
228 INT32_ARRAY = b'i'[0],
229 FLOAT64_ARRAY = b'd'[0],
230 INT64_ARRAY = b'l'[0],
231 BOOL_ARRAY = b'b'[0],
232 BYTE_ARRAY = b'c'[0],
235 # pyfbx.parse_bin
236 parse_bin = type(array)("parse_bin")
237 parse_bin.__dict__.update(
238 dict(
239 parse = parse
243 # ----------------------------------------------------------------------------
244 # JSON Converter
245 # from pyfbx import parse_bin, data_types
246 import json
247 import array
250 def fbx2json_property_as_string(prop, prop_type):
251 if prop_type == data_types.STRING:
252 prop_str = prop.decode('utf-8')
253 prop_str = prop_str.replace('\x00\x01', '::')
254 return json.dumps(prop_str)
255 else:
256 prop_py_type = type(prop)
257 if prop_py_type == bytes:
258 return json.dumps(repr(prop)[2:-1])
259 elif prop_py_type == bool:
260 return json.dumps(prop)
261 elif prop_py_type == array.array:
262 return repr(list(prop))
264 return repr(prop)
267 def fbx2json_properties_as_string(fbx_elem):
268 return ", ".join(fbx2json_property_as_string(*prop_item)
269 for prop_item in zip(fbx_elem.props,
270 fbx_elem.props_type))
273 def fbx2json_recurse(fw, fbx_elem, ident, is_last):
274 fbx_elem_id = fbx_elem.id.decode('utf-8')
275 fw('%s["%s", ' % (ident, fbx_elem_id))
276 fw('[%s], ' % fbx2json_properties_as_string(fbx_elem))
277 fw('"%s", ' % (fbx_elem.props_type.decode('ascii')))
279 fw('[')
280 if fbx_elem.elems:
281 fw('\n')
282 ident_sub = ident + " "
283 for fbx_elem_sub in fbx_elem.elems:
284 fbx2json_recurse(fw, fbx_elem_sub, ident_sub,
285 fbx_elem_sub is fbx_elem.elems[-1])
286 fw(']')
288 fw(']%s' % ('' if is_last else ',\n'))
291 def fbx2json(fn):
292 import os
294 fn_json = "%s.json" % os.path.splitext(fn)[0]
295 print("Writing: %r " % fn_json, end="")
296 fbx_root_elem, fbx_version = parse(fn, use_namedtuple=True)
297 print("(Version %d) ..." % fbx_version)
299 with open(fn_json, 'w', encoding="ascii", errors='xmlcharrefreplace') as f:
300 fw = f.write
301 fw('[\n')
302 ident_sub = " "
303 for fbx_elem_sub in fbx_root_elem.elems:
304 fbx2json_recurse(f.write, fbx_elem_sub, ident_sub,
305 fbx_elem_sub is fbx_root_elem.elems[-1])
306 fw(']\n')
309 # ----------------------------------------------------------------------------
310 # Command Line
312 def main():
313 import sys
315 if "--help" in sys.argv:
316 print(__doc__)
317 return
319 for arg in sys.argv[1:]:
320 try:
321 fbx2json(arg)
322 except:
323 print("Failed to convert %r, error:" % arg)
325 import traceback
326 traceback.print_exc()
329 if __name__ == "__main__":
330 main()