Skip to content

Instantly share code, notes, and snippets.

@moorage
Created July 9, 2018 04:00
Show Gist options
  • Save moorage/a721d65465aec8a5b333930a18f5d7a1 to your computer and use it in GitHub Desktop.
Save moorage/a721d65465aec8a5b333930a18f5d7a1 to your computer and use it in GitHub Desktop.
Python function that returns the (x,y,z) typle of the center of multiple objects, within blender
cntr = _center_of_object_collection([ D.objects['obj_example1'], D.objects['obj_example2'], ... ])
def _center_of_object_collection(obs):
min_x = max_x = min_y = max_y = min_z = max_z = None
for ob in obs:
local_vertices = ob.bound_box[:]
worldify = lambda p: ob.matrix_world * Vector(p[:])
world_vertices = [worldify(p).to_tuple() for p in local_vertices]
for (x,y,z) in world_vertices:
if min_x is None or x < min_x:
min_x = x
if max_x is None or x > max_x:
max_x = x
if min_y is None or y < min_y:
min_y = y
if max_y is None or y > max_y:
max_y = y
if min_z is None or z < min_z:
min_z = z
if max_z is None or z > max_z:
max_z = z
return ( (max_x + min_x) / 2.00, (max_y + min_y) / 2.00, (max_z + min_z) / 2.00 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment