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

2023-12-15

我有一个 .obj 文件。我事先不知道它的内容边界框。我想将它加载到搅拌机中并在“K”帧(例如 15 帧)中围绕它旋转相机。如何使用 python api 在搅拌机中做这样的事情?


进行对象翻转的常见方法是添加一个空物体并使其成为相机的父级,为空物体的 z 旋转设置动画,然后将相机围绕该物体旋转,您可以给相机一个 trackto 约束,以便相机始终指向目标物体。

您可以使用对象bound_box找到其外部限制,然后添加更多一点,使对象保持在视图内,并以此定位相机。使额外距离与对象大小成比例应该适用于大多数对象。

我制作的插件这个答案展示如何围绕多个对象创建边界框,如果您同时有多个对象,这可能会很有帮助。

要在 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐