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()