From: Mikko Rasa Date: Sat, 17 Apr 2021 19:17:16 +0000 (+0300) Subject: Move operators to their own file in the Blender exporter X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=358c29e44cff64b8dd7d9d8b643e6e8bdf4b31fa Move operators to their own file in the Blender exporter --- diff --git a/blender/io_mspgl/__init__.py b/blender/io_mspgl/__init__.py index cba8178f..4519d49e 100644 --- a/blender/io_mspgl/__init__.py +++ b/blender/io_mspgl/__init__.py @@ -6,187 +6,29 @@ bl_info = { "description": "Export Msp GL data", "category": "Import-Export" } +files = ("animation", "armature", "datafile", "export", "export_animation", + "export_armature", "export_camera", "export_material", "export_mesh", + "export_object", "export_scene", "export_texture", "material", "mesh", + "operators", "properties", "scene", "util") + 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", "scene", "util": + for sub in files: if sub in locals(): imp.reload(locals()[sub]) -import os import bpy -import bpy_extras - -class ExportHelper(bpy_extras.io_utils.ExportHelper): - def set_extension(self, ext): - ext_changed = (ext!=self.filename_ext) - if ext_changed: - if self.filepath.endswith(self.filename_ext): - self.filepath = self.filepath[:-len(self.filename_ext)] - self.filename_ext = ext - return ext_changed - -class ExportMspGLData(bpy.types.Operator): - bl_idname = "export.mspgl_data" - bl_label = "Export Msp GL data" - bl_description = "Export object data in Msp GL format" - - filepath: bpy.props.StringProperty(name="File path", description="File path for exporting the data", subtype='FILE_PATH') - collection: bpy.props.BoolProperty(name="As a collection", description="Export all data as a single collection file", default=False) - shared_resources: bpy.props.BoolProperty(name="Shared resources", description="Use global names for resource files to enable sharing", default=True) - - @classmethod - def poll(cls, context): - return len(context.selected_objects)>0 - - def invoke(self, context, event): - blend_filepath = context.blend_data.filepath - if blend_filepath: - self.filepath = os.path.splitext(blend_filepath)[0]+".mdc" - else: - self.filepath = "data.mdc" - context.window_manager.fileselect_add(self) - return {'RUNNING_MODAL'} - - 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) - return {'FINISHED'} - - def draw(self, context): - col = self.layout.column() - col.label(text="Files") - col.prop(self, "collection") - col.prop(self, "shared_resources") - -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" - - filename_ext = ".anim" - - export_all: bpy.props.BoolProperty(name="Export all animations", description="Export all animations present on the selected objects' NLA tracks") - collection: bpy.props.BoolProperty(name="As a collection", description="Export the animations as a single collection file", default=True) - looping_threshold: bpy.props.FloatProperty(name="Looping threshold", description="Tolerance for curve beginning and end values for looping", min=0.0, soft_max=1.0, precision=4, default=0.001) - - def check(self, context): - ext_changed = self.set_extension(".mdc" if self.export_all and self.collection else ".anim") - super_result = super().check(context) - return ext_changed or super_result - - 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) - return {'FINISHED'} - - def draw(self, context): - col = self.layout.column() - col.prop(self, "export_all") - if self.export_all: - col.prop(self, "collection") - col.prop(self, "looping_threshold") - -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", default=False) - visible_only: bpy.props.BoolProperty(name="Visible only", description="Only export objects in visible collections", 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 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 - 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, "collection") - if self.collection: - col.prop(self, "skip_existing") - -class ExportMspGLProject(bpy.types.Operator): - bl_idname = "export.mspgl_project" - bl_label = "Export Msp GL project" - bl_description = "Export the entire project in Msp GL format" - - directory: bpy.props.StringProperty(name="Directory", description="Directory for exporting the data", subtype='FILE_PATH') - - def invoke(self, context, event): - blend_filepath = context.blend_data.filepath - if blend_filepath: - self.directory = os.path.split(blend_filepath)[0] - context.window_manager.fileselect_add(self) - return {'RUNNING_MODAL'} - - def execute(self, context): - from .export import ProjectExporter - exporter = ProjectExporter() - exporter.export_to_directory(context, self.directory) - return {'FINISHED'} - -class AddUniform(bpy.types.Operator): - bl_idname = "material.add_uniform" - bl_label = "Add Uniform" - bl_description = "Add a new uniform value to the material" - - def execute(self, context): - mat = context.active_object.active_material - mat.uniforms.add() - mat.active_uniform_index = len(mat.uniforms)-1 - - return {"FINISHED"} - -class RemoveUniform(bpy.types.Operator): - bl_idname = "material.remove_uniform" - bl_label = "Remove Uniform" - bl_description = "Remove the selected uniform from the material" - - def execute(self, context): - mat = context.active_object.active_material - mat.uniforms.remove(mat.active_uniform_index) - mat.active_uniform_index = min(mat.active_uniform_index, len(mat.uniforms)-1) - - return {"FINISHED"} def menu_func_export(self, context): + from .operators import ExportMspGLData, ExportMspGLAnimation, ExportMspGLScene, ExportMspGLProject self.layout.operator(ExportMspGLData.bl_idname, text="Msp GL data") self.layout.operator(ExportMspGLAnimation.bl_idname, text="Msp GL animation") self.layout.operator(ExportMspGLScene.bl_idname, text="Msp GL scene") self.layout.operator(ExportMspGLProject.bl_idname, text="Msp GL project") -classes = [ExportMspGLData, ExportMspGLAnimation, ExportMspGLScene, ExportMspGLProject, AddUniform, RemoveUniform] - def register(): - for c in classes: - bpy.utils.register_class(c) + from .operators import register_operators + register_operators() bpy.types.TOPBAR_MT_file_export.append(menu_func_export) @@ -194,8 +36,8 @@ def register(): register_properties() def unregister(): - for c in classes: - bpy.utils.unregister_class(c) + from .operators import unregister_operators + unregister_operators() bpy.types.TOPBAR_MT_file_export.remove(menu_func_export) diff --git a/blender/io_mspgl/operators.py b/blender/io_mspgl/operators.py new file mode 100644 index 00000000..8d214eac --- /dev/null +++ b/blender/io_mspgl/operators.py @@ -0,0 +1,173 @@ +import os +import bpy +import bpy_extras + +class ExportHelper(bpy_extras.io_utils.ExportHelper): + def set_extension(self, ext): + ext_changed = (ext!=self.filename_ext) + if ext_changed: + if self.filepath.endswith(self.filename_ext): + self.filepath = self.filepath[:-len(self.filename_ext)] + self.filename_ext = ext + return ext_changed + +class ExportMspGLData(bpy.types.Operator): + bl_idname = "export.mspgl_data" + bl_label = "Export Msp GL data" + bl_description = "Export object data in Msp GL format" + + filepath: bpy.props.StringProperty(name="File path", description="File path for exporting the data", subtype='FILE_PATH') + collection: bpy.props.BoolProperty(name="As a collection", description="Export all data as a single collection file", default=False) + shared_resources: bpy.props.BoolProperty(name="Shared resources", description="Use global names for resource files to enable sharing", default=True) + + @classmethod + def poll(cls, context): + return len(context.selected_objects)>0 + + def invoke(self, context, event): + blend_filepath = context.blend_data.filepath + if blend_filepath: + self.filepath = os.path.splitext(blend_filepath)[0]+".mdc" + else: + self.filepath = "data.mdc" + context.window_manager.fileselect_add(self) + return {'RUNNING_MODAL'} + + 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) + return {'FINISHED'} + + def draw(self, context): + col = self.layout.column() + col.label(text="Files") + col.prop(self, "collection") + col.prop(self, "shared_resources") + +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" + + filename_ext = ".anim" + + export_all: bpy.props.BoolProperty(name="Export all animations", description="Export all animations present on the selected objects' NLA tracks") + collection: bpy.props.BoolProperty(name="As a collection", description="Export the animations as a single collection file", default=True) + looping_threshold: bpy.props.FloatProperty(name="Looping threshold", description="Tolerance for curve beginning and end values for looping", min=0.0, soft_max=1.0, precision=4, default=0.001) + + def check(self, context): + ext_changed = self.set_extension(".mdc" if self.export_all and self.collection else ".anim") + super_result = super().check(context) + return ext_changed or super_result + + 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) + return {'FINISHED'} + + def draw(self, context): + col = self.layout.column() + col.prop(self, "export_all") + if self.export_all: + col.prop(self, "collection") + col.prop(self, "looping_threshold") + +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", default=False) + visible_only: bpy.props.BoolProperty(name="Visible only", description="Only export objects in visible collections", 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 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 + 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, "collection") + if self.collection: + col.prop(self, "skip_existing") + +class ExportMspGLProject(bpy.types.Operator): + bl_idname = "export.mspgl_project" + bl_label = "Export Msp GL project" + bl_description = "Export the entire project in Msp GL format" + + directory: bpy.props.StringProperty(name="Directory", description="Directory for exporting the data", subtype='FILE_PATH') + + def invoke(self, context, event): + blend_filepath = context.blend_data.filepath + if blend_filepath: + self.directory = os.path.split(blend_filepath)[0] + context.window_manager.fileselect_add(self) + return {'RUNNING_MODAL'} + + def execute(self, context): + from .export import ProjectExporter + exporter = ProjectExporter() + exporter.export_to_directory(context, self.directory) + return {'FINISHED'} + +class AddUniform(bpy.types.Operator): + bl_idname = "material.add_uniform" + bl_label = "Add Uniform" + bl_description = "Add a new uniform value to the material" + + def execute(self, context): + mat = context.active_object.active_material + mat.uniforms.add() + mat.active_uniform_index = len(mat.uniforms)-1 + + return {"FINISHED"} + +class RemoveUniform(bpy.types.Operator): + bl_idname = "material.remove_uniform" + bl_label = "Remove Uniform" + bl_description = "Remove the selected uniform from the material" + + def execute(self, context): + mat = context.active_object.active_material + mat.uniforms.remove(mat.active_uniform_index) + mat.active_uniform_index = min(mat.active_uniform_index, len(mat.uniforms)-1) + + return {"FINISHED"} + +classes = [ExportMspGLData, ExportMspGLAnimation, ExportMspGLScene, ExportMspGLProject, AddUniform, RemoveUniform] + +def register_operators(): + for c in classes: + bpy.utils.register_class(c) + +def unregister_operators(): + for c in classes: + bpy.utils.unregister_class(c)