Skip to content

Instantly share code, notes, and snippets.

@Dodotree
Created May 15, 2018 23:50
Show Gist options
  • Save Dodotree/ae318b5d873420cbbef3190e8c7e7661 to your computer and use it in GitHub Desktop.
Save Dodotree/ae318b5d873420cbbef3190e8c7e7661 to your computer and use it in GitHub Desktop.
Blender3D script to find best fit box angle
import bpy, bmesh
from mathutils.geometry import box_fit_2d
'''
After rotation bounding box will have minimum volume
'''
def changeToBestFitAngle(id):
obj = selectAndActivate(id)
bpy.ops.object.mode_set(mode='EDIT')
mesh=bmesh.from_edit_mesh(bpy.context.object.data)
pointsXY = [v.co.xy for v in mesh.verts]
rot_z = box_fit_2d( pointsXY )
del pointsXY
print( degrees(rot_z) )
pointsYZ = [v.co.yz for v in mesh.verts]
rot_x = box_fit_2d( pointsYZ )
del pointsYZ
print( degrees(rot_x) )
pointsXZ = [v.co.xz for v in mesh.verts]
rot_y = box_fit_2d( pointsXZ )
del pointsXZ
print( degrees(rot_y) )
obj.rotation_euler = [rot_x, rot_y, rot_z]
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment