# Bevel modifier auto segments

Recently I created a video about a way to increment automatically the number of segments.

My question is is there a way to script and integrate the formulas directly bpy.context.object.modifiers["Bevel"].segments = "Formula here" .

I spend the last week trying to figure out but I think my python knowledge is not enough. If you have any suggestions it will be cool.

Watch the video to see how it works, I think it will be great to have that feature. ;)

If all what you want is smooth edges, then you are probably better off using a higher number of segments that's high enough for the critical parts, according to your artist's opinion.

First one should discern an adaptive number of segments for all edges together or for each edge.

For all edges together: Calculating the number of segments for a given segment length has no closed mathematical solution and requires numerical approximation. Also some bevels don't have a constant offset but are smaller on one end than the other. You have to find the biggest segment width and increase the number of segments iteratively until your maximum width/offset is not exceeded.

For each edge individually: 1. Using different number of segments for each edge does not allow a clean topology in the intersections (the vertices where bevelled edges meet). Unless you use Ngons for all intersections, there is no known good solution. 2. In the video, multiple bevels are subsequently used. You can clearly see that the intersection is much different from a usual bevel (top face of the cube is a Ngon). If you have 20 edges and each should have a variable number of segments, one can think of 20 subsequent bevel modifiers. The result will depend on the order which edge is bevelled first and so on. 3. The same problem as in "for all edges" applies here as well.

The used formulae in the video is not accurate, it is just a bit correlated. The segment width or offset is far from constant.

Edit: Approximate answer for 90° bevels with profile 0.5:

import bpy
C = bpy.context
from math import pi, ceil

# The approximate size of the segments
segment_size = 0.05

# The modifier you want to alter
modifier = C.object.modifiers["Bevel"]

# This is the arc length of a 90° corner, which is close to the
# summed length of the segments
arc = pi*modifier.width/8
seg = ceil(arc/segment_size)
if seg < 1:
seg = 1

bpy.context.object.modifiers["Bevel"].segments = seg


Anything more general will require:

• Face angles at every bevelled edge
• 3 nested numeric search algorithms
• Oh yeah for sure I have no math knowledge to make the formula accurate it was really trial and errors. It is really for speed and concept design by no mean for having clean topoligy but good enough. My goal is to have a pipepline allows to go to panel as less as posssible and develop tools to do that. Oct 20, 2017 at 8:19
• You could call it a coincidence that I am involved in exactly this code section since recently. I think I could give you an approximate answer, but it will be some more code. However it cannot include any clamping effects or some other situations, so it will be correct in only some cases like your examples. For complicated geometry, it won't be. Before that, please confirm that you want this: a script that changes the number of segments for one specific bevel modifier so that the generated segments are no bigger than a given value for (basic cases). Oct 20, 2017 at 11:31
• Oh oh what a coincidence right. Yeah I know, that's why I am slowly learning python with the book learning python by David Ascher and Mark Lutz to finally be able to create what I need. What i try to achieve is select an edge in edit mode and activate a script allows me by moving the mouse from left to right or vice versa create either a smooth bevel with enough edge based on the volume of the mesh (the script will be base on 1m3 for example) on a simple one. I want to be able to choose between weight or assign group depending on the effect I want. Maye a video will be clearer no? Oct 21, 2017 at 21:09
• @iamdemsugar Sorry to disappoint you but I don't think one can do that with Python. Mouse control requires an operator afaik, which is written in C/C++ and much more complicated. Also working in Edit Mode is a problem in Python. Seems like the original question is only a small part of your idea. Please reconsider if your goal is reachable if you are not a programmer. As I said, I can give you Python code that calculates the required number of segments for given segment width for basic cases. Anything else is beyond the scope. Oct 22, 2017 at 19:44
• Oh yeah so it's definitely out of my reach for now. Yeah the original question was only for the beveling part the addon I want is just a long-term fun project to learn how to code. But definitely not now ha ha. Waouh thanks, man it is really cool, I really appreciate it. It will allow me to study some code that's great Oct 22, 2017 at 23:01