2 # SPDX-License-Identifier: GPL-2.0-or-later
4 # Script copyright (C) 2014 Blender Foundation
12 This script will write a binary FBX file for each JSON argument given.
18 The JSON data is formatted into a list of nested lists of 4 items:
20 ``[id, [data, ...], "data_types", [subtree, ...]]``
22 Where each list may be empty, and the items in
23 the subtree are formatted the same way.
25 data_types is a string, aligned with data that spesifies a type
28 The types are as follows:
38 * 'f': - FLOAT32_ARRAY
40 * 'd': - FLOAT64_ARRAY
45 Note that key:value pairs aren't used since the id's are not
50 def elem_empty(elem
, name
):
52 sub_elem
= encode_bin
.FBXElem(name
)
54 elem
.elems
.append(sub_elem
)
58 def parse_json_rec(fbx_root
, json_node
):
59 name
, data
, data_types
, children
= json_node
62 assert(len(data_types
) == len(data
))
64 e
= elem_empty(fbx_root
, name
.encode())
65 for d
, dt
in zip(data
, data_types
):
79 d
= eval('b"""' + d
+ '"""')
82 d
= d
.encode().replace(b
"::", b
"\x00\x01")
89 e
.add_float32_array(d
)
91 e
.add_float64_array(d
)
97 if name
== "FBXVersion":
98 assert(data_types
== "I")
101 for child
in children
:
102 _ver
= parse_json_rec(e
, child
)
109 def parse_json(json_root
):
110 root
= elem_empty(None, b
"")
114 _ver
= parse_json_rec(root
, n
)
127 fn_fbx
= "%s.fbx" % os
.path
.splitext(fn
)[0]
128 print("Writing: %r " % fn_fbx
, end
="")
130 with
open(fn
) as f_json
:
131 json_root
= json
.load(f_json
)
132 fbx_root
, fbx_version
= parse_json(json_root
)
133 print("(Version %d) ..." % fbx_version
)
134 encode_bin
.write(fn_fbx
, fbx_root
, fbx_version
)
137 # ----------------------------------------------------------------------------
143 if "--help" in sys
.argv
:
147 for arg
in sys
.argv
[1:]:
151 print("Failed to convert %r, error:" % arg
)
154 traceback
.print_exc()
157 if __name__
== "__main__":