From ffeb9a14765703d3d2e73cec751f5099e0d4c341 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 13 Oct 2021 19:34:25 +0300 Subject: [PATCH] Replace the instance variables of exporters by function parameters 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 | 10 +++------- blender/io_mspgl/export_animation.py | 17 ++++++----------- blender/io_mspgl/export_scene.py | 14 ++++---------- blender/io_mspgl/operators.py | 24 ++++++++++++------------ 4 files changed, 25 insertions(+), 40 deletions(-) diff --git a/blender/io_mspgl/export.py b/blender/io_mspgl/export.py index 37813d08..cf4312d2 100644 --- a/blender/io_mspgl/export.py +++ b/blender/io_mspgl/export.py @@ -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: diff --git a/blender/io_mspgl/export_animation.py b/blender/io_mspgl/export_animation.py index 3f174e54..ba83cd49 100644 --- a/blender/io_mspgl/export_animation.py +++ b/blender/io_mspgl/export_animation.py @@ -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") diff --git a/blender/io_mspgl/export_scene.py b/blender/io_mspgl/export_scene.py index 0ad6769f..45a681c6 100644 --- a/blender/io_mspgl/export_scene.py +++ b/blender/io_mspgl/export_scene.py @@ -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: diff --git a/blender/io_mspgl/operators.py b/blender/io_mspgl/operators.py index a6fc5923..920f0251 100644 --- a/blender/io_mspgl/operators.py +++ b/blender/io_mspgl/operators.py @@ -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): -- 2.43.0