1

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

  • Related: http://math.stackexchange.com/questions/1472049/check-if-a-point-is-inside-a-rectangular-shaped-area-3d – batFINGER Mar 07 '17 at 15:08

1 Answers1

2

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)))
Leander
  • 26,725
  • 2
  • 44
  • 105
  • 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 09 '17 at 08:30
  • @AndreBredeweg Updated the code with comments. – Leander Mar 09 '17 at 11:17
  • 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: https://blender.stackexchange.com/questions/297578/python-select-an-object-inside-a-cubic-area-and-rename-it?noredirect=1#comment513293_297578 – Cornivius Jul 23 '23 at 21:00