Skip to main content
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.
Source Link
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()
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 = open(filepath, "w")
    
     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()
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):
    
    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))

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()
added 4 characters in body
Source Link
CodeManX
  • 29.6k
  • 3
  • 92
  • 131
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 = open(filepath, "w")
    
    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 tuple(top_left.to_tuple() + tuple(map(abs, (top_left - bottom_right)))

if __name__ == '__main__':
    main()
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 = open(filepath, "w")
    
    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 tuple(top_left) + tuple(map(abs, (top_left - bottom_right)))

if __name__ == '__main__':
    main()
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 = open(filepath, "w")
    
    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()
added 1525 characters in body
Source Link
CodeManX
  • 29.6k
  • 3
  • 92
  • 131

And here's some code to write out what you asked for (front view):

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 = open(filepath, "w")
    
    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 tuple(top_left) + tuple(map(abs, (top_left - bottom_right)))

if __name__ == '__main__':
    main()

And here's some code to write out what you asked for (front view):

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 = open(filepath, "w")
    
    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 tuple(top_left) + tuple(map(abs, (top_left - bottom_right)))

if __name__ == '__main__':
    main()
Source Link
CodeManX
  • 29.6k
  • 3
  • 92
  • 131
Loading