繁体   English   中英

Blender相机旋转与python - 不是平面?

[英]Blender camera rotation with python - not planar?

我正在尝试创建一个渲染对象360度旋转的脚本。 我根本不熟悉搅拌机,但我一直在努力做到这一点。

我正在编写一个使用Python的脚本,它导入一个STL文件并创建它的360渲染。 我的问题是,当移动相机时,相机不会在正确的平面上移动。

这是一张描述我的意思的图像专辑:

http://imgur.com/a/FkVLp

我用来做这个的功能是:

def rotateCameraAroundOrigin(rx=0):
    cam = bpy.data.objects['Camera']
    old_x = cam.location.x
    old_y = cam.location.y
    old_z = cam.location.z
    radius = math.sqrt((math.pow(old_x,2) + math.pow(old_y,2)))
    current_angle = math.degrees(math.atan2( old_y, old_x))
    print("CUR:\t%+04d degrees" % (current_angle))
    new_angle = current_angle + rx
    print("FROM:\t%+04d, %+04d, %+04d" % (old_x,old_y,old_z))
    new_x = radius * math.cos(math.radians(new_angle))
    new_y = radius * math.sin(math.radians(new_angle))
    moveCamTo(new_x,new_y,old_z)
    print("TO:\t%+04d, %+04d, %+04d\n" % (new_x,new_y,old_z))

接着:

for i in range(1, 10):
    print("Rotation %01d" % (i))
    image = 'images/' + sys.argv[-1] + str(i) + '.' + filetype
    rotateCameraAroundOrigin(36)
    render_thumb(image,gl=False)

这产生了imgur专辑中显示的输出:

Rotation 1
CUR:    +000 degrees
FROM:   +302, +000, +000
TO:     +244, +177, +000

Rotation 2
CUR:    +035 degrees
FROM:   +244, +177, +000
TO:     +093, +287, +000

Rotation 3
CUR:    +071 degrees
FROM:   +093, +287, +000
TO:     -093, +287, +000

Rotation 4
CUR:    +107 degrees
FROM:   -093, +287, +000
TO:     -244, +177, +000

Rotation 5
CUR:    +143 degrees
FROM:   -244, +177, +000
TO:     -302, +000, +000

Rotation 6
CUR:    +179 degrees
FROM:   -302, +000, +000
TO:     -244, -177, +000

Rotation 7
CUR:    -144 degrees
FROM:   -244, -177, +000
TO:     -093, -287, +000

Rotation 8
CUR:    -108 degrees
FROM:   -093, -287, +000
TO:     +093, -287, +000

Rotation 9
CUR:    -072 degrees
FROM:   +093, -287, +000
TO:     +244, -177, +000

我不知所措。 我的飞机是倾斜的,但我不知道为什么。 我想围绕物体顺畅地旋转。

谢谢你的帮助,

H

为什么不在对象位置创建一个空对象。 把相机放到它的父母那里。 然后将旋转矩阵应用于空。 父母将使相机旋转通过所需的旋转弧。

这种方法更加简单,主要用于速度建模,您希望摄像机查看整个对象的进度。

def parent_obj_to_camera(b_obj, b_camera):
    origin = (0,0,0) #can be replaced with b_obj.location
    b_empty = bpy.data.objects.new("Empty", None)
    b_empty.location = origin
    b_camera.parent = b_empty #setup parenting

    scn = bpy.context.scene
    scn.objects.link(b_empty)
    scn.objects.active = b_empty 
    b_empty.select = True

然后在那个for循环中你会创建一个矩阵来旋转空

import mathutils
from math import radians


b_empty = bpy.context.scene.active_object
num_steps = 9
stepsize = 360/num_steps
for i in range(0, num_steps):
    mat_rot = mathutils.Matrix.Rotation(radians(step), 4, 'X')
    b_empty.matrix_local *= mat_rot 

    print("Rotation %01d" % (radians(stepsize)))
    image = 'images/' + sys.argv[-1] + str(i) + '.' + filetype
    render_thumb(image,gl=False)

我正面临@ neomonkeus答案的一些问题。 我在他的代码中做了以下更改,它对我有用。

mat_rot=mathutils.Matrix.Rotation(radians(stepsize*(i+1)), 4, 'X')
b_empty.matrix_world = mat_rot
bpy.context.scene.update() # VERY IMPORTANT

不要忘记更新场景。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM