diff options
author | Julien Duroure <julien.duroure@gmail.com> | 2022-03-13 13:32:48 +0300 |
---|---|---|
committer | Julien Duroure <julien.duroure@gmail.com> | 2022-03-13 13:32:48 +0300 |
commit | 8653db3808888a79bd83b92e33d314bbb9d873c2 (patch) | |
tree | dda8c80cafa3b173aaea8e1c9ffd8bad7522f89a | |
parent | 43477e00ce44cf561a38365d35c769010392c102 (diff) |
glTF exporter: add a hook for user extension to change json encoding
This allows user to use no indent in json, for example
Here is an example of hook:
```
def gather_gltf_encoded_hook(self, gltf_format, sort_order, export_settings):
gltf_format.indent = None
gltf_format.separators = (',', ':')
```
-rwxr-xr-x | io_scene_gltf2/__init__.py | 2 | ||||
-rwxr-xr-x | io_scene_gltf2/io/exp/gltf2_io_export.py | 19 |
2 files changed, 15 insertions, 6 deletions
diff --git a/io_scene_gltf2/__init__.py b/io_scene_gltf2/__init__.py index 369090e5..b87af39a 100755 --- a/io_scene_gltf2/__init__.py +++ b/io_scene_gltf2/__init__.py @@ -4,7 +4,7 @@ bl_info = { 'name': 'glTF 2.0 format', 'author': 'Julien Duroure, Scurest, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin Schmithüsen, Jim Eckerlein, and many external contributors', - "version": (3, 2, 12), + "version": (3, 2, 13), 'blender': (3, 1, 0), 'location': 'File > Import-Export', 'description': 'Import-Export as glTF 2.0', diff --git a/io_scene_gltf2/io/exp/gltf2_io_export.py b/io_scene_gltf2/io/exp/gltf2_io_export.py index 49cc4a78..aee74ba9 100755 --- a/io_scene_gltf2/io/exp/gltf2_io_export.py +++ b/io_scene_gltf2/io/exp/gltf2_io_export.py @@ -7,6 +7,7 @@ import json import struct +from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions # # Globals @@ -19,13 +20,18 @@ from collections import OrderedDict def save_gltf(gltf, export_settings, encoder, glb_buffer): - indent = None - separators = (',', ':') + # Use a class here, to be able to pass data by reference to hook (to be able to change them inside hook) + class GlTF_format: + def __init__(self, indent, separators): + self.indent = indent + self.separators = separators + + gltf_format = GlTF_format(None, (',', ':')) if export_settings['gltf_format'] != 'GLB': - indent = 4 + gltf_format.indent = 4 # The comma is typically followed by a newline, so no trailing whitespace is needed on it. - separators = (',', ' : ') + gltf_format.separators = (',', ' : ') sort_order = [ "asset", @@ -48,8 +54,11 @@ def save_gltf(gltf, export_settings, encoder, glb_buffer): "samplers", "buffers" ] + + export_user_extensions('gather_gltf_encoded_hook', export_settings, gltf_format, sort_order) + gltf_ordered = OrderedDict(sorted(gltf.items(), key=lambda item: sort_order.index(item[0]))) - gltf_encoded = json.dumps(gltf_ordered, indent=indent, separators=separators, cls=encoder, allow_nan=False) + gltf_encoded = json.dumps(gltf_ordered, indent=gltf_format.indent, separators=gltf_format.separators, cls=encoder, allow_nan=False) # |