使用Python在Blender中改变视口角度

8

我想知道是否有一种使用Python在Blender中改变视口角度的方法。我希望得到像按下小键盘上的1、3或7键一样的结果。

非常感谢你的帮助。


bpy.ops.view3d.viewnumpad 是什么? - gandalf3
请参见http://blender.stackexchange.com/a/678/2762。 - Andre Holzner
2个回答

9

首先,注意您可以同时打开多个3D视图,每个视图都可以具有自己的视口角度、透视/正交设置等。因此,您的脚本将需要查找可能存在的所有3D视图(可能没有),并决定它将影响哪一个或哪些。

bpy.data对象开始,它有一个window_managers属性。这个集合似乎总是只有一个元素。但是,可能会有一个或多个打开的windows。每个窗口都有一个screen,它被分成一个或多个areas。因此,您需要搜索所有区域,找到其中一个具有“VIEW_3D”type空间。然后在该区域的spaces中寻找类型为“VIEW_3D”的一个或多个空间。这样的空间将是SpaceView3D子类。它将具有一个region_3d属性,类型为RegionView3D。最后,这个对象又有一个名为view_matrix的属性,它接受Matrix类型的值,您可以获取或设置。

都懂了吗?:)


8
一旦您找到了正确的“视图”,您可以修改以下内容:
view.spaces[0].region_3d.view_matrix
view.spaces[0].region_3d.view_rotation

请注意,region_3d.view_location是“look_at”目标,而不是相机位置;如果要移动相机的位置(据我所知),您必须直接修改view_matrix,但是您可以使用view_rotation轻松微调旋转。 但是,生成有效四元数可能需要阅读此内容:http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation 也许类似这样的东西会有用:
class Utils(object):

  def __init__(self, context):
    self.context = context

  @property
  def views(self):
    """ Returns the set of 3D views.
    """
    rtn = []
    for a in self.context.window.screen.areas:
      if a.type == 'VIEW_3D':
        rtn.append(a)
    return rtn

  def camera(self, view):
    """ Return position, rotation data about a given view for the first space attached to it """
    look_at = view.spaces[0].region_3d.view_location
    matrix = view.spaces[0].region_3d.view_matrix
    camera_pos = self.camera_position(matrix)
    rotation = view.spaces[0].region_3d.view_rotation
    return look_at, camera_pos, rotation

  def camera_position(self, matrix):
    """ From 4x4 matrix, calculate camera location """
    t = (matrix[0][3], matrix[1][3], matrix[2][3])
    r = (
      (matrix[0][0], matrix[0][1], matrix[0][2]),
      (matrix[1][0], matrix[1][1], matrix[1][2]),
      (matrix[2][0], matrix[2][1], matrix[2][2])
    )
    rp = (
      (-r[0][0], -r[1][0], -r[2][0]),
      (-r[0][1], -r[1][1], -r[2][1]),
      (-r[0][2], -r[1][2], -r[2][2])
    )
    output = (
      rp[0][0] * t[0] + rp[0][1] * t[1] + rp[0][2] * t[2],
      rp[1][0] * t[0] + rp[1][1] * t[1] + rp[1][2] * t[2],
      rp[2][0] * t[0] + rp[2][1] * t[1] + rp[2][2] * t[2],
    )
    return output

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接