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 = (',', ':')
```
This commit is contained in:
Julien Duroure 2022-03-13 11:32:48 +01:00
parent 43477e00ce
commit 8653db3808
2 changed files with 15 additions and 6 deletions

View File

@ -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',

View File

@ -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)
#