Toggle Mark Sharp edge/ Clear sharp edge

I’m new to Blender and slowly setting up my keybinds. I was looking for a way to mark sharp edges and clear them in a single bind instead of two, like a toggle. I found a thread with a script for a creasing toggle but I don’t know how to transform it to sharp edges as I’m not familiar enough with Blender’s python API. If someone could lend a hand that would be much appreciated

import bpy
import bmesh


class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):

        ob = context.object
        me = ob.data
        bm = bmesh.from_edit_mesh(me)
        cl = bm.edges.layers.crease.verify()

        for edge in bm.edges:
            if edge.select and (edge[cl] == 0.0 or edge.smooth):
                sharp = False
                break
        else:
            sharp = True

        for edge in bm.edges:
            if edge.select:
                edge[cl] = 0.0 if sharp else 1.0
                edge.smooth = sharp

        bmesh.update_edit_mesh(me, False, False)

        return {'FINISHED'}


def register():
    bpy.utils.register_class(SimpleOperator)


def unregister():
    bpy.utils.unregister_class(SimpleOperator)


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.object.simple_operator()

Recently tried to do something like this, try this code:

import bpy
import bmesh

obj = bpy.context.object
me = obj.data
bm = bmesh.from_edit_mesh(me)
bw = bm.edges.layers.bevel_weight.verify()
cr = bm.edges.layers.crease.verify()
selected = [e for e in bm.edges if e.select]

if any(e.smooth == True for e in selected):
    bpy.ops.mesh.mark_sharp()

else:
    bpy.ops.mesh.mark_sharp(clear=True)

bmesh.update_edit_mesh(me, True)

Similar for the seams:

import bpy

ob = bpy.context.object
me = ob.data

if me.is_editmode:
    ob.update_from_editmode()

for e in me.edges:
    if e.select and e.use_seam == False:
        bpy.ops.mesh.mark_seam()

    elif e.select and e.use_seam == True:
        bpy.ops.mesh.mark_seam(clear=True)
1 Like

Thanks a lot, works just as expected. And kudos for the seams version. Can’t wait to get comfortable with modeling in Blender so i can start diving into more advanced stuff like scripting