Skip to content Skip to sidebar Skip to footer

How To Rotate A Camera Around Obj Wavefront File Contents?

I have an .obj file. I do not know it’s contents bounding box before hand. I want to load it into blender and rotate camera around it in 'K'th frames (e.g. 15 frames). How to do

Solution 1:

A common way to do an object turnaround is to add an empty and make it the parent of the camera, animating the z-rotation of the empty will then rotate the camera around the object, you can give the camera a trackto constraint so that the camera always points at the target object.

You can use the objects bound_box to find its outer limits, then add a bit more so the object stays inside the view and position the camera with that. Making the extra distance proportional to the object size should work for most objects.

The addon I made for this answer shows how to make a bounding box around multiple objects, which may be helpful if you have multiple objects at once.

To do that in python -

import bpy
scn = bpy.context.scene

bpy.ops.import_scene.obj(filepath='obj1.obj')
target = bpy.context.selected_objects[0]
scn.objects.active = target
# centring the origin gives a better bounding box and rotation point
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')

cam_x_pos = max([v[0] for v in target.bound_box]) * 2.5
cam_y_pos = max([v[1] for v in target.bound_box]) * 2.5
cam_z_pos = max([v[2] for v in target.bound_box]) * 2.5

rot_centre = bpy.data.objects.new('rot_centre', None)
scn.objects.link(rot_centre)
rot_centre.location = target.location

camera = bpy.data.objects.new('camera', bpy.data.cameras.new('camera'))
scn.objects.link(camera)
camera.location = (cam_x_pos, cam_y_pos, cam_z_pos)
camera.parent = rot_centre
m = camera.constraints.new('TRACK_TO')
m.target = target
m.track_axis = 'TRACK_NEGATIVE_Z'
m.up_axis = 'UP_Y'

rot_centre.rotation_euler.z = 0.0
rot_centre.keyframe_insert('rotation_euler', index=2, frame=1)
rot_centre.rotation_euler.z = radians(360.0)
rot_centre.keyframe_insert('rotation_euler', index=2, frame=101)
# set linear interpolation for constant rotation speedfor c in rot_centre.animation_data.action.fcurves:
    for k in c.keyframe_points:
        k.interpolation = 'LINEAR'
scn.frame_end = 100

Post a Comment for "How To Rotate A Camera Around Obj Wavefront File Contents?"