2 # ##### BEGIN GPL LICENSE BLOCK #####
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software Foundation,
16 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # ##### END GPL LICENSE BLOCK #####
22 # Script copyright (C) 2014 Blender Foundation
30 This script will write a binary FBX file for each JSON argument given.
36 The JSON data is formatted into a list of nested lists of 4 items:
38 ``[id, [data, ...], "data_types", [subtree, ...]]``
40 Where each list may be empty, and the items in
41 the subtree are formatted the same way.
43 data_types is a string, aligned with data that spesifies a type
46 The types are as follows:
56 * 'f': - FLOAT32_ARRAY
58 * 'd': - FLOAT64_ARRAY
63 Note that key:value pairs aren't used since the id's are not
68 def elem_empty(elem
, name
):
70 sub_elem
= encode_bin
.FBXElem(name
)
72 elem
.elems
.append(sub_elem
)
76 def parse_json_rec(fbx_root
, json_node
):
77 name
, data
, data_types
, children
= json_node
80 assert(len(data_types
) == len(data
))
82 e
= elem_empty(fbx_root
, name
.encode())
83 for d
, dt
in zip(data
, data_types
):
97 d
= eval('b"""' + d
+ '"""')
100 d
= d
.encode().replace(b
"::", b
"\x00\x01")
107 e
.add_float32_array(d
)
109 e
.add_float64_array(d
)
115 if name
== "FBXVersion":
116 assert(data_types
== "I")
119 for child
in children
:
120 _ver
= parse_json_rec(e
, child
)
127 def parse_json(json_root
):
128 root
= elem_empty(None, b
"")
132 _ver
= parse_json_rec(root
, n
)
145 fn_fbx
= "%s.fbx" % os
.path
.splitext(fn
)[0]
146 print("Writing: %r " % fn_fbx
, end
="")
148 with
open(fn
) as f_json
:
149 json_root
= json
.load(f_json
)
150 fbx_root
, fbx_version
= parse_json(json_root
)
151 print("(Version %d) ..." % fbx_version
)
152 encode_bin
.write(fn_fbx
, fbx_root
, fbx_version
)
155 # ----------------------------------------------------------------------------
161 if "--help" in sys
.argv
:
165 for arg
in sys
.argv
[1:]:
169 print("Failed to convert %r, error:" % arg
)
172 traceback
.print_exc()
175 if __name__
== "__main__":