2 # SPDX-FileCopyrightText: 2014-2023 Blender Foundation
4 # SPDX-License-Identifier: GPL-2.0-or-later
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:
39 * 'f': - FLOAT32_ARRAY
41 * 'd': - FLOAT64_ARRAY
46 Note that key:value pairs aren't used since the id's are not
51 def elem_empty(elem
, name
):
53 sub_elem
= encode_bin
.FBXElem(name
)
55 elem
.elems
.append(sub_elem
)
59 def parse_json_rec(fbx_root
, json_node
):
60 name
, data
, data_types
, children
= json_node
63 assert(len(data_types
) == len(data
))
65 e
= elem_empty(fbx_root
, name
.encode())
66 for d
, dt
in zip(data
, data_types
):
82 d
= eval('b"""' + d
+ '"""')
85 d
= d
.encode().replace(b
"::", b
"\x00\x01")
92 e
.add_float32_array(d
)
94 e
.add_float64_array(d
)
100 if name
== "FBXVersion":
101 assert(data_types
== "I")
104 for child
in children
:
105 _ver
= parse_json_rec(e
, child
)
112 def parse_json(json_root
):
113 root
= elem_empty(None, b
"")
117 _ver
= parse_json_rec(root
, n
)
130 fn_fbx
= "%s.fbx" % os
.path
.splitext(fn
)[0]
131 print("Writing: %r " % fn_fbx
, end
="")
133 with
open(fn
) as f_json
:
134 json_root
= json
.load(f_json
)
135 fbx_root
, fbx_version
= parse_json(json_root
)
136 print("(Version %d) ..." % fbx_version
)
137 encode_bin
.write(fn_fbx
, fbx_root
, fbx_version
)
140 # ----------------------------------------------------------------------------
146 if "--help" in sys
.argv
:
150 for arg
in sys
.argv
[1:]:
154 print("Failed to convert %r, error:" % arg
)
157 traceback
.print_exc()
160 if __name__
== "__main__":