I seek a means (python or other) to name objects aligned on an axis (in the example: z-axis)
I want all the objects that have:
the highest value on the z-axis is called "name_1.nnn).
the smallest value is called "name_2.nnn)
...
I have a lot of line so it takes a long time to do it manually!
addons like "name panel," "patch renaming" do not do what I want.

Asked
Active
Viewed 237 times
3
logiquefloue
- 89
- 5
1 Answers
1
Sorting by z and x location
Simple script to sort all the objects in a scene named "Cube[.nnn]" by -z location and x location. Added a tolerance of 0.0001, any cube with a z within is considered in same row. The .nnn extension is the index in the order.
import bpy
context = bpy.context
scene = context.scene
# all cubes in the scene
cubes = [o for o in scene.objects if o.name.startswith("Cube")]
# row tolerance
TOL = 0.0001
if cubes:
#sort them by -z , x loc
cubes = sorted(cubes, key = lambda o: (-o.location.z, o.location.x))
row = 1
z = cubes[0].location.z
for i, cube in enumerate(cubes):
if abs(cube.location.z - z) > TOL:
row += 1
z = cube.location.z
cube.name = "Cube_%d.%03d" % (row, i)
To name them similarly to the convention in the q, could
cube.name = cube.name.replace("Cube", "Cube_%d" % row)
To make sure global locations are used, use cube.matrix_world.to_translation() instead of cube.location.
Or if the objects have differing origins, could use the bounding box centre as a location.
-
Is there a way to make it work for any kind of object in a collection? – Nascent Space Jul 21 '21 at 04:19
