]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/__init__.py
Refactor ExportMspGLBase into a simpler ExportHelper subclass
[libs/gl.git] / blender / io_mspgl / __init__.py
index 13375e2426795d4995e1505987210dd3c4f75476..79575f7df23b2c80761e24c09f499f68b7f45ee6 100644 (file)
@@ -8,17 +8,15 @@ bl_info = {
 
 if "bpy" in locals():
        import imp
-       for sub in "animation", "armature", "datafile", "export", "export_animation", "export_armature", "export_camera", "export_material", "export_mesh", "export_object", "export_scene", "export_texture", "material", "mesh", "properties", "util":
+       for sub in "animation", "armature", "datafile", "export", "export_animation", "export_armature", "export_camera", "export_material", "export_mesh", "export_object", "export_scene", "export_texture", "material", "mesh", "properties", "scene", "util":
                if sub in locals():
                        imp.reload(locals()[sub])
 
 import os
 import bpy
-from bpy_extras.io_utils import ExportHelper
-
-class ExportMspGLBase(ExportHelper):
-       show_progress: bpy.props.BoolProperty(name="Show progress", description="Display progress indicator while exporting", default=True)
+import bpy_extras
 
+class ExportHelper(bpy_extras.io_utils.ExportHelper):
        def set_extension(self, ext):
                ext_changed = (ext!=self.filename_ext)
                if ext_changed:
@@ -27,19 +25,6 @@ class ExportMspGLBase(ExportHelper):
                        self.filename_ext = ext
                return ext_changed
 
-       def execute(self, context):
-               exporter = self.create_exporter()
-               self.prepare_exporter(exporter)
-               exporter.export_to_file(context, self.filepath)
-               return {"FINISHED"}
-
-       def create_exporter(self):
-               raise Exception("create_exporter must be overridden")
-
-       def prepare_exporter(self, exporter):
-               for k, v in self.as_keywords().items():
-                       setattr(exporter, k, v)
-
 class ExportMspGLData(bpy.types.Operator):
        bl_idname = "export.mspgl_data"
        bl_label = "Export Msp GL data"
@@ -76,7 +61,7 @@ class ExportMspGLData(bpy.types.Operator):
                col.prop(self, "collection")
                col.prop(self, "shared_resources")
 
-class ExportMspGLAnimation(bpy.types.Operator, ExportMspGLBase):
+class ExportMspGLAnimation(bpy.types.Operator, ExportHelper):
        bl_idname = "export.mspgl_animation"
        bl_label = "Export Msp GL animation"
        bl_description = "Export one or more animations in Msp GL format"
@@ -92,9 +77,14 @@ class ExportMspGLAnimation(bpy.types.Operator, ExportMspGLBase):
                super_result = super().check(context)
                return ext_changed or super_result
 
-       def create_exporter(self):
+       def execute(self, context):
                from .export_animation import AnimationExporter
-               return AnimationExporter()
+               exporter = AnimationExporter()
+               exporter.export_all = self.export_all
+               exporter.collection = self.collection
+               exporter.looping_threshold = self.looping_threshold
+               exporter.export_to_file(context, self.filepath)
+               return {'FINISHED'}
 
        def draw(self, context):
                col = self.layout.column()
@@ -103,28 +93,43 @@ class ExportMspGLAnimation(bpy.types.Operator, ExportMspGLBase):
                        col.prop(self, "collection")
                col.prop(self, "looping_threshold")
 
-class ExportMspGLScene(bpy.types.Operator, ExportMspGLBase):
+class ExportMspGLScene(bpy.types.Operator, ExportHelper):
        bl_idname = "export_scene.mspgl_scene"
        bl_label = "Export Msp GL scene"
        bl_description = "Export the active scene in Msp GL format"
 
        filename_ext = ".scene"
 
-       selected_only: bpy.props.BoolProperty(name="Selected objects only", description="Only export the selected objects")
+       selected_only: bpy.props.BoolProperty(name="Selected objects only", description="Only export the selected objects", default=False)
        visible_only: bpy.props.BoolProperty(name="Visible only", description="Only export objects in visible collections", default=True)
-       resource_collection: bpy.props.BoolProperty(name="Resource collection", description="Put resources to a single collection file", default=True)
+       collection: bpy.props.BoolProperty(name="As a collection", description="Export the scene and all resources as a single collection file", default=False)
        skip_existing: bpy.props.BoolProperty(name="Skip existing files", description="Skip resources that already exist as files", default=True)
 
-       def create_exporter(self):
+       def invoke(self, context, event):
+               self.filepath = context.scene.name+".scene"
+               return super().invoke(context, event)
+
+       def check(self, context):
+               ext_changed = self.set_extension(".mdc" if self.collection else ".scene")
+               super_result = super().check(context)
+               return ext_changed or super_result
+
+       def execute(self, context):
                from .export_scene import SceneExporter
-               return SceneExporter()
+               exporter = SceneExporter()
+               exporter.selected_only = self.selected_only
+               exporter.visible_only = self.visible_only
+               exporter.collection = self.collection
+               exporter.skip_existing = self.skip_existing
+               exporter.export_to_file(context, self.filepath)
+               return {'FINISHED'}
 
        def draw(self, context):
                col = self.layout.column()
                col.prop(self, "selected_only")
                col.prop(self, "visible_only")
-               col.prop(self, "resource_collection")
-               if self.resource_collection:
+               col.prop(self, "collection")
+               if self.collection:
                        col.prop(self, "skip_existing")
 
 class AddUniform(bpy.types.Operator):