-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmesh_laprelax
More file actions
152 lines (126 loc) · 4.71 KB
/
mesh_laprelax
File metadata and controls
152 lines (126 loc) · 4.71 KB
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
# ##### 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 #####
__bpydoc__ = """\
This addon implements a smoothing algorithm for meshes based on Laplacian relaxation, but adapted
to have no shrinkage.
Documentation
First go to User Preferences->Addons and enable the LapRelax addon in the Mesh category.
Go to EditMode, select some vertices and invoke the addon (button in the Mesh Tool panel).
Set the amount of times you want the smoothing operation to be repeated.
The mesh will be smoothed.
If you wish to hotkey LapRelax:
In the Input section of User Preferences at the bottom of the 3D View > Mesh section click 'Add New' button.
In the Operator Identifier box put 'mesh.laprelax'.
Assign a hotkey.
Save as Default (Optional).
"""
bl_info = {
"name": "LapRelax",
"author": "Gert De Roost", "Marco_105 (update)"
"version": (0, 2, 1),
"blender": (2, 7, 3),
"location": "View3D > Tools",
"description": "Smoothing mesh keeping volume",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Mesh"}
import bpy
import bmesh
from mathutils import *
import math
from bpy.props import *
class LapRelax(bpy.types.Operator):
"""Laplace Relax"""
bl_idname = "mesh.laprelax"
bl_label = "LapRelax"
bl_description = "Smoothing mesh keeping volume"
bl_options = {'REGISTER', 'UNDO'}
LapRepeat = bpy.props.IntProperty(
name = "LapRepeat",
description = "Repeat how many times",
default=1,
min=1,
max=100)
@classmethod
def poll(cls, context):
obj = context.active_object
return (obj and obj.type == 'MESH' and context.mode == 'EDIT_MESH')
def draw(self, context):
layout = self.layout
layout.prop(self, "LapRepeat")
# initialize operator
def invoke(self, context, event):
self.LapRepeat = 1
return self.execute(context)
def execute(self, context):
#smooth repeat times
for i in range(self.LapRepeat):
self.do_laprelax()
return {'FINISHED'}
def do_laprelax(self):
context = bpy.context
region = context.region
area = context.area
selobj = bpy.context.active_object
mesh = selobj.data
bm = bmesh.from_edit_mesh(mesh)
bm.verts.ensure_lookup_table()
bmprev = bm.copy()
for v in bmprev.verts:
if v.select:
tot = Vector((0, 0, 0))
cnt = 0
for e in v.link_edges:
for f in e.link_faces:
if not(f.select):
cnt = 1
if len(e.link_faces) == 1:
cnt = 1
break
if cnt:
# dont affect border edges: they cause shrinkage
continue
# find Laplacian mean
for e in v.link_edges:
tot += e.other_vert(v).co
tot /= len(v.link_edges)
# cancel movement in direction of vertex normal
delta = (tot - v.co)
if delta.length != 0:
ang = delta.angle(v.normal)
deltanor = math.cos(ang) * delta.length
nor = v.normal
nor.length = abs(deltanor)
bm.verts[v.index].co = tot + nor
mesh.update()
bm.free()
bmprev.free()
bpy.ops.object.editmode_toggle()
bpy.ops.object.editmode_toggle()
def panel_func(self, context):
self.layout.label(text="LapRelax:")
self.layout.operator(LapRelax.bl_idname, text="Laplace Relax")
def register():
bpy.utils.register_module(__name__)
bpy.types.VIEW3D_PT_tools_meshedit.append(panel_func)
def unregister():
bpy.utils.unregister_module(__name__)
bpy.types.VIEW3D_PT_tools_meshedit.remove(panel_func)
if __name__ == "__main__":
register()