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

mesh_auto_mirror.py - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 032bff37950501168e73a277ddf63c990e2bf719 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# ########################################################### #
#   An simple add-on to auto cut in two and mirror an object  #
#   Actualy partialy uncommented (see further version)        #
#   Author: Lapineige                                         #
#   License: GPL v3                                           #
# ########################################################### #

bl_info = {
    "name": "Auto Mirror",
    "description": "Super fast cutting and mirroring for Mesh objects",
    "author": "Lapineige",
    "version": (2, 4, 2),
    "blender": (2, 7, 1),
    "location": "View 3D > Toolbar > Tools tab > AutoMirror (panel)",
    "warning": "",
    "wiki_url": "https://wiki.blender.org/index.php/Extensions:2.6/"
                "Py/Scripts/Modeling/AutoMirror",
    "category": "Mesh"}


import bpy
from bpy.props import (
        BoolProperty,
        EnumProperty,
        FloatProperty,
        PointerProperty,
        )
from bpy.types import (
        Operator,
        Panel,
        PropertyGroup,
        )
from mathutils import Vector


# Operators
class AlignVertices(Operator):
    bl_idname = "object.align_vertices"
    bl_label = "Align Vertices on an Axis"
    bl_description = ("Align Vertices on an Axis\n"
                      "Needs an Active Mesh Object")

    @classmethod
    def poll(cls, context):
        obj = context.active_object
        return obj and obj.type == "MESH"

    def execute(self, context):
        auto_m = context.scene.auto_mirror
        bpy.ops.object.mode_set(mode='OBJECT')

        x1, y1, z1 = bpy.context.scene.cursor_location
        bpy.ops.view3d.snap_cursor_to_selected()

        x2, y2, z2 = bpy.context.scene.cursor_location

        bpy.context.scene.cursor_location[0], \
        bpy.context.scene.cursor_location[1], \
        bpy.context.scene.cursor_location[2] = 0, 0, 0

        # Vertices coordinate to 0 (local coordinate, so on the origin)
        for vert in bpy.context.object.data.vertices:
            if vert.select:
                if auto_m.axis == 'x':
                    axis = 0
                elif auto_m.axis == 'y':
                    axis = 1
                elif auto_m.axis == 'z':
                    axis = 2
                vert.co[axis] = 0

        bpy.context.scene.cursor_location = x2, y2, z2
        bpy.ops.object.origin_set(type='ORIGIN_CURSOR')

        bpy.context.scene.cursor_location = x1, y1, z1
        bpy.ops.object.mode_set(mode='EDIT')

        return {'FINISHED'}


class AutoMirror(Operator):
    bl_idname = "object.automirror"
    bl_label = "AutoMirror"
    bl_description = ("Automatically cut an object along an axis\n"
                      "Needs an Active Mesh Object")
    bl_options = {'REGISTER'}

    @classmethod
    def poll(cls, context):
        obj = context.active_object
        return obj and obj.type == "MESH"

    def get_local_axis_vector(self, context, X, Y, Z, orientation):
        loc = context.object.location
        bpy.ops.object.mode_set(mode="OBJECT")  # Needed to avoid to translate vertices

        v1 = Vector((loc[0], loc[1], loc[2]))
        bpy.ops.transform.translate(
                value=(X * orientation, Y * orientation, Z * orientation),
                constraint_axis=((X == 1), (Y == 1), (Z == 1)),
                constraint_orientation='LOCAL'
                )
        v2 = Vector((loc[0], loc[1], loc[2]))
        bpy.ops.transform.translate(
                value=(-X * orientation, -Y * orientation, -Z * orientation),
                constraint_axis=((X == 1), (Y == 1), (Z == 1)),
                constraint_orientation='LOCAL'
                )

        bpy.ops.object.mode_set(mode="EDIT")
        return v2 - v1

    def execute(self, context):
        auto_m = context.scene.auto_mirror

        X, Y, Z = 0, 0, 0
        if auto_m.axis == 'x':
            X = 1
        elif auto_m.axis == 'y':
            Y = 1
        elif auto_m.axis == 'z':
            Z = 1

        current_mode = bpy.context.object.mode    # Save the current mode

        if bpy.context.object.mode != "EDIT":
            bpy.ops.object.mode_set(mode="EDIT")  # Go to edit mode

        bpy.ops.mesh.select_all(action='SELECT')  # Select all the vertices
        if auto_m.orientation == 'positive':
            orientation = 1
        else:
            orientation = -1
        cut_normal = self.get_local_axis_vector(context, X, Y, Z, orientation)

        # Cut the mesh
        bpy.ops.mesh.bisect(
                plane_co=(
                    bpy.context.object.location[0],
                    bpy.context.object.location[1],
                    bpy.context.object.location[2]
                    ),
                plane_no=cut_normal,
                use_fill=False,
                clear_inner=auto_m.cut,
                clear_outer=0,
                threshold=auto_m.threshold
                )

        # Use to align the vertices on the origin, needed by the "threshold"
        bpy.ops.object.align_vertices()

        if not auto_m.toggle_edit:
            bpy.ops.object.mode_set(mode=current_mode)  # Reload previous mode

        if auto_m.cut:
            bpy.ops.object.modifier_add(type='MIRROR')  # Add a mirror modifier
            bpy.context.object.modifiers[-1].use_x = X  # Choose the axis to use, based on the cut's axis
            bpy.context.object.modifiers[-1].use_y = Y
            bpy.context.object.modifiers[-1].use_z = Z
            bpy.context.object.modifiers[-1].use_clip = auto_m.use_clip
            bpy.context.object.modifiers[-1].show_on_cage = auto_m.show_on_cage

            if auto_m.apply_mirror:
                bpy.ops.object.mode_set(mode='OBJECT')
                bpy.ops.object.modifier_apply(
                        apply_as='DATA',
                        modifier=bpy.context.object.modifiers[-1].name
                        )
                if auto_m.toggle_edit:
                    bpy.ops.object.mode_set(mode='EDIT')
                else:
                    bpy.ops.object.mode_set(mode=current_mode)

        return {'FINISHED'}


# Panel
class BisectMirror(Panel):
    bl_label = "Auto Mirror"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_category = "Tools"
    bl_options = {"DEFAULT_CLOSED"}

    def draw(self, context):
        layout = self.layout
        auto_m = context.scene.auto_mirror
        obj = context.active_object

        if obj and obj.type == 'MESH':
            layout.operator("object.automirror", icon="MOD_MIRROR")
            layout.label("Options:")
            layout.prop(auto_m, "axis", text="Mirror Axis", expand=True)
            layout.prop(auto_m, "orientation", text="Orientation")
            layout.prop(auto_m, "threshold", text="Threshold")
            layout.prop(auto_m, "toggle_edit", text="Toggle Edit")
            layout.prop(auto_m, "cut", text="Cut and Mirror", toggle=True, icon="MOD_REMESH")

            if auto_m.cut:
                col = layout.column(align=True)
                row = col.row(align=True)
                row.prop(auto_m, "use_clip", text="Use Clip", toggle=True)
                row.prop(auto_m, "show_on_cage", text="Editable", toggle=True)
                col.prop(auto_m, "apply_mirror", text="Apply Mirror", toggle=True)
        else:
            layout.label(icon="INFO", text="No Mesh selected")


class AutoMirrorProperties(PropertyGroup):
    axis = EnumProperty(
            name="Axis",
            items=[
                ("x", "X", "", 1),
                ("y", "Y", "", 2),
                ("z", "Z", "", 3)
                ],
            description="Axis used by the mirror modifier"
            )
    orientation = EnumProperty(
            name="Orientation",
            items=[
                ("positive", "Positive", "", 1),
                ("negative", "Negative", "", 2)
                ],
            description="Choose the side along the axis of the editable part (+/- coordinates)"
            )
    threshold = FloatProperty(
            default=0.001,
            min=0.001,
            description="Vertices closer than this distance are merged on the loopcut"
            )
    toggle_edit = BoolProperty(
            name="Toggle Edit Mode",
            default=True,
            description="If not in Edit mode, change mode to it"
            )
    cut = BoolProperty(
            name="Cut",
            default=True,
            description="If enabled, cut the mesh in two parts and mirror it\n"
                        "If not, just make a loopcut"
            )
    clipping = BoolProperty(
            default=True
            )
    use_clip = BoolProperty(
            default=True,
            description="Use clipping for the mirror modifier"
            )
    show_on_cage = BoolProperty(
            default=True,
            description="Enable editing the cage (it's the classical modifier's option)"
            )
    apply_mirror = BoolProperty(
            description="Apply the mirror modifier (useful to symmetrise the mesh)"
            )


def register():
    bpy.utils.register_class(BisectMirror)
    bpy.utils.register_class(AutoMirror)
    bpy.utils.register_class(AlignVertices)
    bpy.utils.register_class(AutoMirrorProperties)
    bpy.types.Scene.auto_mirror = PointerProperty(
                                        type=AutoMirrorProperties
                                        )


def unregister():
    bpy.utils.unregister_class(BisectMirror)
    bpy.utils.unregister_class(AutoMirror)
    bpy.utils.unregister_class(AlignVertices)
    bpy.utils.unregister_class(AutoMirrorProperties)
    del bpy.types.Scene.auto_mirror


if __name__ == "__main__":
    register()