1
$\begingroup$

He there,

is there a way to select all objects in a predefined region via python? Let's say i've got a complex scene and I want to select all objects that are located somwhere within the bounding of 0,0,-5 to 10,15,15 Just like the border select tool does but then within the given coordinates. thanks already

$\endgroup$
1

1 Answer 1

2
$\begingroup$

Look into this answer, as it is similiar.

My example will handle an XYZ bounding box with perpendicular edges only. Otherwise look into batFINGER's suggestion.

Paste the code into Blender's Text Editor and press "Run Script".

import bpy
from mathutils import Vector


# checks if a supplied coordinate if in the bounding box created by vector1 and vector2
# vector_check           the vector which is compared against the bounding box
# vector1                     the vector defining the start of the bounding box
# vector2                     the vector defining the end of the bounding box
def IsInBoundingVectors(vector_check, vector1, vector2):
    # if vector_check is either bigger or smaller than both other, it does not lie between them
    # in that case it won't be inside the bounding box; hence return false
    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 SelectObjectsInBound(vector1, vector2):
    # deselect all
    bpy.ops.object.select_all(action='DESELECT')

    # cycle through all objects in the scene
    for obj in bpy.context.scene.objects:
        # check if the object is in the bounding vectors
        # if yes, select it
        # if no, deselect it
        if(IsInBoundingVectors(obj.matrix_world.to_translation(), vector1, vector2)):
            obj.select = True
        else:
            obj.select = False


SelectObjectsInBound(Vector((0, 0, -5)), Vector((10, 15, 15)))
$\endgroup$
3
  • $\begingroup$ This works, thanks a lot. Since i'm a fairly new python-scripter, could you tell me why the vector check is used? $\endgroup$ Mar 9, 2017 at 8:30
  • $\begingroup$ @AndreBredeweg Updated the code with comments. $\endgroup$
    – Leander
    Mar 9, 2017 at 11:17
  • $\begingroup$ This doesn't work on blender 3.5 anymore. Any chance of an updated answer please? I'm pretty much asking the same thing on my question: blender.stackexchange.com/questions/297578/… $\endgroup$
    – Cornivius
    Jul 23, 2023 at 21:00

You must log in to answer this question.

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