# How to scale primitive shapes in Blender so that they are within a [unit] sphere?

I wonder how is it possible to scale all shapes so that they are within boundaries of a [unit] sphere? I know how to do this for a cube: if radius of the sphere is S I set the length of X, Y and Z of the cube to S * math.sqrt(3)/3. This way, the corners of the cube are barely touching the surface of the sphere. But I don't know how to do this for other objects such as torus, cylinder and cone. Is there a general rule for this?

Use the bound box. Result on Default cube scaled to fit in unit sphere

Half the length of one of the principal diagonals of the bounding box will be the radius of an encompassing sphere.

Simple test script. Looks purely at local coordinates from the bounding box and scales such that will fit in local unit sphere.

Note this ensures they fit in a cuboid that fits in a sphere, not such that it fits perfectly into a sphere.

import bpy
from mathutils import Vector

def bbox(ob):
return (Vector(b) for b in ob.bound_box)

def bbox_center(ob):
return sum(bbox(ob), Vector()) / 8

bb = list(bbox(ob))
return (bb - bb).length / 2

context = bpy.context
for ob in context.selected_objects:
ob.scale *= scale_factor


Max vert from centroid.

A similar method could be employed that looks at maximum distance from centroid on a per vertex basis.

def mesh_radius(ob):
o = bbox_center(ob)
return max((v.co - o).length for v in ob.data.vertices)

context = bpy.context
for ob in context.selected_objects: