Repository: khronosgroup/gltf-blender-io
Issue: JPG textures referenced as PNG

When exporting a model with pre-existing jpg textures they will be referenced as png textures anyway (both in uri and mime type). This completely breaks the model because that file doesn't exist.

Changed files:
addons/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py, addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py

diff --git a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py
index 7077faeb..c9c2875d 100644
--- a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py
+++ b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py
@@ -11,6 +11,7 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
+import re
 
 import bpy
 import typing
@@ -30,13 +31,17 @@ def gather_image(
         export_settings):
     if not __filter_image(blender_shader_sockets_or_texture_slots, export_settings):
         return None
+
+    uri = __gather_uri(blender_shader_sockets_or_texture_slots, export_settings)
+    mime_type = __gather_mime_type(uri.filepath)
+
     image = gltf2_io.Image(
         buffer_view=__gather_buffer_view(blender_shader_sockets_or_texture_slots, export_settings),
         extensions=__gather_extensions(blender_shader_sockets_or_texture_slots, export_settings),
         extras=__gather_extras(blender_shader_sockets_or_texture_slots, export_settings),
-        mime_type=__gather_mime_type(blender_shader_sockets_or_texture_slots, export_settings),
+        mime_type=mime_type,
         name=__gather_name(blender_shader_sockets_or_texture_slots, export_settings),
-        uri=__gather_uri(blender_shader_sockets_or_texture_slots, export_settings)
+        uri=uri
     )
     return image
 
@@ -63,9 +68,13 @@ def __gather_extras(sockets_or_slots, export_settings):
     return None
 
 
-def __gather_mime_type(sockets_or_slots, export_settings):
-    return 'image/png'
-    # return 'image/jpeg'
+def __gather_mime_type(filepath):
+    extension_types = {'.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg'}
+    fallback_extension = extension_types['.png']
+
+    matches = re.findall(r'\\.\\w+$', filepath)
+    extension = matches[0] if len(matches) > 0 else fallback_extension
+    return extension_types[extension] if extension.lower() in extension_types.keys() else fallback_extension
 
 
 def __gather_name(sockets_or_slots, export_settings):
diff --git a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py
index f5ac7fa0..d75fa301 100644
--- a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py
+++ b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py
@@ -215,7 +215,7 @@ class GlTF2Exporter:
         # TODO: we need to know the image url at this point already --> maybe add all options to the constructor of the
         # exporter
         # TODO: allow embedding of images (base64)
-        return image.name + ".png"
+        return image.name + image.get_extension()
 
     @classmethod
     def __get_key_path(cls, d: dict, keypath: List[str], default=[]):