[Bf-extensions-cvs] [fc8bd7d6] master: glTF: Better uri management

Julien Duroure noreply at git.blender.org
Fri Jan 27 18:48:49 CET 2023


Commit: fc8bd7d6d4b7e0b34fb008ac328e839cca813094
Author: Julien Duroure
Date:   Fri Jan 27 18:45:45 2023 +0100
Branches: master
https://developer.blender.org/rBAfc8bd7d6d4b7e0b34fb008ac328e839cca813094

glTF: Better uri management

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

M	io_scene_gltf2/__init__.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py
M	io_scene_gltf2/blender/imp/gltf2_blender_image.py
A	io_scene_gltf2/io/com/gltf2_io_path.py
M	io_scene_gltf2/io/imp/gltf2_io_gltf.py

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

diff --git a/io_scene_gltf2/__init__.py b/io_scene_gltf2/__init__.py
index 770814b1..1f038922 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, 5, 18),
+    "version": (3, 5, 19),
     'blender': (3, 4, 0),
     'location': 'File > Import-Export',
     'description': 'Import-Export as glTF 2.0',
@@ -576,6 +576,7 @@ class ExportGLTF2_Base(ConvertGLTF2_Base):
         import os
         import datetime
         from .blender.exp import gltf2_blender_export
+        from .io.com.gltf2_io_path import path_to_uri
 
         if self.will_save_settings:
             self.save_settings(context)
@@ -680,7 +681,7 @@ class ExportGLTF2_Base(ConvertGLTF2_Base):
 
         export_settings['gltf_binary'] = bytearray()
         export_settings['gltf_binaryfilename'] = (
-            os.path.splitext(os.path.basename(self.filepath))[0] + '.bin'
+            path_to_uri(os.path.splitext(os.path.basename(self.filepath))[0] + '.bin')
         )
 
         user_extensions = []
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py b/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py
index 98878f92..6f31bd8d 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py
@@ -6,6 +6,7 @@ import typing
 import os
 
 from . import gltf2_blender_export_keys
+from io_scene_gltf2.io.com.gltf2_io_path import path_to_uri
 from io_scene_gltf2.io.com import gltf2_io
 from io_scene_gltf2.blender.exp import gltf2_blender_search_node_tree
 from io_scene_gltf2.io.exp import gltf2_io_binary_data
@@ -63,12 +64,6 @@ def gather_image(
 
 def __gather_original_uri(original_uri, export_settings):
 
-    def _path_to_uri(path):
-        import urllib
-        path = os.path.normpath(path)
-        path = path.replace(os.sep, '/')
-        return urllib.parse.quote(path)
-
     path_to_image = bpy.path.abspath(original_uri)
     if not os.path.exists(path_to_image): return None
     try:
@@ -79,7 +74,7 @@ def __gather_original_uri(original_uri, export_settings):
     except ValueError:
         # eg. because no relative path between C:\ and D:\ on Windows
         return None
-    return _path_to_uri(rel_path)
+    return path_to_uri(rel_path)
 
 
 @cached
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py b/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py
index 574693d7..eff9e121 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py
@@ -6,6 +6,7 @@ import urllib.parse
 from typing import List
 
 from ... import get_version_string
+from io_scene_gltf2.io.com.gltf2_io_path import path_to_uri
 from io_scene_gltf2.io.com import gltf2_io
 from io_scene_gltf2.io.com import gltf2_io_extensions
 from io_scene_gltf2.io.exp import gltf2_io_binary_data
@@ -236,7 +237,7 @@ class GlTF2Exporter:
             abs_path,
             start=self.export_settings[gltf2_blender_export_keys.FILE_DIRECTORY],
         )
-        return _path_to_uri(rel_path)
+        return path_to_uri(rel_path)
 
     @classmethod
     def __get_key_path(cls, d: dict, keypath: List[str], default):
@@ -325,8 +326,3 @@ class GlTF2Exporter:
 
         # do nothing for any type that does not match a glTF schema (primitives)
         return node
-
-def _path_to_uri(path):
-    path = os.path.normpath(path)
-    path = path.replace(os.sep, '/')
-    return urllib.parse.quote(path)
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_image.py b/io_scene_gltf2/blender/imp/gltf2_blender_image.py
index abce0354..c9a87527 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_image.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_image.py
@@ -3,11 +3,9 @@
 
 import bpy
 import os
-import tempfile
-from os.path import dirname, join, isfile, basename, normpath
-import urllib.parse
-import re
+from os.path import dirname, join, basename
 
+from ...io.com.gltf2_io_path import uri_to_path
 from ...io.imp.gltf2_io_binary import BinaryData
 from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions
 
@@ -47,7 +45,7 @@ def create_from_file(gltf, img_idx):
 
     img = gltf.data.images[img_idx]
 
-    path = join(dirname(gltf.filename), _uri_to_path(img.uri))
+    path = join(dirname(gltf.filename), uri_to_path(img.uri))
     path = os.path.abspath(path)
     if bpy.data.is_saved and bpy.context.preferences.filepaths.use_relative_paths:
         try:
@@ -100,6 +98,3 @@ def _placeholder_image(name, path):
     image.source = 'FILE'
     return image
 
-def _uri_to_path(uri):
-    uri = urllib.parse.unquote(uri)
-    return normpath(uri)
diff --git a/io_scene_gltf2/io/com/gltf2_io_path.py b/io_scene_gltf2/io/com/gltf2_io_path.py
new file mode 100644
index 00000000..37ad36ed
--- /dev/null
+++ b/io_scene_gltf2/io/com/gltf2_io_path.py
@@ -0,0 +1,16 @@
+# SPDX-License-Identifier: Apache-2.0
+# Copyright 2018-2023 The glTF-Blender-IO authors.
+
+from urllib.parse import unquote, quote
+from os.path import normpath
+from os import sep
+
+def uri_to_path(uri):
+    uri = uri.replace('\\', '/') # Some files come with \\ as dir separator
+    uri = unquote(uri)
+    return normpath(uri)
+
+def path_to_uri(path):
+    path = normpath(path)
+    path = path.replace(sep, '/')
+    return quote(path)
diff --git a/io_scene_gltf2/io/imp/gltf2_io_gltf.py b/io_scene_gltf2/io/imp/gltf2_io_gltf.py
index 75ce7265..17b91d53 100755
--- a/io_scene_gltf2/io/imp/gltf2_io_gltf.py
+++ b/io_scene_gltf2/io/imp/gltf2_io_gltf.py
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: Apache-2.0
 # Copyright 2018-2021 The glTF-Blender-IO authors.
 
+from ...io.com.gltf2_io_path import uri_to_path
 from ..com.gltf2_io import gltf_from_dict
 from ..com.gltf2_io_debug import Log
 import logging
@@ -8,7 +9,6 @@ import json
 import struct
 import base64
 from os.path import dirname, join, isfile
-from urllib.parse import unquote
 
 
 # Raise this error to have the importer report an error message.
@@ -186,7 +186,7 @@ class glTFImporter():
                 data = uri[idx + len(sep):]
                 return memoryview(base64.b64decode(data))
 
-        path = join(dirname(self.filename), unquote(uri))
+        path = join(dirname(self.filename), uri_to_path(uri))
         try:
             with open(path, 'rb') as f_:
                 return memoryview(f_.read())



More information about the Bf-extensions-cvs mailing list