Skip to content

Instantly share code, notes, and snippets.

@sambler
Last active February 27, 2017 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sambler/273d8f8c170e3415f54196f10bdc3b73 to your computer and use it in GitHub Desktop.
Save sambler/273d8f8c170e3415f54196f10bdc3b73 to your computer and use it in GitHub Desktop.
Create a bounding box around specific objects
# for comment at http://blender.stackexchange.com/a/14188/935
import bpy
import bmesh
from bpy.props import BoolProperty, FloatVectorProperty
import mathutils
from bpy_extras import object_utils
# from blender templates
def add_box(width, height, depth):
"""
This function takes inputs and returns vertex and face arrays.
no actual mesh data creation is done here.
"""
verts = [(+1.0, +1.0, -1.0),
(+1.0, -1.0, -1.0),
(-1.0, -1.0, -1.0),
(-1.0, +1.0, -1.0),
(+1.0, +1.0, +1.0),
(+1.0, -1.0, +1.0),
(-1.0, -1.0, +1.0),
(-1.0, +1.0, +1.0),
]
faces = [(0, 1, 2, 3),
(4, 7, 6, 5),
(0, 4, 5, 1),
(1, 5, 6, 2),
(2, 6, 7, 3),
(4, 0, 3, 7),
]
# apply size
for i, v in enumerate(verts):
verts[i] = v[0] * width, v[1] * depth, v[2] * height
return verts, faces
def group_bounding_box():
minx, miny, minz = (999999.0,)*3
maxx, maxy, maxz = (-999999.0,)*3
location = [0.0,]*3
for obj in bpy.context.selected_objects:
for v in obj.bound_box:
v_world = obj.matrix_world * mathutils.Vector((v[0],v[1],v[2]))
if v_world[0] < minx:
minx = v_world[0]
if v_world[0] > maxx:
maxx = v_world[0]
if v_world[1] < miny:
miny = v_world[1]
if v_world[1] > maxy:
maxy = v_world[1]
if v_world[2] < minz:
minz = v_world[2]
if v_world[2] > maxz:
maxz = v_world[2]
verts_loc, faces = add_box((maxx-minx)/2, (maxz-minz)/2, (maxy-miny)/2)
mesh = bpy.data.meshes.new("BoundingBox")
bm = bmesh.new()
for v_co in verts_loc:
bm.verts.new(v_co)
bm.verts.ensure_lookup_table()
for f_idx in faces:
bm.faces.new([bm.verts[i] for i in f_idx])
bm.to_mesh(mesh)
mesh.update()
location[0] = minx+((maxx-minx)/2)
location[1] = miny+((maxy-miny)/2)
location[2] = minz+((maxz-minz)/2)
bbox = object_utils.object_data_add(context, mesh, operator=None)
# does a bounding box need to display more than the bounds??
bbox.object.location = location
bbox.object.draw_type = 'BOUNDS'
bbox.object.hide_render = True
#select suzanne and the armature
bpy.data.objects['Suzanne'].select =True
bpy.data.objects['Armature'].select = True
group_bounding_box()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment