Fix add-ons with Python 3.12 by replacing "imp" with "importlib"
[blender-addons.git] / io_scene_fbx / json2fbx.py
blob8cbea51aee130cb5e27b156b19645ad293464404
1 #!/usr/bin/env python3
2 # SPDX-FileCopyrightText: 2014-2023 Blender Foundation
4 # SPDX-License-Identifier: GPL-2.0-or-later
6 """
7 Usage
8 =====
10 json2fbx [FILES]...
12 This script will write a binary FBX file for each JSON argument given.
15 Input
16 ======
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
26 for each property.
28 The types are as follows:
30 * 'Z': - INT8
31 * 'Y': - INT16
32 * 'B': - BOOL
33 * 'C': - CHAR
34 * 'I': - INT32
35 * 'F': - FLOAT32
36 * 'D': - FLOAT64
37 * 'L': - INT64
38 * 'R': - BYTES
39 * 'S': - STRING
40 * 'f': - FLOAT32_ARRAY
41 * 'i': - INT32_ARRAY
42 * 'd': - FLOAT64_ARRAY
43 * 'l': - INT64_ARRAY
44 * 'b': - BOOL ARRAY
45 * 'c': - BYTE ARRAY
47 Note that key:value pairs aren't used since the id's are not
48 ensured to be unique.
49 """
52 def elem_empty(elem, name):
53 import encode_bin
54 sub_elem = encode_bin.FBXElem(name)
55 if elem is not None:
56 elem.elems.append(sub_elem)
57 return sub_elem
60 def parse_json_rec(fbx_root, json_node):
61 name, data, data_types, children = json_node
62 ver = 0
64 assert(len(data_types) == len(data))
66 e = elem_empty(fbx_root, name.encode())
67 for d, dt in zip(data, data_types):
68 if dt == "B":
69 e.add_bool(d)
70 elif dt == "C":
71 d = eval('b"""' + d + '"""')
72 e.add_char(d)
73 elif dt == "Z":
74 e.add_int8(d)
75 elif dt == "Y":
76 e.add_int16(d)
77 elif dt == "I":
78 e.add_int32(d)
79 elif dt == "L":
80 e.add_int64(d)
81 elif dt == "F":
82 e.add_float32(d)
83 elif dt == "D":
84 e.add_float64(d)
85 elif dt == "R":
86 d = eval('b"""' + d + '"""')
87 e.add_bytes(d)
88 elif dt == "S":
89 d = d.encode().replace(b"::", b"\x00\x01")
90 e.add_string(d)
91 elif dt == "i":
92 e.add_int32_array(d)
93 elif dt == "l":
94 e.add_int64_array(d)
95 elif dt == "f":
96 e.add_float32_array(d)
97 elif dt == "d":
98 e.add_float64_array(d)
99 elif dt == "b":
100 e.add_bool_array(d)
101 elif dt == "c":
102 e.add_byte_array(d)
104 if name == "FBXVersion":
105 assert(data_types == "I")
106 ver = int(data[0])
108 for child in children:
109 _ver = parse_json_rec(e, child)
110 if _ver:
111 ver = _ver
113 return ver
116 def parse_json(json_root):
117 root = elem_empty(None, b"")
118 ver = 0
120 for n in json_root:
121 _ver = parse_json_rec(root, n)
122 if _ver:
123 ver = _ver
125 return root, ver
128 def json2fbx(fn):
129 import os
130 import json
132 import encode_bin
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 # ----------------------------------------------------------------------------
145 # Command Line
147 def main():
148 import sys
150 if "--help" in sys.argv:
151 print(__doc__)
152 return
154 for arg in sys.argv[1:]:
155 try:
156 json2fbx(arg)
157 except:
158 print("Failed to convert %r, error:" % arg)
160 import traceback
161 traceback.print_exc()
164 if __name__ == "__main__":
165 main()