Newer
Older
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
"author": "Guillaume Bouchard (Guillaum)",
"location": "File > Import-Export > Stl",
"description": "Import-Export STL files",
"wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
"tracker_url": "https://projects.blender.org/tracker/index.php?"
- Import automatically remove the doubles.
- Export can export with/without modifiers applied
Issues:
Import:
- Does not handle the normal of the triangles
- Does not handle endien
if "bpy" in locals():
import imp
if "stl_utils" in locals():
imp.reload(stl_utils)
if "blender_utils" in locals():
imp.reload(blender_utils)
Campbell Barton
committed
from bpy.props import (StringProperty,
BoolProperty,
CollectionProperty,
EnumProperty,
FloatProperty,
)
from bpy_extras.io_utils import (ImportHelper,
ExportHelper,
axis_conversion,
)
Campbell Barton
committed
from bpy.types import Operator, OperatorFileListElement
Campbell Barton
committed
class ImportSTL(Operator, ImportHelper):
"""Load STL triangle mesh data"""
bl_idname = "import_mesh.stl"
bl_label = "Import STL"
filename_ext = ".stl"
filter_glob = StringProperty(
default="*.stl",
options={'HIDDEN'},
)
files = CollectionProperty(
name="File Path",
Campbell Barton
committed
type=OperatorFileListElement,
)
directory = StringProperty(
subtype='DIR_PATH',
)
def execute(self, context):
from . import stl_utils
from . import blender_utils
paths = [os.path.join(self.directory, name.name)
for name in self.files]
if not paths:
paths.append(self.filepath)
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='OBJECT')
if bpy.ops.object.select_all.poll():
bpy.ops.object.select_all(action='DESELECT')
objName = bpy.path.display_name(os.path.basename(path))
tris, pts = stl_utils.read_stl(path)
blender_utils.create_and_link_mesh(objName, tris, pts)
return {'FINISHED'}
Campbell Barton
committed
class ExportSTL(Operator, ExportHelper):
"""Save STL triangle mesh data from the active object"""
bl_idname = "export_mesh.stl"
bl_label = "Export STL"
filename_ext = ".stl"
Campbell Barton
committed
filter_glob = StringProperty(default="*.stl", options={'HIDDEN'})
ascii = BoolProperty(
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,
)
Campbell Barton
committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
axis_forward = EnumProperty(
name="Forward",
items=(('X', "X Forward", ""),
('Y', "Y Forward", ""),
('Z', "Z Forward", ""),
('-X', "-X Forward", ""),
('-Y', "-Y Forward", ""),
('-Z', "-Z Forward", ""),
),
default='Y',
)
axis_up = EnumProperty(
name="Up",
items=(('X', "X Up", ""),
('Y', "Y Up", ""),
('Z', "Z Up", ""),
('-X', "-X Up", ""),
('-Y', "-Y Up", ""),
('-Z', "-Z Up", ""),
),
default='Z',
)
global_scale = FloatProperty(
name="Scale",
min=0.01, max=1000.0,
default=1.0,
)
def execute(self, context):
from . import stl_utils
from . import blender_utils
import itertools
Campbell Barton
committed
from mathutils import Matrix
global_matrix = axis_conversion(to_forward=self.axis_forward,
to_up=self.axis_up,
).to_4x4() * Matrix.Scale(self.global_scale, 4)
Campbell Barton
committed
blender_utils.faces_from_mesh(ob, global_matrix, self.use_mesh_modifiers)
Thomas Dinges
committed
stl_utils.write_stl(self.filepath, faces, self.ascii)
return {'FINISHED'}
def menu_import(self, context):
Campbell Barton
committed
self.layout.operator(ImportSTL.bl_idname, text="Stl (.stl)")
def menu_export(self, context):
default_path = os.path.splitext(bpy.data.filepath)[0] + ".stl"
Campbell Barton
committed
self.layout.operator(ExportSTL.bl_idname, text="Stl (.stl)")
def register():
Campbell Barton
committed
bpy.utils.register_module(__name__)
bpy.types.INFO_MT_file_import.append(menu_import)
bpy.types.INFO_MT_file_export.append(menu_export)
def unregister():
Campbell Barton
committed
bpy.utils.unregister_module(__name__)
bpy.types.INFO_MT_file_import.remove(menu_import)
bpy.types.INFO_MT_file_export.remove(menu_export)
if __name__ == "__main__":
register()