如何围绕obj波前文件内容旋转相机?

时间:2018-05-23 08:04:24

标签: python blender

我有一个.obj文件。我手头之前不知道它的内容是什么。我想将它装入搅拌机并在“K”帧(例如15帧)内围绕它旋转相机。如何使用python api在blender中做这样的事情?

1 个答案:

答案 0 :(得分:1)

执行对象周转的常用方法是添加一个空并使其成为摄影机的父级,为空的z旋转设置动画然后围绕对象旋转摄像机,可以为摄像机提供轨道约束这样相机总是指向目标物体。

您可以使用对象bound_box查找其外部限制,然后再添加一点,以便对象停留在视图中并使用该对象定位相机。使对象大小成比例的额外距离应适用于大多数对象。

我为this answer制作的插件显示了如何围绕多个对象创建一个边界框,如果您同时拥有多个对象,这可能会有所帮助。

在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 speed
for c in rot_centre.animation_data.action.fcurves:
    for k in c.keyframe_points:
        k.interpolation = 'LINEAR'
scn.frame_end = 100