Skip to content
Snippets Groups Projects
mesh_relax.py 3.01 KiB
Newer Older
  • Learn to ignore specific revisions
  • # SPDX-License-Identifier: GPL-2.0-or-later
    # Copyright 2010 Fabian Fricke.
    
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    # Relaxes selected vertices while retaining the shape as much as possible
    
    
    bl_info = {
    
    Luca Bonavita's avatar
    Luca Bonavita committed
        "name": "Relax",
    
        "author": "Fabian Fricke",
    
        "version": (1, 2, 0),
        "blender": (2, 80, 0),
    
    meta-androcto's avatar
    meta-androcto committed
        "location": "View3D > Edit Mode Context Menu > Relax ",
    
        "description": "Relax the selected verts while retaining the shape",
        "warning": "",
    
        "doc_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
                   "Scripts/Modeling/Relax",
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    Usage:
    
    Launch from "W-menu" or from "Mesh -> Vertices -> Relax"
    
    
    Additional links:
        Author Site: http://frigi.designdevil.de
        e-mail: frigi.f {at} gmail {dot} com
    """
    
    
    import bpy
    from bpy.props import IntProperty
    
    
    
        # deselect everything that's not related
        for obj in context.selected_objects:
    
            obj.select_set(False)
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        obj = context.active_object
    
        # duplicate the object so it can be used for the shrinkwrap modifier
    
        obj.select_set(True) # make sure the object is selected!
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        bpy.ops.object.mode_set(mode='OBJECT')
        bpy.ops.object.duplicate()
        target = context.active_object
    
    
        # remove all other modifiers from the target
        for m in range(0, len(target.modifiers)):
    
            target.modifiers.remove(target.modifiers[0])
    
        context.view_layer.objects.active = obj
    
        sw = obj.modifiers.new(type='SHRINKWRAP', name='relax_target')
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        sw.target = target
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        # run smooth operator to relax the mesh
        bpy.ops.object.mode_set(mode='EDIT')
    
        bpy.ops.mesh.vertices_smooth(factor=0.5)
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        bpy.ops.object.mode_set(mode='OBJECT')
    
    
        bpy.ops.object.modifier_apply(modifier='relax_target')
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        # delete the target object
    
        obj.select_set(False)
        target.select_set(True)
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        bpy.ops.object.delete()
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        # go back to initial state
    
        obj.select_set(True)
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        bpy.ops.object.mode_set(mode='EDIT')
    
    class Relax(bpy.types.Operator):
    
        """Relaxes selected vertices while retaining the shape """ \
        """as much as possible"""
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        bl_idname = 'mesh.relax'
        bl_label = 'Relax'
        bl_options = {'REGISTER', 'UNDO'}
    
    
        iterations: IntProperty(name="Relax iterations",
    
                    default=1, min=0, max=100, soft_min=0, soft_max=10)
    
        @classmethod
        def poll(cls, context):
    
    Brendon Murphy's avatar
    Brendon Murphy committed
            obj = context.active_object
            return (obj and obj.type == 'MESH')
    
        def execute(self, context):
    
    Brendon Murphy's avatar
    Brendon Murphy committed
            return {'FINISHED'}
    
    
    
    def menu_func(self, context):
        self.layout.operator(Relax.bl_idname, text="Relax")
    
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    
    def register():
    
        bpy.utils.register_class(Relax)
    
        bpy.types.VIEW3D_MT_edit_mesh_context_menu.append(menu_func)
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        bpy.types.VIEW3D_MT_edit_mesh_vertices.append(menu_func)
    
    def unregister():
    
        bpy.utils.unregister_class(Relax)
    
        bpy.types.VIEW3D_MT_edit_mesh_context_menu.remove(menu_func)
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        bpy.types.VIEW3D_MT_edit_mesh_vertices.remove(menu_func)
    
    if __name__ == "__main__":