[Bf-extensions-cvs] [7a3fdf08] master: glTF exporter: less naive file format detection for textures

Julien Duroure noreply at git.blender.org
Sat Apr 11 15:42:36 CEST 2020


Commit: 7a3fdf08f3fe4984bc81219a075a8bd3234c7d72
Author: Julien Duroure
Date:   Sat Apr 11 15:34:44 2020 +0200
Branches: master
https://developer.blender.org/rBA7a3fdf08f3fe4984bc81219a075a8bd3234c7d72

glTF exporter: less naive file format detection for textures

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

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_image.py

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

diff --git a/io_scene_gltf2/__init__.py b/io_scene_gltf2/__init__.py
index 73ffc5a1..ffcfeb7d 100755
--- a/io_scene_gltf2/__init__.py
+++ b/io_scene_gltf2/__init__.py
@@ -15,7 +15,7 @@
 bl_info = {
     'name': 'glTF 2.0 format',
     'author': 'Julien Duroure, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin Schmithüsen, Jim Eckerlein, and many external contributors',
-    "version": (1, 2, 60),
+    "version": (1, 2, 61),
     'blender': (2, 82, 7),
     'location': 'File > Import-Export',
     'description': 'Import-Export as glTF 2.0',
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 d1579803..c9683baf 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py
@@ -103,7 +103,7 @@ def __gather_mime_type(sockets_or_slots, export_image, export_settings):
 
     if export_settings["gltf_image_format"] == "AUTO":
         image = export_image.blender_image()
-        if image is not None and image.file_format == 'JPEG':
+        if image is not None and __is_blender_image_a_jpeg(image):
             return "image/jpeg"
         return "image/png"
 
@@ -244,3 +244,10 @@ def __get_texname_from_slot(sockets_or_slots, export_settings):
 
     elif isinstance(sockets_or_slots[0], bpy.types.MaterialTextureSlot):
         return sockets_or_slots[0].texture.image.name
+
+
+def __is_blender_image_a_jpeg(image: bpy.types.Image) -> bool:
+    if image.source != 'FILE':
+        return False
+    path = image.filepath_raw.lower()
+    return path.endswith('.jpg') or path.endswith('.jpeg') or path.endswith('.jpe')
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_image.py b/io_scene_gltf2/blender/exp/gltf2_blender_image.py
index 145e1ed9..e9db7e66 100644
--- a/io_scene_gltf2/blender/exp/gltf2_blender_image.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_image.py
@@ -272,15 +272,24 @@ class ExportImage:
 
     def __encode_from_image(self, image: bpy.types.Image) -> bytes:
         # See if there is an existing file we can use.
+        data = None
         if image.source == 'FILE' and image.file_format == self.file_format and \
                 not image.is_dirty:
             if image.packed_file is not None:
-                return image.packed_file.data
+                data = image.packed_file.data
             else:
                 src_path = bpy.path.abspath(image.filepath_raw)
                 if os.path.isfile(src_path):
                     with open(src_path, 'rb') as f:
-                        return f.read()
+                        data = f.read()
+        # Check magic number is right
+        if data:
+            if self.file_format == 'PNG':
+                if data.startswith(b'\x89PNG'):
+                    return data
+            elif self.file_format == 'JPEG':
+                if data.startswith(b'\xff\xd8\xff'):
+                    return data
 
         # Copy to a temp image and save.
         tmp_image = None



More information about the Bf-extensions-cvs mailing list