Select objects in a specified region with python

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

• – batFINGER Mar 7 '17 at 15:08

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)))

• This works, thanks a lot. Since i'm a fairly new python-scripter, could you tell me why the vector check is used? – Andre Bredeweg Mar 9 '17 at 8:30
• @AndreBredeweg Updated the code with comments. – Leander Mar 9 '17 at 11:17