2
$\begingroup$

I need to set weights for some edges from a python script. I have tried looping through object.data.edges and setting bevel_weight values but I can't get it right. It only works if I have first edited some edge's bevel weight by hand from the 3d-view's transform window. But I would like to get it work from my script without any manual hacking.

Steps to reproduce:

Start Blender with default cube and switch to Scripting view.

Try to set weights for all edges:

for edge in C.object.data.edges:
    edge.bevel_weight = 1.0

Verify that values changed:

for edge in C.object.data.edges:
    edge.bevel_weight

Switch into edit mode in 3d-view and open up transform window by pressing n. Select edges to see that the change did not work (bevel weights are all zero). Switch back to object mode and read values again in scripting console to see that they were reset to zero:

for edge in C.object.data.edges:
    edge.bevel_weight

Go back to edit mode and change some edge bevel weight straight from the transform window by hand. Go back to object mode and change values again from the scripting console:

for edge in C.object.data.edges:
    edge.bevel_weight = 1.0

This time it works, bevel weights are 1.0 when you go back to edit mode and look from the transform window.

Any ideas? My final need is to apply a bevel modifier to only some set of edges and that's why I'm trying to mark them by using weights. Using vertex group for bevel modifier does not work in my situation, since I need to specify edges, not vertices, which to bevel.

I'm using Blender v2.71.

$\endgroup$

1 Answer 1

5
$\begingroup$

There is no custom data layer for edge bevel weights by default. You need to explicitly enable it (this is done automatically if you use the Edge Bevel Weight modal operator in the UI):

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

Furthermore, the mesh needs to be in Object Mode when you apply a bevel weight:

if ob.is_editmode:
    bpy.ops.object.mode_set(mode='OBJECT')
me.edges[0].bevel_weight = 0.7

If you want to avoid mode switching, you may use the bmesh module instead (bm.edges.layers.bevel_weight).

$\endgroup$
1
  • $\begingroup$ I was confused for a while with this. I thought whenever I printed the bevel weight within my script it would update, but it wouldn't update if I didn't print. But it turns out correlation isn't causation; what really happened is that I added the print statement, went into edit mode, inadvertently added custom data by messing with the UI bevel weight, and then ran the script to see the bevel weight "magically" update! $\endgroup$ Commented Mar 19, 2021 at 16:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .