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

mesh_easylattice.py « add_advanced_objects_menu - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 91a167dce668092eeee6e5845816da6675390e14 (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# ##### 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 #####

# TODO: find a better solution for allowing more than one lattice per scene

bl_info = {
    "name": "Easy Lattice Object",
    "author": "Kursad Karatas",
    "version": (0, 5, 1),
    "blender": (2, 66, 0),
    "location": "View3D > Easy Lattice",
    "description": "Create a lattice for shape editing",
    "warning": "",
    "wiki_url": "https://wiki.blender.org/index.php/Easy_Lattice_Editing_Addon",
    "tracker_url": "https://bitbucket.org/kursad/blender_addons_easylattice/src",
    "category": "Mesh"}


import bpy
from mathutils import (
        Matrix,
        Vector,
        )
from bpy.types import Operator
from bpy.props import (
        EnumProperty,
        IntProperty,
        StringProperty,
        )


# Cleanup
def modifiersDelete(obj):
    for mod in obj.modifiers:
        if mod.name == "latticeeasytemp":
            try:
                if mod.object == bpy.data.objects['LatticeEasytTemp']:
                    bpy.ops.object.modifier_apply(apply_as='DATA', modifier=mod.name)
            except:
                bpy.ops.object.modifier_remove(modifier=mod.name)


def modifiersApplyRemove(obj):
    bpy.ops.object.select_all(action='DESELECT')
    bpy.ops.object.select_pattern(pattern=obj.name, extend=False)
    bpy.context.scene.objects.active = obj

    for mod in obj.modifiers:
        if mod.name == "latticeeasytemp":
            if mod.object == bpy.data.objects['LatticeEasytTemp']:
                bpy.ops.object.modifier_apply(apply_as='DATA', modifier=mod.name)


def latticeDelete(obj):
    bpy.ops.object.select_all(action='DESELECT')
    for ob in bpy.context.scene.objects:
        if "LatticeEasytTemp" in ob.name:
            ob.select = True
    bpy.ops.object.delete(use_global=False)

    obj.select = True


def createLattice(obj, size, pos, props):
    # Create lattice and object
    lat = bpy.data.lattices.new('LatticeEasytTemp')
    ob = bpy.data.objects.new('LatticeEasytTemp', lat)

    loc, rot, scl = getTransformations(obj)

    # the position comes from the bbox
    ob.location = pos

    # the size  from bbox
    ob.scale = size

    # the rotation comes from the combined obj world
    # matrix which was converted to euler pairs
    ob.rotation_euler = buildRot_World(obj)

    ob.show_x_ray = True
    # Link object to scene
    scn = bpy.context.scene
    scn.objects.link(ob)
    scn.objects.active = ob
    scn.update()

    # Set lattice attributes
    lat.interpolation_type_u = props[3]
    lat.interpolation_type_v = props[3]
    lat.interpolation_type_w = props[3]

    lat.use_outside = False

    lat.points_u = props[0]
    lat.points_v = props[1]
    lat.points_w = props[2]

    return ob


def selectedVerts_Grp(obj):
    vertices = obj.data.vertices
    selverts = []

    if obj.mode == "EDIT":
        bpy.ops.object.editmode_toggle()

    for grp in obj.vertex_groups:
        if "templatticegrp" in grp.name:
            bpy.ops.object.vertex_group_set_active(group=grp.name)
            bpy.ops.object.vertex_group_remove()

    tempgroup = obj.vertex_groups.new("templatticegrp")

    for vert in vertices:
        if vert.select is True:
            selverts.append(vert)
            tempgroup.add([vert.index], 1.0, "REPLACE")

    return selverts


def getTransformations(obj):
    rot = obj.rotation_euler
    loc = obj.location
    size = obj.scale

    return [loc, rot, size]


def findBBox(obj, selvertsarray):

    mat = buildTrnScl_WorldMat(obj)
    mat_world = obj.matrix_world

    minx, miny, minz = selvertsarray[0].co
    maxx, maxy, maxz = selvertsarray[0].co

    c = 1

    for c in range(len(selvertsarray)):
        co = selvertsarray[c].co

        if co.x < minx:
            minx = co.x
        if co.y < miny:
            miny = co.y
        if co.z < minz:
            minz = co.z

        if co.x > maxx:
            maxx = co.x
        if co.y > maxy:
            maxy = co.y
        if co.z > maxz:
            maxz = co.z
        c += 1

    minpoint = Vector((minx, miny, minz))
    maxpoint = Vector((maxx, maxy, maxz))

    # middle point has to be calculated based on the real world matrix
    middle = ((minpoint + maxpoint) / 2)

    minpoint = mat * minpoint    # Calculate only based on loc/scale
    maxpoint = mat * maxpoint    # Calculate only based on loc/scale
    middle = mat_world * middle  # the middle has to be calculated based on the real world matrix

    size = maxpoint - minpoint
    size = Vector((abs(size.x), abs(size.y), abs(size.z)))

    return [minpoint, maxpoint, size, middle]


def buildTrnSclMat(obj):
    # This function builds a local matrix that encodes translation
    # and scale and it leaves out the rotation matrix
    # The rotation is applied at obejct level if there is any
    mat_trans = Matrix.Translation(obj.location)
    mat_scale = Matrix.Scale(obj.scale[0], 4, (1, 0, 0))
    mat_scale *= Matrix.Scale(obj.scale[1], 4, (0, 1, 0))
    mat_scale *= Matrix.Scale(obj.scale[2], 4, (0, 0, 1))

    mat_final = mat_trans * mat_scale

    return mat_final


def buildTrnScl_WorldMat(obj):
    # This function builds a real world matrix that encodes translation
    # and scale and it leaves out the rotation matrix
    # The rotation is applied at obejct level if there is any
    loc, rot, scl = obj.matrix_world.decompose()
    mat_trans = Matrix.Translation(loc)

    mat_scale = Matrix.Scale(scl[0], 4, (1, 0, 0))
    mat_scale *= Matrix.Scale(scl[1], 4, (0, 1, 0))
    mat_scale *= Matrix.Scale(scl[2], 4, (0, 0, 1))

    mat_final = mat_trans * mat_scale

    return mat_final


# Feature use
def buildRot_WorldMat(obj):
    # This function builds a real world matrix that encodes rotation
    # and it leaves out translation and scale matrices
    loc, rot, scl = obj.matrix_world.decompose()
    rot = rot.to_euler()

    mat_rot = Matrix.Rotation(rot[0], 4, 'X')
    mat_rot *= Matrix.Rotation(rot[1], 4, 'Z')
    mat_rot *= Matrix.Rotation(rot[2], 4, 'Y')
    return mat_rot


def buildTrn_WorldMat(obj):
    # This function builds a real world matrix that encodes translation
    # and scale and it leaves out the rotation matrix
    # The rotation is applied at obejct level if there is any
    loc, rot, scl = obj.matrix_world.decompose()
    mat_trans = Matrix.Translation(loc)

    return mat_trans


def buildScl_WorldMat(obj):
    # This function builds a real world matrix that encodes translation
    # and scale and it leaves out the rotation matrix
    # The rotation is applied at obejct level if there is any
    loc, rot, scl = obj.matrix_world.decompose()

    mat_scale = Matrix.Scale(scl[0], 4, (1, 0, 0))
    mat_scale *= Matrix.Scale(scl[1], 4, (0, 1, 0))
    mat_scale *= Matrix.Scale(scl[2], 4, (0, 0, 1))

    return mat_scale


def buildRot_World(obj):
    # This function builds a real world rotation values
    loc, rot, scl = obj.matrix_world.decompose()
    rot = rot.to_euler()

    return rot


def run(lat_props):
    obj = bpy.context.object

    if obj.type == "MESH":
        # set global property for the currently active latticed object
        # removed in __init__ on unregister if created
        bpy.types.Scene.activelatticeobject = StringProperty(
                name="currentlatticeobject",
                default=""
                )
        bpy.types.Scene.activelatticeobject = obj.name

        modifiersDelete(obj)
        selvertsarray = selectedVerts_Grp(obj)
        bbox = findBBox(obj, selvertsarray)

        size = bbox[2]
        pos = bbox[3]

        latticeDelete(obj)
        lat = createLattice(obj, size, pos, lat_props)

        modif = obj.modifiers.new("latticeeasytemp", "LATTICE")
        modif.object = lat
        modif.vertex_group = "templatticegrp"

        bpy.ops.object.select_all(action='DESELECT')
        bpy.ops.object.select_pattern(pattern=lat.name, extend=False)
        bpy.context.scene.objects.active = lat

        bpy.context.scene.update()
        bpy.ops.object.mode_set(mode='EDIT')

    if obj.type == "LATTICE":
        if bpy.types.Scene.activelatticeobject:
            name = bpy.types.Scene.activelatticeobject

            # Are we in edit lattice mode? If so move on to object mode
            if obj.mode == "EDIT":
                bpy.ops.object.editmode_toggle()

            for ob in bpy.context.scene.objects:
                if ob.name == name:  # found the object with the lattice mod
                    object = ob
                    modifiersApplyRemove(object)
                    latticeDelete(obj)

    return


def main(context, latticeprops):
    run(latticeprops)


class EasyLattice(Operator):
    bl_idname = "object.easy_lattice"
    bl_label = "Easy Lattice Creator"
    bl_description = ("Create a Lattice modifier ready to edit\n"
                      "Needs an existing Active Mesh Object\n"
                      "Note: Works only with one lattice per scene")

    lat_u = IntProperty(
            name="Lattice u",
            description="Points in u direction",
            default=3
            )
    lat_w = IntProperty(
            name="Lattice w",
            description="Points in w direction",
            default=3
            )
    lat_m = IntProperty(
            name="Lattice m",
            description="Points in m direction",
            default=3
            )
    lat_types = (('KEY_LINEAR', "Linear", "Linear Interpolation type"),
                 ('KEY_CARDINAL', "Cardinal", "Cardinal Interpolation type"),
                 ('KEY_BSPLINE', "BSpline", "Key BSpline Interpolation Type")
                )
    lat_type = EnumProperty(
            name="Lattice Type",
            description="Choose Lattice Type",
            items=lat_types,
            default='KEY_LINEAR'
            )

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

    def draw(self, context):
        layout = self.layout

        col = layout.column(align=True)
        col.prop(self, "lat_u")
        col.prop(self, "lat_w")
        col.prop(self, "lat_m")

        layout.prop(self, "lat_type")

    def execute(self, context):
        lat_u = self.lat_u
        lat_w = self.lat_w
        lat_m = self.lat_m

        # enum property no need to complicate things
        lat_type = self.lat_type
        lat_props = [lat_u, lat_w, lat_m, lat_type]
        try:
            main(context, lat_props)

        except Exception as e:
            print("\n[Add Advanced Objects]\nOperator:object.easy_lattice\n{}\n".format(e))
            self.report({'WARNING'},
                         "Easy Lattice Creator could not be completed (See Console for more info)")

            return {"CANCELLED"}

        return {"FINISHED"}

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


def register():
    bpy.utils.register_class(EasyLattice)


def unregister():
    bpy.utils.unregister_class(EasyLattice)


if __name__ == "__main__":
    register()