import bpy
from mathutils import Vector
def main():
# specify the object
ob = bpy.context.object
# change to your desired output location and file name
filepath = r"D:\temp\bounds_2d.txt"
scene = bpy.context.scene
frame_current = scene.frame_current
frame_start = scene.frame_start
frame_end = scene.frame_end
write_bounds_3d(filepath, ob, frame_start, frame_end)
scene.frame_set(frame_current)
def write_bounds_3d(filepath, ob, frame_start, frame_end):
file =with open(filepath, "w") as file:
for frame in range(frame_start, frame_end + 1):
bpy.context.scene.frame_set(frame)
file.write("%f %f %f %f\n" % get_bounds(ob))
file.close()
def get_bounds(ob):
mat = ob.matrix_world
bbox = [mat * Vector(b) for b in ob.bound_box]
min_x = min(b.x for b in bbox)
max_x = max(b.x for b in bbox)
min_y = min(b.y for b in bbox)
max_y = max(b.y for b in bbox)
top_left = Vector((min_x, max_y))
bottom_right = Vector((max_x, min_y))
# make flat tuple, x y width height
return top_left.to_tuple() + tuple(map(abs, (top_left - bottom_right)))
if __name__ == '__main__':
main()
Wrap the 'open file' in a 'with' statement. This makes sure the file object gets closed, even if the 'get_bounds' function throws an error.
jasperge
- 1.9k
- 14
- 17