如何用Python在Blender 2.61中移动相机

15 投票
3 回答
23272 浏览
提问于 2025-04-17 10:15

我在找一个简单的脚本,用Python在Blender 2.61中移动相机。

我以为这会是个简单的任务,但相机对象没有像loc这样的属性,或者类似的东西。

我只在网上找到了一些Blender 2.49的脚本,但因为Blender 2.5进行了很大的API改动,这些脚本现在都不管用了。

如果有人能给点提示,我会很感激。

3 个回答

0

也许这个页面底部的相机模型可以作为一个不错的起点。

16

furtelwart的回答非常有用。我进一步研究了一下,发现你还可以设置一些关于相机和渲染的其他非常有用的属性。

import bpy

tx = 0.0
ty = 0.0
tz = 80.0

rx = 0.0
ry = 0.0
rz = 0.0

fov = 50.0

pi = 3.14159265

scene = bpy.data.scenes["Scene"]

# Set render resolution
scene.render.resolution_x = 480
scene.render.resolution_y = 359

# Set camera fov in degrees
scene.camera.data.angle = fov*(pi/180.0)

# Set camera rotation in euler angles
scene.camera.rotation_mode = 'XYZ'
scene.camera.rotation_euler[0] = rx*(pi/180.0)
scene.camera.rotation_euler[1] = ry*(pi/180.0)
scene.camera.rotation_euler[2] = rz*(pi/180.0)

# Set camera translation
scene.camera.location.x = tx
scene.camera.location.y = ty
scene.camera.location.z = tz

我正在使用这个脚本来进行批量渲染。你可以在这里查看: http://code.google.com/p/encuadro/source/browse/renders/marker/model/marker_a4.py

这个脚本以后会改进,支持命令行参数。我对python和blender还很陌生,所以这可能有点业余,但它确实能工作。

7

在reddit上,有位热心用户给我指了一条正确的解决方案:诀窍在于把相机当作一个Object来获取,而不是直接作为Camera。这样的话,你就可以通过常规的方法来设置位置,并且可以设置关键帧。

如果你想设置一些特定于Camera的对象,就需要通过bpy.data.cameras来获取。

import bpy

if(len(bpy.data.cameras) == 1):
    obj = bpy.data.objects['Camera'] # bpy.types.Camera
    obj.location.x = 0.0
    obj.location.y = -10.0
    obj.location.z = 10.0
    obj.keyframe_insert(data_path="location", frame=10.0)
    obj.location.x = 10.0
    obj.location.y = 0.0
    obj.location.z = 5.0
    obj.keyframe_insert(data_path="location", frame=20.0)

撰写回答