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:
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
):
71 d
= eval('b"""' + d
+ '"""')
86 d
= eval('b"""' + d
+ '"""')
89 d
= d
.encode().replace(b
"::", b
"\x00\x01")
96 e
.add_float32_array(d
)
98 e
.add_float64_array(d
)
104 if name
== "FBXVersion":
105 assert(data_types
== "I")
108 for child
in children
:
109 _ver
= parse_json_rec(e
, child
)
116 def parse_json(json_root
):
117 root
= elem_empty(None, b
"")
121 _ver
= parse_json_rec(root
, n
)
134 fn_fbx
= "%s.fbx" % os
.path
.splitext(fn
)[0]
135 print("Writing: %r " % fn_fbx
, end
="")
136 with
open(fn
) as f_json
:
137 json_root
= json
.load(f_json
)
138 with encode_bin
.FBXElem
.enable_multithreading_cm():
139 fbx_root
, fbx_version
= parse_json(json_root
)
140 print("(Version %d) ..." % fbx_version
)
141 encode_bin
.write(fn_fbx
, fbx_root
, fbx_version
)
144 # ----------------------------------------------------------------------------
150 if "--help" in sys
.argv
:
154 for arg
in sys
.argv
[1:]:
158 print("Failed to convert %r, error:" % arg
)
161 traceback
.print_exc()
164 if __name__
== "__main__":