Page MenuHome

mesh_relaxlhc.py

mesh_relaxlhc.py

# mesh_relax_lhc.py Copyright (C) 2012, Tobias Oelgarte
#
# Relaxes selected vertices while retaining the shape
#
# ***** 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 *****
bl_info = {
"name": "RelaxLHC",
"author": "Tobias Oelgarte",
"version": (1,0),
"blender": (2,6,3),
"location": "View3D > Specials > RelaxLHC",
"description": "Relaxes the selected vertices while retaining shape",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Mesh"
}
"""
Usage:
Launch from "W-menu" or from "Mesh -> Vertices -> Relax"
"""
import bpy
from mathutils import Vector as Vec
from bpy.props import FloatProperty
def relax(context, strength, hcFactor):
bpy.ops.object.mode_set(mode='OBJECT')
mesh = context.active_object.data
vertices = mesh.vertices
edgeKeys = mesh.edge_keys
num = len(vertices)
data = [[Vec((0,0,0)),Vec((0,0,0)),Vec((0,0,0)),Vec((0,0,0)), 0] for i in range(num)]
for ek in edgeKeys:
data[ek[0]][0] += vertices[ek[1]].co
data[ek[1]][0] += vertices[ek[0]].co
data[ek[0]][4] += 1
data[ek[1]][4] += 1
for ve in vertices:
i = ve.index
diff = data[i][0] * (1.0/data[i][4])
data[i][1] = diff
data[i][2] = diff - ve.co
for ek in edgeKeys:
data[ek[0]][3] += data[ek[1]][2]
data[ek[1]][3] += data[ek[0]][2]
for i in range(num):
ve = vertices[i]
if ve.select:
loc_l = data[i][0] / data[i][4]
d = loc_l - ve.co
d = ve.normal * (d.dot(ve.normal) / ve.normal.length_squared)
loc_l -= d
loc_hc = data[i][1] - 0.5 * (data[i][2] + data[i][3]/data[i][4])
loc_lhc = loc_l*(1.0-hcFactor) + loc_hc*hcFactor
ve.co = ve.co*(1.0-strength) + loc_lhc*strength
bpy.ops.object.mode_set(mode='EDIT')
class RelaxLHC(bpy.types.Operator):
""" Relaxes the selected vertices while retaining shape """
bl_idname = "mesh.relaxlhc"
bl_label = "RelaxLHC"
bl_options = {'REGISTER', 'UNDO'}
strength = FloatProperty( \
name="Strength", \
description="Strength of the relaxiation", \
default=1.0, min=-10.0, max=10.0, soft_min=0.0, soft_max=1.0, \
step=3, precision=2)
hcFactor = FloatProperty( \
name="HC Factor", \
description="Blends from Laplacian to HC relaxiation", \
default=0.0, min=0.0, max=1.0, soft_min=0.0, soft_max=1.0, \
step=3, precision=2)
@classmethod
def poll(self, context):
obj = context.active_object
return (obj and obj.type == 'MESH')
def execute(self, context):
relax(context, self.strength, self.hcFactor)
return {"FINISHED"}
def menu_func(self, context):
self.layout.operator(RelaxLHC.bl_idname, text="RelaxLHC")
def register():
bpy.utils.register_module(__name__)
bpy.types.VIEW3D_MT_edit_mesh_specials.append(menu_func)
bpy.types.VIEW3D_MT_edit_mesh_vertices.append(menu_func)
def unregister():
bpy.utils.unregister_module(__name__)
bpy.types.VIEW3D_MT_edit_mesh_specials.remove(menu_func)
bpy.types.VIEW3D_MT_edit_mesh_vertices.remove(menu_func)
if __name__ == "__main__":
register()

File Metadata

Mime Type
text/x-python
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
dd/4f/c373c848d4295d9dc7e55972fb5a

Event Timeline