Find the average location of several objects

Hey Guys,

I’ m writing a script in which it requires the average (or ‘mean’) coordinates of all selected objects and create another object in the centre of these. I know how to do it manually however it is time consuming and I would like to automate the process. Anyone have any ideas on how this would work? :slight_smile:

Questions welcome!

Cursor to average position of selected objects.



import bpy


obj= [i.location for i in bpy.context.selected_objects]


x = 0
y = 0
z = 0


for i in obj:
    x += i[0]
    y += i[1]
    z += i[2]


l = len(obj)
bpy.context.scene.cursor_location = (x/l,y/l,z/l)


do you want to average the location or the bounding boxes of all selected objects? two different things.

avg_loc = (0.0, 0.0, 0.0)
for ob in bpy.context.selected_objects:
avg_loc[0] += ob.location[0]
avg_loc[1] += ob.location[1]
avg_loc[2] += ob.location[2]

avg_loc = [i/len(bpy.context.select_objects) for i in avg_loc]

then make a new object with avg_loc

Thank you so much!

there is another way to do it with

bpy.ops.view3d.snap_cursor_to_selected()

but I get an error on the scene context ?

any idea how to set the proper context !

thanks
happy bl