]> git.tdb.fi Git - libs/gl.git/commitdiff
Replace the instance variables of exporters by function parameters
authorMikko Rasa <tdb@tdb.fi>
Wed, 13 Oct 2021 16:34:25 +0000 (19:34 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 13 Oct 2021 16:48:01 +0000 (19:48 +0300)
This is more flexible when exporting a lot of data.  In almost all cases
the variables were only used by the export_to_file function anyway.

blender/io_mspgl/export.py
blender/io_mspgl/export_animation.py
blender/io_mspgl/export_scene.py
blender/io_mspgl/operators.py

index 37813d081a0d1bebf2cf5407431b59dce6de4589..cf4312d2631993aeb37d7e4d9fd925c70952ff1f 100644 (file)
@@ -2,11 +2,7 @@ import os
 import itertools
 
 class DataExporter:
-       def __init__(self):
-               self.collection = False
-               self.shared_resources = True
-
-       def export_to_file(self, context, out_fn):
+       def export_to_file(self, context, out_fn, *, collection=False, shared_resources=False):
                from .util import Progress
                progress = Progress(context)
 
@@ -21,7 +17,7 @@ class DataExporter:
                base, ext = os.path.splitext(base)
 
                refs = dummy_res.collect_references()
-               if not self.shared_resources:
+               if not shared_resources:
                        numbers = {}
                        for r in refs:
                                res_ext = os.path.splitext(r.name)[1]
@@ -32,7 +28,7 @@ class DataExporter:
                                        r.name = base+res_ext
                                numbers[res_ext] = n+1
 
-               if self.collection:
+               if collection:
                        dummy_res.write_collection(out_fn, exclude_self=True)
                else:
                        for r in refs:
index 3f174e549ba8d616542314e27a7cc36830ebae29..ba83cd490686069cb68b9f874c9fa2fc82add84c 100644 (file)
@@ -2,13 +2,8 @@ import math
 import os
 
 class AnimationExporter:
-       def __init__(self):
-               self.export_all = False
-               self.collection = True
-               self.looping_threshold = 0.001
-
-       def export_to_file(self, context, out_fn):
-               if self.export_all:
+       def export_to_file(self, context, out_fn, *, export_all=False, collection=True, looping_threshold=0.001):
+               if export_all:
                        actions = []
                        for o in context.selected_objects:
                                if not o.animation_data:
@@ -24,12 +19,12 @@ class AnimationExporter:
 
                        resources = {}
                        for a in actions:
-                               resources[a.name+".anim"] = self.export_animation(context, a)
+                               resources[a.name+".anim"] = self.export_animation(context, a, looping_threshold=looping_threshold)
 
                        path, base = os.path.split(out_fn)
                        base, ext = os.path.splitext(base)
 
-                       if self.collection:
+                       if collection:
                                from .datafile import Resource
                                dummy = Resource("dummy", "dummy")
                                dummy.references = list(sorted(resources.values(), key=lambda r: r.name))
@@ -48,9 +43,9 @@ class AnimationExporter:
 
                        resource.write_to_file(out_fn)
 
-       def export_animation(self, context, action):
+       def export_animation(self, context, action, *, looping_threshold=0.001):
                from .animation import create_animation_from_action
-               anim = create_animation_from_action(context, action, looping_threshold=self.looping_threshold)
+               anim = create_animation_from_action(context, action, looping_threshold=looping_threshold)
 
                from .datafile import Resource, Statement
                resource = Resource(action.name+".anim", "animation")
index 0ad6769f1dedb0daa1230aa131d0fed229f11db2..45a681c6e888d63e58b615977af599bcc1758987 100644 (file)
@@ -2,18 +2,12 @@ import math
 import os
 
 class SceneExporter:
-       def __init__(self):
-               self.selected_only = False
-               self.visible_only = True
-               self.collection = True
-               self.skip_existing = True
-
-       def export_to_file(self, context, out_fn):
+       def export_to_file(self, context, out_fn, *, selected_only=False, visible_only=True, collection=True, skip_existing=True):
                from .util import Progress
                progress = Progress(context)
 
                from .scene import create_scene_from_current
-               scene = create_scene_from_current(context, selected_only=self.selected_only, visible_only=self.visible_only)
+               scene = create_scene_from_current(context, selected_only=selected_only, visible_only=visible_only)
 
                resources = {}
                self.export_scene_resources(context, scene, resources, progress)
@@ -23,9 +17,9 @@ class SceneExporter:
                path, base = os.path.split(out_fn)
                base, ext = os.path.splitext(base)
 
-               if self.collection:
+               if collection:
                        existing = None
-                       if self.skip_existing:
+                       if skip_existing:
                                existing = lambda r: not os.path.exists(os.path.join(path, r.name))
                        scene_res.write_collection(out_fn, filter=existing)
                else:
index a6fc592307fd5a549e57f6035271d8b2054e5d3b..920f025141cde05add6a40631a902b6777de511e 100644 (file)
@@ -36,9 +36,9 @@ class ExportMspGLData(bpy.types.Operator):
        def execute(self, context):
                from .export import DataExporter
                exporter = DataExporter()
-               exporter.collection = self.collection
-               exporter.shared_resources = self.shared_resources
-               exporter.export_to_file(context, self.filepath)
+               exporter.export_to_file(context, self.filepath,
+                       collection=self.collection,
+                       shared_resources=self.shared_resources)
                return {'FINISHED'}
 
        def draw(self, context):
@@ -66,10 +66,10 @@ class ExportMspGLAnimation(bpy.types.Operator, ExportHelper):
        def execute(self, context):
                from .export_animation import 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)
+               exporter.export_to_file(context, self.filepath,
+                       export_all=self.export_all,
+                       collection=self.collection,
+                       looping_threshold=looping_threshold)
                return {'FINISHED'}
 
        def draw(self, context):
@@ -103,11 +103,11 @@ class ExportMspGLScene(bpy.types.Operator, ExportHelper):
        def execute(self, context):
                from .export_scene import 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)
+               exporter.export_to_file(context, self.filepath,
+                       selected_only=self.selected_only,
+                       visible_only=self.visible_only,
+                       collection=self.collection,
+                       skip_existing=self.skip_existing)
                return {'FINISHED'}
 
        def draw(self, context):