From 0260327cff34787394cf9aaa3e8cc5bd35cb0a25 Mon Sep 17 00:00:00 2001 From: Mikhail Rachinskiy Date: Tue, 15 Oct 2019 14:09:44 +0400 Subject: STL: cleanup and PEP8 Unused imports, move rare imports inside functions, correct description. --- io_mesh_stl/__init__.py | 183 ++++++++++++++++++++++--------------------- io_mesh_stl/blender_utils.py | 10 ++- io_mesh_stl/stl_utils.py | 26 +++--- 3 files changed, 115 insertions(+), 104 deletions(-) diff --git a/io_mesh_stl/__init__.py b/io_mesh_stl/__init__.py index cd81b228..18246d4d 100644 --- a/io_mesh_stl/__init__.py +++ b/io_mesh_stl/__init__.py @@ -23,9 +23,8 @@ bl_info = { "author": "Guillaume Bouchard (Guillaum)", "version": (1, 1, 3), "blender": (2, 81, 6), - "location": "File > Import-Export > Stl", + "location": "File > Import-Export", "description": "Import-Export STL files", - "warning": "", "wiki_url": "https://docs.blender.org/manual/en/latest/addons/io_mesh_stl.html", "support": 'OFFICIAL', "category": "Import-Export", @@ -53,75 +52,70 @@ if "bpy" in locals(): if "blender_utils" in locals(): importlib.reload(blender_utils) -import os - import bpy from bpy.props import ( - StringProperty, - BoolProperty, - CollectionProperty, - EnumProperty, - FloatProperty, - ) + StringProperty, + BoolProperty, + CollectionProperty, + EnumProperty, + FloatProperty, +) from bpy_extras.io_utils import ( - ImportHelper, - ExportHelper, - orientation_helper, - axis_conversion, - ) + ImportHelper, + ExportHelper, + orientation_helper, + axis_conversion, +) from bpy.types import ( - Operator, - OperatorFileListElement, - ) + Operator, + OperatorFileListElement, +) @orientation_helper(axis_forward='Y', axis_up='Z') class ImportSTL(Operator, ImportHelper): - """Load STL triangle mesh data""" bl_idname = "import_mesh.stl" bl_label = "Import STL" + bl_description = "Load STL triangle mesh data" bl_options = {'UNDO'} filename_ext = ".stl" filter_glob: StringProperty( - default="*.stl", - options={'HIDDEN'}, - ) + default="*.stl", + options={'HIDDEN'}, + ) files: CollectionProperty( - name="File Path", - type=OperatorFileListElement, - ) + name="File Path", + type=OperatorFileListElement, + ) directory: StringProperty( - subtype='DIR_PATH', - ) - + subtype='DIR_PATH', + ) global_scale: FloatProperty( - name="Scale", - soft_min=0.001, soft_max=1000.0, - min=1e-6, max=1e6, - default=1.0, - ) - + name="Scale", + soft_min=0.001, soft_max=1000.0, + min=1e-6, max=1e6, + default=1.0, + ) use_scene_unit: BoolProperty( - name="Scene Unit", - description="Apply current scene's unit (as defined by unit scale) to imported data", - default=False, - ) - + name="Scene Unit", + description="Apply current scene's unit (as defined by unit scale) to imported data", + default=False, + ) use_facet_normal: BoolProperty( - name="Facet Normals", - description="Use (import) facet normals (note that this will still give flat shading)", - default=False, - ) + name="Facet Normals", + description="Use (import) facet normals (note that this will still give flat shading)", + default=False, + ) def execute(self, context): + import os + from mathutils import Matrix from . import stl_utils from . import blender_utils - from mathutils import Matrix - paths = [os.path.join(self.directory, name.name) - for name in self.files] + paths = [os.path.join(self.directory, name.name) for name in self.files] scene = context.scene @@ -130,9 +124,10 @@ class ImportSTL(Operator, ImportHelper): if scene.unit_settings.system != 'NONE' and self.use_scene_unit: global_scale /= scene.unit_settings.scale_length - global_matrix = axis_conversion(from_forward=self.axis_forward, - from_up=self.axis_up, - ).to_4x4() @ Matrix.Scale(global_scale, 4) + global_matrix = axis_conversion( + from_forward=self.axis_forward, + from_up=self.axis_up, + ).to_4x4() @ Matrix.Scale(global_scale, 4) if not paths: paths.append(self.filepath) @@ -209,64 +204,70 @@ class STL_PT_import_geometry(bpy.types.Panel): @orientation_helper(axis_forward='Y', axis_up='Z') class ExportSTL(Operator, ExportHelper): - """Save STL triangle mesh data from the active object""" bl_idname = "export_mesh.stl" bl_label = "Export STL" + bl_description = """Save STL triangle mesh data""" filename_ext = ".stl" filter_glob: StringProperty(default="*.stl", options={'HIDDEN'}) use_selection: BoolProperty( - name="Selection Only", - description="Export selected objects only", - default=False, - ) + name="Selection Only", + description="Export selected objects only", + default=False, + ) global_scale: FloatProperty( - name="Scale", - min=0.01, max=1000.0, - default=1.0, - ) - + name="Scale", + min=0.01, max=1000.0, + default=1.0, + ) use_scene_unit: BoolProperty( - name="Scene Unit", - description="Apply current scene's unit (as defined by unit scale) to exported data", - default=False, - ) + name="Scene Unit", + description="Apply current scene's unit (as defined by unit scale) to exported data", + default=False, + ) ascii: BoolProperty( - name="Ascii", - description="Save the file in ASCII file format", - default=False, - ) + name="Ascii", + description="Save the file in ASCII file format", + default=False, + ) use_mesh_modifiers: BoolProperty( - name="Apply Modifiers", - description="Apply the modifiers before saving", - default=True, - ) + name="Apply Modifiers", + description="Apply the modifiers before saving", + default=True, + ) batch_mode: EnumProperty( - name="Batch Mode", - items=(('OFF', "Off", "All data in one file"), - ('OBJECT', "Object", "Each object as a file"), - )) + name="Batch Mode", + items=( + ('OFF', "Off", "All data in one file"), + ('OBJECT', "Object", "Each object as a file"), + ), + ) @property def check_extension(self): return self.batch_mode == 'OFF' def execute(self, context): - from . import stl_utils - from . import blender_utils + import os import itertools from mathutils import Matrix - keywords = self.as_keywords(ignore=("axis_forward", - "axis_up", - "use_selection", - "global_scale", - "check_existing", - "filter_glob", - "use_scene_unit", - "use_mesh_modifiers", - "batch_mode" - )) + from . import stl_utils + from . import blender_utils + + keywords = self.as_keywords( + ignore=( + "axis_forward", + "axis_up", + "use_selection", + "global_scale", + "check_existing", + "filter_glob", + "use_scene_unit", + "use_mesh_modifiers", + "batch_mode" + ), + ) scene = context.scene if self.use_selection: @@ -279,9 +280,10 @@ class ExportSTL(Operator, ExportHelper): if scene.unit_settings.system != 'NONE' and self.use_scene_unit: global_scale *= scene.unit_settings.scale_length - global_matrix = axis_conversion(to_forward=self.axis_forward, - to_up=self.axis_up, - ).to_4x4() @ Matrix.Scale(global_scale, 4) + global_matrix = axis_conversion( + to_forward=self.axis_forward, + to_up=self.axis_up, + ).to_4x4() @ Matrix.Scale(global_scale, 4) if self.batch_mode == 'OFF': faces = itertools.chain.from_iterable( @@ -424,6 +426,7 @@ classes = ( STL_PT_export_geometry, ) + def register(): for cls in classes: bpy.utils.register_class(cls) diff --git a/io_mesh_stl/blender_utils.py b/io_mesh_stl/blender_utils.py index d1b14cf6..8589e196 100644 --- a/io_mesh_stl/blender_utils.py +++ b/io_mesh_stl/blender_utils.py @@ -18,10 +18,6 @@ # -import bpy -import array -from itertools import chain - def create_and_link_mesh(name, faces, face_nors, points, global_matrix): """ @@ -29,6 +25,10 @@ def create_and_link_mesh(name, faces, face_nors, points, global_matrix): *points* and *faces* and link it in the current scene. """ + import array + from itertools import chain + import bpy + mesh = bpy.data.meshes.new(name) mesh.from_pydata(points, [], faces) @@ -77,6 +77,8 @@ def faces_from_mesh(ob, global_matrix, use_mesh_modifiers=False): Split the quad into two triangles """ + import bpy + # get the editmode data ob.update_from_editmode() diff --git a/io_mesh_stl/stl_utils.py b/io_mesh_stl/stl_utils.py index 0c108e83..ee693375 100644 --- a/io_mesh_stl/stl_utils.py +++ b/io_mesh_stl/stl_utils.py @@ -26,14 +26,9 @@ Used as a blender script, it load all the stl files in the scene: blender --python stl_utils.py -- file1.stl file2.stl file3.stl ... """ -import os -import struct -import contextlib -import itertools -from mathutils.geometry import normal - # TODO: endien + class ListDict(dict): """ Set struct with order. @@ -88,6 +83,10 @@ def _is_ascii_file(data): represents a binary file. It can be a (very *RARE* in real life, but can easily be forged) ascii file. """ + + import os + import struct + # Skip header... data.seek(BINARY_HEADER) size = struct.unpack('