API for setting bevel weights?

After googling around, I came up with this, but it is not working… any ideas? If I run the commands by hand in the console it works, but not if I run it from a text block.

To run it, I enter edit mode and select some edges. The object already has a bevel modifier activated, with the “Limit Method” set to weight – this works as expected.

import bpy
ob = bpy.context.object
ob.update_from_editmode()
ob.data.use_customdata_edge_bevel = True
edges = [e for e in ob.data.edges if e.select]
if str(ob.mode) == 'EDIT':
    bpy.ops.object.mode_set(mode='OBJECT')
for edge in edges:
    edge.bevel_weight=0.5

Apparently you have to be in Object mode to set the bevel weights?

I also saw bpy.context.object.is_editmode but that caused an error so I had to do the string compare…?

The list comprehension needs to take place after the mode set.

edges = [e for e in ob.data.edges if e.select]
for edge in edges:
    edge.bevel_weight=0.5

is_editmode is for data, not objects.
ob.mode is already a string.

Much obliged – that worked.

If you need to do it through edit mode, you’ll need to use bmesh and access bevel weight with the BMLayerAccessEdge object.

Thanks!

I used my custom script to create this tapered bevel:
tapered_bevel

…is there a clever GUI way to achieve something like that?

Also, is there any way to set/modify/paint bevel weights besides the Ctrl-E “Edge Bevel Weight”?

In Blender 2.91.2 I toggle in and out of EDIT mode to bevel edges. The example here has 2mm bevel, following the Scripting workspace command history after mouse clicking in the UV Editing workspace.

bpy.ops.object.editmode_toggle()
bpy.ops.mesh.bevel(offset = 0.002, offset_pct = 0, affect = 'EDGES')
bpy.ops.object.editmode_toggle()