2 # SPDX-License-Identifier: GPL-2.0-or-later
6 # Script copyright (C) 2014 Blender Foundation
14 This script will write a binary FBX file for each JSON argument given.
20 The JSON data is formatted into a list of nested lists of 4 items:
22 ``[id, [data, ...], "data_types", [subtree, ...]]``
24 Where each list may be empty, and the items in
25 the subtree are formatted the same way.
27 data_types is a string, aligned with data that spesifies a type
30 The types are as follows:
40 * 'f': - FLOAT32_ARRAY
42 * 'd': - FLOAT64_ARRAY
47 Note that key:value pairs aren't used since the id's are not
52 def elem_empty(elem
, name
):
54 sub_elem
= encode_bin
.FBXElem(name
)
56 elem
.elems
.append(sub_elem
)
60 def parse_json_rec(fbx_root
, json_node
):
61 name
, data
, data_types
, children
= json_node
64 assert(len(data_types
) == len(data
))
66 e
= elem_empty(fbx_root
, name
.encode())
67 for d
, dt
in zip(data
, data_types
):
81 d
= eval('b"""' + d
+ '"""')
84 d
= d
.encode().replace(b
"::", b
"\x00\x01")
91 e
.add_float32_array(d
)
93 e
.add_float64_array(d
)
99 if name
== "FBXVersion":
100 assert(data_types
== "I")
103 for child
in children
:
104 _ver
= parse_json_rec(e
, child
)
111 def parse_json(json_root
):
112 root
= elem_empty(None, b
"")
116 _ver
= parse_json_rec(root
, n
)
129 fn_fbx
= "%s.fbx" % os
.path
.splitext(fn
)[0]
130 print("Writing: %r " % fn_fbx
, end
="")
132 with
open(fn
) as f_json
:
133 json_root
= json
.load(f_json
)
134 fbx_root
, fbx_version
= parse_json(json_root
)
135 print("(Version %d) ..." % fbx_version
)
136 encode_bin
.write(fn_fbx
, fbx_root
, fbx_version
)
139 # ----------------------------------------------------------------------------
145 if "--help" in sys
.argv
:
149 for arg
in sys
.argv
[1:]:
153 print("Failed to convert %r, error:" % arg
)
156 traceback
.print_exc()
159 if __name__
== "__main__":