3D Development

Python script to create sharp creases on Blender

This thing is pretty much simple. I just want to create a sharp crease to help me on my hard surface modeling stuff. This will simply carve a thin line in any mesh using bevel and extrude, and then another bevel in the outer edges just to give a sharpness on the mesh.

You basically select any edge loop like this (ONLY TESTED USING EDGE LOOPS):

Then, proceed running this script below under the “script” tab:

import bpy
import bmesh
from mathutils import Vector

# Ensure we are in object mode
bpy.ops.object.mode_set(mode='OBJECT')

# Check if there is an active object
if bpy.context.active_object is not None:
    
    # Ensure the active object is a mesh
    if bpy.context.active_object.type == 'MESH':
        
        # Switch to edit mode
        bpy.ops.object.mode_set(mode='EDIT')
        
        # Bevel selected edges
        bpy.ops.mesh.bevel(offset=0.01, segments=1, affect='EDGES')
        
        # Switch to face select mode to ensure faces are selected for extrusion
        #bpy.ops.mesh.select_mode(type="FACE")
        
        # Switch to face select mode to ensure faces are selected for extrusion
        bpy.ops.mesh.select_mode(type="FACE")
        
        # shrink the selected faces to create the crease
        bpy.ops.mesh.extrude_region_shrink_fatten(TRANSFORM_OT_shrink_fatten={"value":-0.03})
        
        #NOW, I Want to select the external edges and BEVEL THEM!
        obj = bpy.context.active_object
        bm = bmesh.from_edit_mesh(obj.data)
        original_selection_face = [face for face in bm.faces if face.select]
        original_selection_edge = [edge for edge in bm.edges if edge.select]
        #bpy.ops.mesh.select_similar()
        #bpy.ops.mesh.select_similar_region()
        #bpy.ops.mesh.select_next_item()
        bpy.ops.mesh.select_more(use_face_step=False)
        bpy.ops.mesh.region_to_loop()
        for face in original_selection_face:
            face.select = False
        for edge in original_selection_edge:
            edge.select = False  
        bpy.ops.mesh.bevel(offset=0.001, segments=1, affect='EDGES')      
        #bpy.ops.mesh.inset(thickness=0.1, depth=0)
        bmesh.update_edit_mesh(obj.data)        

else:
    print("No active mesh object selected.")

It will create this:

You can see the results by applying subdivision in the mesh:

You can change the values. There are comments in the script referring to them. For example If you wish to change the size of this crease, change the offset values:

bpy.ops.mesh.bevel(offset=0.01, segments=1, affect='EDGES')

Cheers!

Leave a comment