Simplifying material check thanks to the teachings of Master Campbell
[blender-addons.git] / mesh_relax.py
bloba9e476b7da5d17e3e8df237774831d84204e586d
1 # mesh_relax.py Copyright (C) 2010, Fabian Fricke
3 # Relaxes selected vertices while retaining the shape as much as possible
5 # ***** BEGIN GPL LICENSE BLOCK *****
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software Foundation,
20 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 # ***** END GPL LICENCE BLOCK *****
24 bl_addon_info = {
25 "name": "Relax",
26 "author": "Fabian Fricke",
27 "version": (1,1),
28 "blender": (2, 5, 3),
29 "api": 31847,
30 "location": "View3D > Specials > Relax ",
31 "description": "Relax the selected verts while retaining the shape",
32 "warning": "",
33 "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/"\
34 "Scripts/Modeling/Relax",
35 "tracker_url": "https://projects.blender.org/tracker/index.php?"\
36 "func=detail&aid=21421&group_id=153&atid=469",
37 "category": "Mesh"}
39 """
40 Usage:
42 Launch from "W-menu" or from "Mesh -> Vertices -> Relax"
45 Additional links:
46 Author Site: http://frigi.designdevil.de
47 e-mail: frigi.f {at} gmail {dot} com
48 """
51 import bpy
52 from bpy.props import IntProperty
54 def relax_mesh(context):
56 # deselect everything that's not related
57 for obj in context.selected_objects:
58 obj.select = False
60 # get active object
61 obj = context.active_object
63 # duplicate the object so it can be used for the shrinkwrap modifier
64 obj.select = True # make sure the object is selected!
65 bpy.ops.object.mode_set(mode='OBJECT')
66 bpy.ops.object.duplicate()
67 target = context.active_object
69 # remove all other modifiers from the target
70 for m in range(0, len(target.modifiers)):
71 target.modifiers.remove(target.modifiers[0])
73 context.scene.objects.active = obj
75 sw = obj.modifiers.new(type='SHRINKWRAP', name='relax_target')
76 sw.target = target
78 # run smooth operator to relax the mesh
79 bpy.ops.object.mode_set(mode='EDIT')
80 bpy.ops.mesh.vertices_smooth()
81 bpy.ops.object.mode_set(mode='OBJECT')
83 # apply the modifier
84 bpy.ops.object.modifier_apply(modifier='relax_target')
86 # delete the target object
87 obj.select = False
88 target.select = True
89 bpy.ops.object.delete()
91 # go back to initial state
92 obj.select = True
93 bpy.ops.object.mode_set(mode='EDIT')
95 class Relax(bpy.types.Operator):
96 '''Relaxes selected vertices while retaining the shape as much as possible'''
97 bl_idname = 'mesh.relax'
98 bl_label = 'Relax'
99 bl_options = {'REGISTER', 'UNDO'}
101 iterations = IntProperty(name="Relax iterations",
102 default=1, min=0, max=100, soft_min=0, soft_max=10)
104 @classmethod
105 def poll(cls, context):
106 obj = context.active_object
107 return (obj and obj.type == 'MESH')
109 def execute(self, context):
110 for i in range(0,self.iterations):
111 relax_mesh(context)
112 return {'FINISHED'}
115 def menu_func(self, context):
116 self.layout.operator(Relax.bl_idname, text="Relax")
119 def register():
120 bpy.types.VIEW3D_MT_edit_mesh_specials.append(menu_func)
121 bpy.types.VIEW3D_MT_edit_mesh_vertices.append(menu_func)
123 def unregister():
124 bpy.types.VIEW3D_MT_edit_mesh_specials.remove(menu_func)
125 bpy.types.VIEW3D_MT_edit_mesh_vertices.remove(menu_func)
127 if __name__ == "__main__":
128 register()