[Bf-extensions-cvs] [8653db38] master: glTF exporter: add a hook for user extension to change json encoding

Julien Duroure noreply at git.blender.org
Sun Mar 13 11:34:46 CET 2022


Commit: 8653db3808888a79bd83b92e33d314bbb9d873c2
Author: Julien Duroure
Date:   Sun Mar 13 11:32:48 2022 +0100
Branches: master
https://developer.blender.org/rBA8653db3808888a79bd83b92e33d314bbb9d873c2

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 = (',', ':')
```

===================================================================

M	io_scene_gltf2/__init__.py
M	io_scene_gltf2/io/exp/gltf2_io_export.py

===================================================================

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



More information about the Bf-extensions-cvs mailing list