0
$\begingroup$

I know basically nothing about python but seeing other questions/answers I think this might be possible. I am wondering if it can be used to select an object based on the rough location of it's origin.

What I have is a series of objects aligned in a grid like this:

enter image description here

They have just been split from the same object and so their names are object.001, object.002, object.003, etc in a random order. After doing 'Origin to Geometry' all their origins will roughly align in a grid but not perfectly on specific coordinates.

The objects were all originally separated by 15m so the coordinates of "A"'s origin will always be between X (-7.5, 7.5), Y (-7.5, 7.5) and Z (-7.5, 7.5); the coordinates of "B"'s origin will always be between X (7.5, 22.5), Y (-7.5, 7.5) and Z (-7.5, 7.5); etc.

What I want to do is rename the objects so that the "A" object is called "1", the "B" object "2", the "C" object "3" and so on.

Is it possible to have a script that selects an object if it's origin is on a specific range of coordinates, e.g "A"'s XYZ will be between (-7.5 , 7.5, -7.5) and (7.5, -7.5, 7.5) and then renames it to a specific name?

The names are irregular and don't follow a pattern so I assume a script like this would be the same segment copy-pasted as many times as necessary with the coordinates and names changed manually on each iteration. I need a script for this because I do this process with multiple objects split into separate parts so it'd be much quicker if it could be automated

TL;DR How to use Python to select an object within a specific cubic area

I've found the same question with an answer but it's from 6 years ago and the script doesn't work on blender 3.5: Select objects in a specified region with python

$\endgroup$
4
  • 1
    $\begingroup$ How will the script know that for example object.001 is A to begin with? $\endgroup$
    – Harry McKenzie
    Commented Jul 23, 2023 at 16:24
  • $\begingroup$ It doesn't need to. The names are random when you divide an object into separate pieces. That's why I need it to select the object in that area, not object "A" by name in particular. $\endgroup$
    – Cornivius
    Commented Jul 23, 2023 at 20:22
  • $\begingroup$ But u said "What I want to do is rename the objects so that the 'A' object is called '1'". How would the script know the object u selected is A'? There's no way u can rename 'A' to '1' if the script can't tell which object is 'A' $\endgroup$
    – Harry McKenzie
    Commented Jul 23, 2023 at 21:25
  • $\begingroup$ "A" is just an example. The script need to select the object located in this bounding box: (-7.5 , 7.5, -7.5) (7.5, -7.5, 7.5) and rename it to whatever I tell it. Not select "A" specifically I've put a similar question in the edit. It doesn't work in 3.5 but it seems to be possible to do $\endgroup$
    – Cornivius
    Commented Jul 23, 2023 at 21:32

1 Answer 1

0
$\begingroup$

I actually managed to make a code that works based on this answer by removing the function that doesn't exist in 3.5: Select objects in a specified region with python

I know it's very niche but I'm posting it in case it helps anyone:

import bpy
from mathutils import Vector

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

for obj in bpy.context.scene.objects:
        if(IsInBoundingVectors(obj.matrix_world.to_translation(), ((-7.5, -7.5, 10)), ((7.5, 7.5, 25)))):
            obj.name = "E"
        elif(IsInBoundingVectors(obj.matrix_world.to_translation(), ((-7.5, -7.5, -5)), ((7.5, 7.5, 10)))):
            obj.name = "A"
        elif(IsInBoundingVectors(obj.matrix_world.to_translation(), ((7.5, -7.5, 10)), ((22.5, 7.5, 25)))):
            obj.name = "F"
        elif(IsInBoundingVectors(obj.matrix_world.to_translation(), ((7.5, -7.5, -5)), ((22.5, 7.5, 10)))):
            obj.name = "B"    
        else:
            False

Instead of selecting the object by location and then renaming them. This starts with all the objects I want to rename selected and just checks if the objects' location falls within the bounding box created by those vectors. If it does, it sets the name to whatever is after, otherwise moves to check the next bounding box.

enter image description here

enter image description here

I'm surprised it even works since this is the first time I use python and I barely know anything about programming so there's better ways of doing it but at least it's functional for what I needed it. I'd be glad to get some help in optimizing it, like stop checking the other 'IFs' if the name has already been changed or something like that.

$\endgroup$

You must log in to answer this question.

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