如何围绕obj波前文件内容旋转相机? [英] How to rotate a camera around obj wavefront file contents?

查看:100
本文介绍了如何围绕obj波前文件内容旋转相机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.obj文件.我不知道它的内容边界框是否在手.我想将其加载到Blender中,并在第"K"帧(例如15帧)中旋转相机.如何在使用Python API的Blender中做这样的事情?

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 such a thing in blender using python api?

推荐答案

进行对象周转的一种常见方法是添加一个空对象并将其设为相机的父对象,对空对象的z旋转进行动画处理然后旋转围绕对象的摄影机,您可以为摄影机设置跟踪约束,以便摄影机始终指向目标对象.

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.

您可以使用对象 bound_box 查找其外部限制,然后再添加一点,使对象停留在视图内,并以此定位摄像机.对于大多数对象来说,使额外的距离与对象的大小成正比即可.

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.

要在python中执行此操作-

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

这篇关于如何围绕obj波前文件内容旋转相机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆