3

New to blender here, as the title suggests how would I use blender to do this without resorting to mathematical calculations?

To be clear center of geometry of a group of objects means the center of a box fitting the furtherest bounds of all objects.

EDIT: Yes, this is using python, I'm automating my renders

  • There is no tool to do exactly what you're asking for, as you have worded it. However I think this answer, may in practice do what you want. - http://blender.stackexchange.com/questions/51563/how-to-automatically-fit-the-camera-to-objects-in-the-view/51566#51566 I think this question may be being unnecessarily spesific, if you do want to perform this operation, exactly as worded, then you would need to write a Python script to perform the task. – ideasman42 May 28 '16 at 04:42
  • @ideasman42 sorry forgot to add that I would be using Python scripting. Thanks for the link, it sort of is what I'm looking for but I would still like to point the camera to the geometric center of objects. – BlenderGreenhorn May 28 '16 at 04:55

1 Answers1

4

This is possible, but you will have to calculate the center.

The example below uses a rough approximation based on the volume of the objects bounding boxes.

from bpy import context
from mathutils import Vector


def point_the_camera(obj_camera, look_at, epsilon=1e-8):
    camera_matrix = obj_camera.matrix_world.copy()
    view_axis_src = camera_matrix.col[2].xyz.normalized()
    view_axis_dst = camera_matrix.to_translation() - look_at

    if view_axis_dst.length_squared < epsilon:
        print("looking into ourselves!")
        return
    view_axis_dst.normalize()

    axis = view_axis_src.cross(view_axis_dst)
    if axis.length_squared < epsilon:
        print("already aligned!")
        return

    axis.normalize()

    from mathutils import Matrix
    camera_matrix_rot = Matrix.Rotation(view_axis_src.angle(view_axis_dst), 3, axis)
    obj_camera.matrix_world = (
            Matrix.Translation(camera_matrix.to_translation()) *
            (camera_matrix_rot * camera_matrix.to_3x3()).to_4x4())


def derived_objects(scene, obj_base):
    obj_base.dupli_list_create(scene)
    obj_matrix_list = (
        [(obj_base, obj_base.matrix_world.copy())] +
        [(dob.object, dob.matrix.copy()) for dob in obj_base.dupli_list]
        )
    obj_base.dupli_list_clear()
    return obj_matrix_list


def main():

    scene = context.scene
    obj = context.object
    obj_camera = scene.camera

    center_accum = Vector()
    center_accum_volume = 0.0

    # objects without a bound-box (camera, lamp, etc).
    # have zero volume so won't influence the outcome.
    for obj_iter, matrix in derived_objects(scene, obj):
        print(matrix)
        box = obj_iter.bound_box

        # diagonal
        b_0 = matrix * Vector(box[0])
        b_1 = matrix * Vector(box[6])

        diag = b_0 - b_1
        diag_volume = diag.length ** 3.0
        diag_cent = ((b_0 + b_1) / 2) * diag_volume

        center_accum += diag_cent
        center_accum_volume += diag_volume

    # we have our center
    center = center_accum * (1.0 / center_accum_volume)

    ## Quick test
    # scene.cursor_location = center

    point_the_camera(obj_camera, center)


main()
ideasman42
  • 47,387
  • 10
  • 141
  • 223