You just need a few changes (6 lines in total) from the code you linked to achieve that.
There is just one little problem with the method you suggest and I will explain that to you in the end.
- I changed the line
object= bpy.data.objects['Cube']
to object= bpy.context.object
just to have the script operate on the active object instead of the default Cube object. This is optional but I wanted to use this for faster testing.
- Then, the line
mesh = object.data
to mesh = object.bound_box
because I need to work on the bounding box and not on the mesh data (where the vertices are stored).
- Then I changed
numVertices = len(object.data.vertices)
to numVertices = len(mesh)
. Remember, mesh
is now an array that contains the coordinates of the corners of the object's bounding box (You probably want to change the names of those variables by more significative names but for the sake of this answer, I'm going to use the same names as the original code).
co = mesh.vertices[t].co
becomes co= mesh[t]
.
pos = (col0 * co.x) + (col1 * co.y) + (col2 * co.z) + col3
is changed to pos = (col0 * co[0]) + (col1 * co[1]) + (col2 * co[2]) + col3
because each element of object.bound_box
is stored as an array not a vector3.
- I also changed
object= bpy.data.objects['Cube']
to object= bpy.context.object
in the function UpdateFrame(scene)
.
This is the full code (I removed the comments to avoid making it to long):
import bpy
import bpy_extras.object_utils
UpdateFrames = 1
def UpdateBorder(camera, scene, object):
print("\nUpdateBorder Frame "+str(scene.frame_current))
matrix = object.matrix_world
mesh = object.bound_box
col0 = matrix.col[0]
col1 = matrix.col[1]
col2 = matrix.col[2]
col3 = matrix.col[3]
minX = 1
maxX = 0
minY = 1
maxY = 0
numVertices = len(mesh)
for t in range(0, numVertices):
co = mesh[t]
pos = (col0 * co[0]) + (col1 * co[1]) + (col2 * co[2]) + col3
pos = bpy_extras.object_utils.world_to_camera_view(scene, camera, pos)
if (pos.x < minX):
minX = pos.x
if (pos.y < minY):
minY = pos.y
if (pos.x > maxX):
maxX = pos.x
if (pos.y > maxY):
maxY = pos.y
render = scene.render
pMinX = str(int(minX*render.resolution_x))
pMinY = str(int(minY*render.resolution_y))
pMaxX = str(int(maxX*render.resolution_x))
pMaxY = str(int(maxY*render.resolution_y))
print(" ("+pMinX+", "+pMinY+") - ("+pMaxX+", "+pMaxY+")")
render.border_min_x = minX
render.border_min_y = minY
render.border_max_x = maxX
render.border_max_y = maxY
def UpdateFrame(scene):
camera = bpy.data.objects['Camera']
object= bpy.context.object
UpdateBorder(camera, scene, object)
print("Initializing")
scene = bpy.context.scene
camera = bpy.data.objects['Camera']
object= bpy.context.object
UpdateBorder(camera, scene, object)
bpy.app.handlers.frame_change_post.clear()
bpy.app.handlers.frame_change_post.append(UpdateFrame)
This code works as intended but as I told you in the begining, this doesn't give you exactly, depending on the camera angle, the same results as the first code and here is why:
The (projected) 2D bounding box of a mesh and the 2D bounding box of that mesh's bounding box are not the same. It depends on the camera angle. Just try to run that code with Suzanne and you'll see :

while the previous code gives this :

which have a render border that fits our favorite monkey perfectly.
To understand what's happening, just enable Suzanne's border box in the Object properties :


There you have it. The method you suggested can be done and would definitely be faster, especially when involving a lot of objects, but keep in mind that it doesn't give as good a result as the original code.
I hope I helped you in some way.