How to find location of rotation center of several selected objects in scene?

There are several selected cubes in scene. I’m going to rotate them with bpy.ops.transform.rotate. How I can get in Python location of their rotation center?

This is a guess, because I have not tested it, but my bet is

  1. You have the x, y, z, coordinates each of the objects.
  2. If you take all the x-coordinates, add them together, and then divide them by the number of objects,
    you would get the average distance between all the x-coordinates, the center I believe, of the x-coordinates.
  3. Do the same with the y and the z coordinates.
  4. That should give you the center of the group.
  5. Create an empty at that coordinate, parent the objects to it and rotate the empty.

You want to rotate them as a group around the center of the group?
Calculate bounds. Calculate center of bounds.

So… if you want the ‘bounds’ on X:
Get the x of the nearest cube and the x of the furthest cube.
The Math.abs distance *0.5 is the center of x for the entire group.
Do that for all 3 dimensions.

But it only works if the cubes are all the same size and with their origins at the center of their respective geometries.

To get true bounds you have to add half the size of x for both cubes to the distance between cube origins.
That will give you the extent of bounds. Half of that is the center.

It gets more complicated if you’ve moved the origins of the cubes away from their geometric centers…
Or if you have local rotations… But now that you know BASICALLY what you gotta do…
You can figure out how to account for variants on your onesies.

How much did you want to bet ?

I would suggest you read this Forum’s Terms of Service. If there is any doubt, I would be happy to contact a moderator to resolve it.

Oh sorry… Is there a rule against gambling?
Oh dammit… I knew I should have tacked that ‘winky-smiley’ face on the end there…
But I looked at it it and said Naaaaaahhh… I don’t need to put no ‘winky-smiley’ face on there.
It’s obvious that it’s just some good-natured banter… A little tweaking of the foibles.
I didn’t really want to take your money.

WINKEY SMILEY FACE :wink:

I clearly stated that what I posted was a guess, meaning it was not proven
or supportable, so the fact that you challenged it as if I should defend it
says much more about you than it does about me.

This can go several ways, calculate based on the center of geometry, calculate the visual center of dispersed objects using some sort of bounds or simply averaging the center points of the objects and using that, which is somewhat similar to calculating center of geometry however it is not as accurate.

Here is an example that shows how quickly this can become complex when calculating the center of geometry, for your case you may want to go with the simpler solution and gather the object origin coords and use those to create the center.

Note; If accuracy is needed based on geometry then you may need to do matri multiplication between each vertex coord and object matrix


coordinates = []
for object in context.selected_objects:
    coordinates += [vertex.co for vertex in object.data.vertices]

rotation_origin = location(coordinates)

def location(coordinates):

    x = [vertex.x for vertex in coordinates]
    y = [vertex.y for vertex in coordinates]
    z = [vertex.z for vertex in coordinates]

    return sum(bounds(x, y, z), Vector()) / len(bounds(x, y, z))

def bounds(x, y, z):

    return [
        Vector((min(x), min(y), min(z))),
        Vector((min(x), min(y), max(z))),

        Vector((min(x), max(y), min(z))),
        Vector((min(x), max(y), max(z))),

        Vector((max(x), min(y), min(z))),
        Vector((max(x), min(y), max(z))),

        Vector((max(x), max(y), min(z))),
        Vector((max(x), max(y), max(z))),
    ]

I personally do matrix multiplication between the vertex coords when I create the initial coordinates list and I prefer to use bmesh to create a convex hull around all of the geometry as a single object and feed those vertex coords into coordinates, this step isn’t mandatory however matrix multiplication between vertex coords is but it can easily become more complex then I am willing to address here in this post.

By doing object matrix multiplication on each vertex coord you can get accurate bounds regardless of translation, rotation or scale performed on the object and object origins are not needed at all to calculate the center.

The concept here is to grab the coords and feed them into a bound function that creates a bound box that can then be used to find the center via averaging.

Setting the 3D cursor to this location and placing the rotation pivot point around the 3D cursor is the way I recommend how to rotate these objects after the fact.