[Bf-extensions-cvs] [3ce41afd] blender-v2.92-release: glTF exporter: more fixes for changing the filename when format changed

Julien Duroure noreply at git.blender.org
Wed Feb 10 20:17:08 CET 2021


Commit: 3ce41afdfa56a9eef6934dc012e5621a74ebbb7b
Author: Julien Duroure
Date:   Wed Feb 10 20:16:27 2021 +0100
Branches: blender-v2.92-release
https://developer.blender.org/rBA3ce41afdfa56a9eef6934dc012e5621a74ebbb7b

glTF exporter: more fixes for changing the filename when format changed

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

M	io_scene_gltf2/__init__.py

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

diff --git a/io_scene_gltf2/__init__.py b/io_scene_gltf2/__init__.py
index dd733b34..5f4f9fa8 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, Scurest, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin Schmithüsen, Jim Eckerlein, and many external contributors',
-    "version": (1, 5, 16),
+    "version": (1, 5, 17),
     'blender': (2, 91, 0),
     'location': 'File > Import-Export',
     'description': 'Import-Export as glTF 2.0',
@@ -70,21 +70,42 @@ from bpy_extras.io_utils import ImportHelper, ExportHelper
 extension_panel_unregister_functors = []
 
 
+def ensure_filepath_matches_export_format(filepath, export_format):
+    import os
+    filename = os.path.basename(filepath)
+    if not filename:
+        return filepath
+
+    stem, ext = os.path.splitext(filename)
+    if stem.startswith('.') and not ext:
+        stem, ext = '', stem
+
+    desired_ext = '.glb' if export_format == 'GLB' else '.gltf'
+    ext_lower = ext.lower()
+    if ext_lower not in ['.glb', '.gltf']:
+        return filepath + desired_ext
+    elif ext_lower != desired_ext:
+        filepath = filepath[:-len(ext)]  # strip off ext
+        return filepath + desired_ext
+    else:
+        return filepath
+
+
 def on_export_format_changed(self, context):
-    # Update the file extension when the format (.glb/.gltf) changes
+    # Update the filename in the file browser when the format (.glb/.gltf)
+    # changes
     sfile = context.space_data
-    if sfile is None:
-        return # Avoid error when export from background
-    operator = sfile.active_operator
-    if operator.bl_idname != "EXPORT_SCENE_OT_gltf":
+    if not isinstance(sfile, bpy.types.SpaceFileBrowser):
+        return
+    if not sfile.active_operator:
         return
-    if operator.check(context):
-        # Weird hack to force the filepicker to notice filename changed
-        from os.path import basename
-        filepath = operator.filepath
-        bpy.ops.file.filenum(increment=-1)
-        if basename(operator.filepath) != basename(filepath):
-            bpy.ops.file.filenum(increment=1)
+    if sfile.active_operator.bl_idname != "EXPORT_SCENE_OT_gltf":
+        return
+
+    sfile.params.filename = ensure_filepath_matches_export_format(
+        sfile.params.filename,
+        self.export_format,
+    )
 
 
 class ExportGLTF2_Base:
@@ -384,28 +405,12 @@ class ExportGLTF2_Base:
 
     def check(self, _context):
         # Ensure file extension matches format
-        import os
-        filename = os.path.basename(self.filepath)
-        if filename:
-            filepath = self.filepath
-            desired_ext = '.glb' if self.export_format == 'GLB' else '.gltf'
-
-            stem, ext = os.path.splitext(filename)
-            if stem.startswith('.') and not ext:
-                stem, ext = '', stem
-
-            ext_lower = ext.lower()
-            if ext_lower not in ['.glb', '.gltf']:
-                filepath = filepath + desired_ext
-            elif ext_lower != desired_ext:
-                filepath = filepath[:-len(ext)]  # strip off ext
-                filepath += desired_ext
-
-            if filepath != self.filepath:
-                self.filepath = filepath
-                return True
-
-        return False
+        old_filepath = self.filepath
+        self.filepath = ensure_filepath_matches_export_format(
+            self.filepath,
+            self.export_format,
+        )
+        return self.filepath != old_filepath
 
     def invoke(self, context, event):
         settings = context.scene.get(self.scene_key)



More information about the Bf-extensions-cvs mailing list