Welcome to mirror list, hosted at ThFree Co, Russian Federation.

__init__.py « io_mesh_stl - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 40f8a363362aa74edeb1adae858f702d5bc82eb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
# ##### 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 #####

bl_addon_info = {
    "name": "STL format",
    "author": "Guillaume Bouchard (Guillaum)",
    "version": (1,),
    "blender": (2, 5, 3),
    "api": 31667,
    "location": "File > Import/Export > Stl",
    "description": "Import/Export STL files",
    "warning": "",
    "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/"\
        "Scripts/File I-O/STL",
    "tracker_url": "https://projects.blender.org/tracker/index.php?"
        "func=detail&aid=22837&group_id=153&atid=469",
    "category": "Import/Export"}

# @todo write the wiki page

"""
Import/Export STL files (binary or ascii)

- 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
"""

import itertools
import os

import bpy
from bpy.props import *


try:
    init_data

    reload(stl_utils)
    reload(blender_utils)
except:
    from io_mesh_stl import stl_utils
    from io_mesh_stl import blender_utils

init_data = True


class StlImporter(bpy.types.Operator):
    '''
    Load STL triangle mesh data
    '''
    bl_idname = "import_mesh.stl"
    bl_label = "Import STL"

    files = CollectionProperty(name="File Path",
                          description="File path used for importing "
                                      "the STL file",
                          type=bpy.types.OperatorFileListElement)

    directory = StringProperty()

    def execute(self, context):
        paths = (os.path.join(self.directory, name.name) for name in self.files)

        for path in paths:
            objName = bpy.path.display_name(path.split("\\")[-1].split("/")[-1])
            tris, pts = stl_utils.read_stl(path)

            blender_utils.create_and_link_mesh(objName, tris, pts)

        return {'FINISHED'}

    def invoke(self, context, event):
        wm = context.window_manager
        wm.add_fileselect(self)

        return {'RUNNING_MODAL'}


class StlExporter(bpy.types.Operator):
    '''
    Save STL triangle mesh data from the active object
    '''
    bl_idname = "export_mesh.stl"
    bl_label = "Export STL"

    filepath = StringProperty(name="File Path",
                          description="File path used for exporting "
                                      "the active object to STL file",
                          maxlen=1024,
                          default="")
    check_existing = BoolProperty(name="Check Existing",
                                  description="Check and warn on "
                                              "overwriting existing files",
                                  default=True,
                                  options={'HIDDEN'})

    ascii = BoolProperty(name="Ascii",
                         description="Save the file in ASCII file format",
                         default=False)
    apply_modifiers = BoolProperty(name="Apply Modifiers",
                                   description="Apply the modifiers "
                                               "before saving",
                                   default=True)

    def execute(self, context):
        faces = itertools.chain.from_iterable(
            blender_utils.faces_from_mesh(ob, self.apply_modifiers)
            for ob in context.selected_objects)

        stl_utils.write_stl(self.filepath, faces, self.ascii)

        return {'FINISHED'}

    def invoke(self, context, event):
        wm = context.window_manager
        wm.add_fileselect(self)
        return {'RUNNING_MODAL'}


def menu_import(self, context):
    self.layout.operator(StlImporter.bl_idname,
                         text="Stl (.stl)").filepath = "*.stl"


def menu_export(self, context):
    default_path = os.path.splitext(bpy.data.filepath)[0] + ".stl"
    self.layout.operator(StlExporter.bl_idname,
                         text="Stl (.stl)").filepath = default_path


def register():
    bpy.types.INFO_MT_file_import.append(menu_import)
    bpy.types.INFO_MT_file_export.append(menu_export)


def unregister():
    bpy.types.INFO_MT_file_import.remove(menu_import)
    bpy.types.INFO_MT_file_export.remove(menu_export)


if __name__ == "__main__":
    register()