glTF: Better uri management

This commit is contained in:
Julien Duroure 2023-01-27 18:45:45 +01:00
parent 2207539e32
commit fc8bd7d6d4
6 changed files with 28 additions and 25 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, 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 = []

View File

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

View File

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

View File

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

View File

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

View File

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