Remove bl_options from menus which caused tests to fail
[blender-addons.git] / io_scene_fbx / json2fbx.py
blob0443cd4fffbde5da675fd97958fe3aac70d58fe1
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 * 'C': - BOOL
33 * 'I': - INT32
34 * 'F': - FLOAT32
35 * 'D': - FLOAT64
36 * 'L': - INT64
37 * 'R': - BYTES
38 * 'S': - STRING
39 * 'f': - FLOAT32_ARRAY
40 * 'i': - INT32_ARRAY
41 * 'd': - FLOAT64_ARRAY
42 * 'l': - INT64_ARRAY
43 * 'b': - BOOL ARRAY
44 * 'c': - BYTE ARRAY
46 Note that key:value pairs aren't used since the id's are not
47 ensured to be unique.
48 """
51 def elem_empty(elem, name):
52 import encode_bin
53 sub_elem = encode_bin.FBXElem(name)
54 if elem is not None:
55 elem.elems.append(sub_elem)
56 return sub_elem
59 def parse_json_rec(fbx_root, json_node):
60 name, data, data_types, children = json_node
61 ver = 0
63 assert(len(data_types) == len(data))
65 e = elem_empty(fbx_root, name.encode())
66 for d, dt in zip(data, data_types):
67 if dt == "C":
68 e.add_bool(d)
69 elif dt == "Z":
70 e.add_int8(d)
71 elif dt == "Y":
72 e.add_int16(d)
73 elif dt == "I":
74 e.add_int32(d)
75 elif dt == "L":
76 e.add_int64(d)
77 elif dt == "F":
78 e.add_float32(d)
79 elif dt == "D":
80 e.add_float64(d)
81 elif dt == "R":
82 d = eval('b"""' + d + '"""')
83 e.add_bytes(d)
84 elif dt == "S":
85 d = d.encode().replace(b"::", b"\x00\x01")
86 e.add_string(d)
87 elif dt == "i":
88 e.add_int32_array(d)
89 elif dt == "l":
90 e.add_int64_array(d)
91 elif dt == "f":
92 e.add_float32_array(d)
93 elif dt == "d":
94 e.add_float64_array(d)
95 elif dt == "b":
96 e.add_bool_array(d)
97 elif dt == "c":
98 e.add_byte_array(d)
100 if name == "FBXVersion":
101 assert(data_types == "I")
102 ver = int(data[0])
104 for child in children:
105 _ver = parse_json_rec(e, child)
106 if _ver:
107 ver = _ver
109 return ver
112 def parse_json(json_root):
113 root = elem_empty(None, b"")
114 ver = 0
116 for n in json_root:
117 _ver = parse_json_rec(root, n)
118 if _ver:
119 ver = _ver
121 return root, ver
124 def json2fbx(fn):
125 import os
126 import json
128 import encode_bin
130 fn_fbx = "%s.fbx" % os.path.splitext(fn)[0]
131 print("Writing: %r " % fn_fbx, end="")
132 json_root = []
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 # ----------------------------------------------------------------------------
141 # Command Line
143 def main():
144 import sys
146 if "--help" in sys.argv:
147 print(__doc__)
148 return
150 for arg in sys.argv[1:]:
151 try:
152 json2fbx(arg)
153 except:
154 print("Failed to convert %r, error:" % arg)
156 import traceback
157 traceback.print_exc()
160 if __name__ == "__main__":
161 main()