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.

enter image description here 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

def bbox_radius(ob):
    bb = list(bbox(ob))
    return (bb[6] - bb[0]).length / 2

context = bpy.context
for ob in context.selected_objects:
    scale_factor = 1 / bbox_radius(ob)
    ob.scale *= scale_factor

Max vert from centroid.

enter image description here Result on Cone

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:
    scale_factor = 1 / mesh_radius(ob)
    ob.scale *= scale_factor
  • $\begingroup$ Thank you. Does this also work for things like cone, torus etc? $\endgroup$ – Amir Apr 19 '19 at 15:49
  • $\begingroup$ I am mostly looking at a deterministic approach to this problem. Do you know if it would be possible to get the scaling factor of a shape so that it fits in a sphere using a single equation? $\endgroup$ – Amir Apr 19 '19 at 15:50
  • $\begingroup$ A more advanced algorithm should be found so that at least 4 vertices of the target object touch the sphere, which is the minimum. $\endgroup$ – Vilém Duha Apr 19 '19 at 18:19

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.