# how to adapt a box to an object with a python script?

First I create a bouding box around an object with this script :

selected = bpy.context.selected_objects
for obj in selected:
#ensure origin is centered on bounding box center
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='BOUNDS')
#create a cube for the bounding box
#our new cube is now the active object, so we can keep track of it in a variable:
bound_box = bpy.context.active_object
#copy transforms
bound_box.dimensions = obj.dimensions
bound_box.location = obj.location
bound_box.rotation_euler = obj.rotation_euler


Then I resize the box like I wish :

bpy.ops.transform.resize(value=(1.4,1.4,3.5))


This is my result :

After that I want to resize only the highest face of the box so that it arrive 5 mm above the object, like this :

I already know this script to select the highest face of the bouding box :

object = bpy.data.objects['Cube']
object.select = True
me = bpy.context.object.data
bm = bmesh.new()
bm.from_mesh(me)
for face in bm.faces:
center = face.calc_center_median()
if center.z >= 1:
face.select_set(True)
bm.to_mesh(me)
bm.free()


I equally know how to get the highest point of the object :

max = -9999
for i in obj.data.vertices :
if i.co[2] > max :
max_ind = i.index
max  = i.co[2]


But I don't know how to position the highest plan of the box 5 mm above the highest point of the object ?

• If you want to use the bmesh module for this operation, you need to get the top face's vertices (you can't move faces and edges directly, but their verts; it's as simple as face.verts). Then translate the vertices, either by individually changing their .co.z value or by using bmesh.ops.translate(). The latter will be relative to the vertices' current locations. Jul 9 '15 at 15:25

I found how to move the highest plan of the bounding box and position it correctly above the object :

activeob = bpy.context.selected_objects
cube = activeob[0]
bpy.context.scene.objects.active = bpy.data.objects[cube.name]
bpy.ops.object.mode_set(mode='EDIT')
mesh = bmesh.from_edit_mesh(cube.data)
ob = bpy.data.objects[cube.name]
matrix = ob.matrix_world
for v in mesh.verts:
if v.select:
v.co = v.co*matrix
v.co.z += 20
matrix_inv = matrix.inverted()
v.co = matrix_inv*v.co