2
$\begingroup$

The question is the same as Selecting vertices within a box defined by exact coordinates, but now on Blender 3.0.1:

I want to select all vertices within a bounding box defined by (x1,y1,z1) and (x2, y2, z2) exactly without selecting other vertices outside. Is there anyway to achieve this? (Add-ons will be fine)

$\endgroup$

1 Answer 1

1
$\begingroup$

It looks like only three things had to be changed:

#36 bmesh.update_edit_mesh(mesh, False, False)

to bmesh.update_edit_mesh(mesh)


#46 active_object = bpy.context.scene.objects.active

to active_object = bpy.context.object


For it to work in Object Mode, also line #41 from bpy.context.scene.objects.active = bpy.context.scene.objects.active

to bpy.context.view_layer.update()


import bpy, bmesh
from mathutils import Vector

# checks if a supplied coordinate if in the bounding box created by vector1 and vector2
def IsInBoundingVectors(vector_check, vector1, vector2):
    for i in range(0, 3):
        if (vector_check[i] < vector1[i] and vector_check[i] < vector2[i]
            or vector_check[i] > vector1[i] and vector_check[i] > vector2[i]):
            return False
    return True


def SelectVerticesInBound(object, vector1, vector2):
    # get the mesh data from the object reference
    mesh = object.data

    # get the bmesh data
    if mesh.is_editmode:
        bm = bmesh.from_edit_mesh(mesh)
    else:
        bm = bmesh.new()
        bm.from_mesh(mesh)

    # cycle through all vertices
    for vert in bm.verts:
        # check if the vertice is in the bounding vectors
        # if yes, select it
        # if no, deselect it
        if(IsInBoundingVectors(vert.co, vector1, vector2)):
            vert.select = True
        else:
            vert.select = False

    # update bmesh to mesh
    if bm.is_wrapped:
        bmesh.update_edit_mesh(mesh)
    else:
        bm.to_mesh(mesh)
        mesh.update()
    # trigger viewport update
    bpy.context.view_layer.update()



# get the active object from the scene
active_object = bpy.context.object

# ensure that there is an active object
if (active_object != None):
    # call the method to select all vertices in bounding box vectors
    # object             the object to operate on
    # vector 1           the first coordinate of the bounding box
    # vector 2           the second coordinate of the bounding box
    SelectVerticesInBound(active_object, Vector((0, 0, 0)), Vector((1, 1, 1)))
$\endgroup$
4
  • $\begingroup$ Mmm ... it's not working for me. The python console shows plenty of errors, the very first is an indentation error on line 18 !!! $\endgroup$ Commented May 16, 2023 at 16:15
  • $\begingroup$ @RodolfoLeibner you're not supposed to paste it into Python console. Paste the code to a textblock and run it ▶ $\endgroup$ Commented May 16, 2023 at 17:51
  • $\begingroup$ @RodolfoLeibner Also I tested it only in Edit Mode. Apparently in Object Mode you need to replace line 41 bpy.context.scene.objects.active = bpy.context.scene.objects.active with bpy.context.view_layer.update() $\endgroup$ Commented May 16, 2023 at 17:54
  • 1
    $\begingroup$ Got it. Thanks. $\endgroup$ Commented May 16, 2023 at 18:36

You must log in to answer this question.

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