5
$\begingroup$

I have a scene with objects organized hierarchically like this:

enter image description here

I would like to get the overall bounding box of the 00000_361.L3B EMPTY object - however, since this is an empty it has no dimensions, and its bounding box also has no dimensions.

I am thinking of recursing through the hierarchy like explained here but I wonder if there is a better/easier way to do that at the scene level.

$\endgroup$
2
  • $\begingroup$ The bounding box of all mesh objects that are children of the empty? $\endgroup$ – batFINGER May 27 at 8:57
  • $\begingroup$ @batFINGER - yes, correct. Or even better - if there were more empties, of all of them together. $\endgroup$ – simone May 27 at 10:14
6
$\begingroup$

Bounding box coords of all meshes in scene.

Pretty sure this has been answered before, but finding it is another matter.

Here is some numpy code I use to create a bounding box for all mesh objects in a scene

Basically, list coordinates of all bounding box coordinates of all mesh objects in the scene.

Use the minima / maxima of each axis produce an axis aligned bounding box encompassing all others.

import bpy
from mathutils import Vector
from bpy import context
import numpy as np
import itertools

# multiply 3d coord list by matrix
def np_matmul_coords(coords, matrix, space=None):
    M = (space @ matrix @ space.inverted()
         if space else matrix).transposed()
    ones = np.ones((coords.shape[0], 1))
    coords4d = np.hstack((coords, ones))
    
    return np.dot(coords4d, M)[:,:-1]
    return coords4d[:,:-1]

# get the global coordinates of all object bounding box corners    
coords = np.vstack(
    tuple(np_matmul_coords(np.array(o.bound_box), o.matrix_world.copy())
         for o in  
            context.scene.objects
            if o.type == 'MESH'
            )
        )
print("#" * 72)
# bottom front left (all the mins)
bfl = coords.min(axis=0)
# top back right
tbr = coords.max(axis=0)
G  = np.array((bfl, tbr)).T
# bound box coords ie the 8 combinations of bfl tbr.
bbc = [i for i in itertools.product(*G)]
print(np.array(bbc))
$\endgroup$
4
  • $\begingroup$ thanks - it looks like it works. If it's not too much trouble for you, would you add a couple comments in the code? It's a bit more idiomatic than the Python I can handle. But I can understand if you don't have the time $\endgroup$ – simone May 27 at 13:37
  • $\begingroup$ what does space @ matrix @ space.inverted() if space else matrix do? I read it as matrix multiplication of space by matrix - but I am stuck with the multiply by space.inverted if something. Sorry for the basic question - it looks like numpy - but it's hard to google for "if". BTW - the comments help a lot $\endgroup$ – simone May 27 at 14:58
  • $\begingroup$ ...and whats's the second return statement in np_matmul_coords? $\endgroup$ – simone May 27 at 15:26
  • $\begingroup$ To put the bounding box in the local space of another object if need be.From a personal addon to scale a lattice to encompass objects and make an oblique projection eg blender.stackexchange.com/a/195675/15543 The second return statement will never be run and can be ignored or removed. Be a remnant from a copy paste job from a method to convert 3d coords to 4d $\endgroup$ – batFINGER May 27 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.