0
$\begingroup$

I have been plugging away at this for a couple days without any real progress. I am creating a series of cubes in Blender which I need to all have beveled bottom edges (see image) enter image description here I was able to easily do this in CAD but this is kicking my butt in Blender. I have tried programmatically selecting the bottom vertices and creating a vertex group, however none of my attempts have resulted in a vertex group being created, and when it has been created the vertex groups end up being empty for some reason. The following script is what I am using to create this cube.

numberSpaces = 3
baseWidth = 32.25
wallThickness = 7
wallLength = baseWidth + wallThickness
tolerance = 0.2
floorHeight = 2.5
wallHeight = 25.75

brickLength = 10
brickHeight = 5

groutSpacing = 0.5
#Createe Base Geometry

#Insertion Base
# Add a basic cube to the scene
# Add a basic cube to the scene
def create_base_geometry():
    bpy.ops.mesh.primitive_cube_add()
    bpy.context.object.name = "Base Geometry Cube"
    cube = bpy.context.object

    # set scale and location of the cube
    cube.scale = (numberSpaces * (baseWidth + wallThickness)/2, 
                  wallThickness/2 - tolerance, 
                  floorHeight/2)
    cube.location = (numberSpaces * (baseWidth + wallThickness) / 2, 
                     0, 
                     floorHeight/2)

    # Select the cube object by name
    cube_object = bpy.data.objects["Base Geometry Cube"]

    # Get the active object
    obj = bpy.context.active_object

    # Switch to edit mode
    bpy.ops.object.mode_set(mode='EDIT')

    # Deselect all vertices and faces
    bpy.ops.mesh.select_all(action='DESELECT')

    # Select the bottom face of the cube
    for face in obj.data.polygons:
        if face.normal.z == -1.0:
            face.select = True

    # Create a new vertex group for the selected face
    group_name = "Bottom Face"
    group_index = obj.vertex_groups.new(name=group_name)
    bpy.ops.object.vertex_group_assign()

    # Switch back to object mode
    bpy.ops.object.mode_set(mode='OBJECT')

This creates the geometry, however I am unable to script-select the bottom vertices (I could do it manually but I would like this all to be scripted). I have the current selection criteria shown just to show what I have been trying, though there have been numerous incarnations of this and none of them have worked.

I followed This example however at the end of that script, although the selection claims to be created in the console the face/vertices are not selected in the UI, nor does the vertex group cretion menu allow me to create a new group from the selected vertices.

$\endgroup$

1 Answer 1

0
$\begingroup$

Depends on the average z position of each face vertices

import bpy, bmesh

def fn(obj, bevel_width, bevel_segments):
    bpy.context.view_layer.objects.active = obj
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.mesh.select_all(action='DESELECT')

    mat = obj.matrix_world
    bm = bmesh.from_edit_mesh(obj.data)

    lowerest_z = float("inf")
    lowerest_face = None
    # Select the bottom face of the obj
    for face in bm.faces:
        coors_z = [(mat @ (v.co)).z for v in face.verts]
        z_average = sum(coors_z) / len(coors_z)

        if z_average < lowerest_z:
            lowerest_z = z_average
            lowerest_face = face

    if lowerest_face is not None:
        lowerest_face.select_set(True)
        obj.data.update()

    # Create a new vertex group for the selected face
    group_name = "Bottom Face"
    group_index = obj.vertex_groups.new(name=group_name)
    bpy.ops.object.vertex_group_assign()

    # Switch back to object mode
    bpy.ops.object.mode_set(mode='OBJECT')

    # add bevel modifier
    md = obj.modifiers.new("Bevel", "BEVEL")
    md.width = bevel_width
    md.segments = bevel_segments
    md.limit_method = 'VGROUP'
    md.vertex_group = "Bottom Face"


for obj in bpy.context.selected_objects:
    fn(obj, 0.1, 6)
$\endgroup$

You must log in to answer this question.

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