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

object_mangle_tools.py « add_advanced_objects_panels - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c36bb22484df7d2248e9bd5d92a1d7e3732ddd15 (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
# mangle_tools.py (c) 2011 Phil Cote (cotejrp1)

# ###### 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 LICENCE BLOCK ######

# Note: properties are moved into __init__

bl_info = {
    "name": "Mangle Tools",
    "author": "Phil Cote",
    "blender": (2, 71, 0),
    "location": "View3D > Toolshelf > Tools Tab",
    "description": "Set of tools to mangle curves, meshes, and shape keys",
    "warning": "",
    "wiki_url": "",
    "category": "Object"}


import bpy
import random
from bpy.types import (
        Operator,
        Panel,
        )
import time
from math import pi
import bmesh


def move_coordinate(context, co, is_curve=False):
    advanced_objects = context.scene.advanced_objects
    xyz_const = advanced_objects.mangle_constraint_vector
    random.seed(time.time())
    multiplier = 1

    # For curves, we base the multiplier on the circumference formula.
    # This helps make curve changes more noticable.
    if is_curve:
        multiplier = 2 * pi
    random_mag = advanced_objects.mangle_random_magnitude
    if xyz_const[0]:
        co.x += .01 * random.randrange(-random_mag, random_mag) * multiplier
    if xyz_const[1]:
        co.y += .01 * random.randrange(-random_mag, random_mag) * multiplier
    if xyz_const[2]:
        co.z += .01 * random.randrange(-random_mag, random_mag) * multiplier


class MeshManglerOperator(Operator):
    bl_idname = "ba.mesh_mangler"
    bl_label = "Mangle Mesh"
    bl_description = ("Push vertices on the selected object around in random\n"
                      "directions to create a crumpled look")
    bl_options = {"REGISTER", "UNDO"}

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

    def execute(self, context):
        mesh = context.active_object.data
        bm = bmesh.new()
        bm.from_mesh(mesh)
        verts = bm.verts
        advanced_objects = context.scene.advanced_objects
        randomMag = advanced_objects.mangle_random_magnitude
        random.seed(time.time())

        if mesh.shape_keys is not None:
            self.report({'INFO'},
                        "Cannot mangle mesh: Shape keys present. Operation Cancelled")
            return {'CANCELLED'}

        for vert in verts:
            xVal = .01 * random.randrange(-randomMag, randomMag)
            yVal = .01 * random.randrange(-randomMag, randomMag)
            zVal = .01 * random.randrange(-randomMag, randomMag)

            vert.co.x = vert.co.x + xVal
            vert.co.y = vert.co.y + yVal
            vert.co.z = vert.co.z + zVal

        del verts

        bm.to_mesh(mesh)
        mesh.update()

        return {'FINISHED'}


class AnimanglerOperator(Operator):
    bl_idname = "ba.ani_mangler"
    bl_label = "Mangle Shape Key"
    bl_description = ("Make a shape key and pushes the verts around on it\n"
                      "to set up for random pulsating animation")

    @classmethod
    def poll(cls, context):
        ob = context.active_object
        return ob is not None and ob.type in ['MESH', 'CURVE']

    def execute(self, context):
        scn = context.scene.advanced_objects
        mangleName = scn.mangle_name
        ob = context.object
        shapeKey = ob.shape_key_add(name=mangleName)
        verts = shapeKey.data

        for vert in verts:
            move_coordinate(context, vert.co, is_curve=ob.type == 'CURVE')

        return {'FINISHED'}


class CurveManglerOp(Operator):
    bl_idname = "ba.curve_mangler"
    bl_label = "Mangle Curve"
    bl_description = "Mangle a curve to the degree the user specifies"
    bl_options = {'REGISTER', 'UNDO'}

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

    def execute(self, context):
        ob = context.active_object
        if ob.data.shape_keys is not None:
            self.report({'INFO'},
                        "Cannot mangle curve. Shape keys present. Operation Cancelled")
            return {'CANCELLED'}

        splines = context.object.data.splines

        for spline in splines:
            if spline.type == 'BEZIER':
                points = spline.bezier_points
            elif spline.type in {'POLY', 'NURBS'}:
                points = spline.points

            for point in points:
                move_coordinate(context, point.co, is_curve=True)

        return {'FINISHED'}


class MangleToolsPanel(Panel):
    bl_label = "Mangle Tools"
    bl_space_type = "VIEW_3D"
    bl_context = "objectmode"
    bl_region_type = "TOOLS"
    bl_category = "Create"
    bl_options = {'DEFAULT_CLOSED'}

    def draw(self, context):
        scn = context.scene.advanced_objects
        obj = context.object

        if obj and obj.type in ['MESH']:
            layout = self.layout

            row = layout.row(align=True)
            row.prop(scn, "mangle_constraint_vector", toggle=True)

            col = layout.column()
            col.prop(scn, "mangle_random_magnitude")
            col.operator("ba.mesh_mangler")
            col.separator()

            col.prop(scn, "mangle_name")
            col.operator("ba.ani_mangler")
        else:
            layout = self.layout
            layout.label(text="Please select a Mesh Object", icon="INFO")


def register():
    bpy.utils.register_class(AnimanglerOperator)
    bpy.utils.register_class(MeshManglerOperator)
    bpy.utils.register_class(CurveManglerOp)
    bpy.utils.register_class(MangleToolsPanel)


def unregister():
    bpy.utils.unregister_class(AnimanglerOperator)
    bpy.utils.unregister_class(MeshManglerOperator)
    bpy.utils.unregister_class(MangleToolsPanel)
    bpy.utils.unregister_class(CurveManglerOp)


if __name__ == "__main__":
    register()