1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
22 def _is_persp_matrix(persmat, eps=0.00001):
24 crummy way to check if its a perspective matrix
27 abs(persmat[0][3]) < eps and \
28 abs(persmat[1][3]) < eps and \
29 abs(persmat[2][3]) < eps and \
30 abs(persmat[3][3] - 1.0) < eps)
33 def region_2d_to_vector_3d(region, rv3d, coord):
35 Return a direction vector from the viewport at the spesific 2d region
38 :arg region: region of the 3D viewport, typically bpy.context.region.
39 :type region: :class:`Region`
40 :arg rv3d: 3D region data, typically bpy.context.space_data.region_3d.
41 :type rv3d: :class:`RegionView3D`
42 :arg coord: 2d coordinates relative to the region;
43 (event.mouse_region_x, event.mouse_region_y) for example.
44 :type coord: 2d vector
45 :return: normalized 3d vector.
46 :rtype: :class:`Vector`
48 from mathutils import Vector
50 persmat = rv3d.perspective_matrix.copy()
51 viewvec = rv3d.view_matrix.inverted()[2].xyz.normalized()
53 if _is_persp_matrix(persmat):
54 dx = (2.0 * coord[0] / region.width) - 1.0
55 dy = (2.0 * coord[1] / region.height) - 1.0
57 perspinv_x, perspinv_y = persmat.inverted().to_3x3()[0:2]
58 return ((perspinv_x * dx + perspinv_y * dy) - viewvec).normalized()
63 def region_2d_to_location_3d(region, rv3d, coord, depth_location):
65 Return a 3d location from the region relative 2d coords, aligned with
68 :arg region: region of the 3D viewport, typically bpy.context.region.
69 :type region: :class:`Region`
70 :arg rv3d: 3D region data, typically bpy.context.space_data.region_3d.
71 :type rv3d: :class:`RegionView3D`
72 :arg coord: 2d coordinates relative to the region;
73 (event.mouse_region_x, event.mouse_region_y) for example.
74 :type coord: 2d vector
75 :arg depth_location: the returned vectors depth is aligned with this since
76 there is no defined depth with a 2d region input.
77 :type depth_location: 3d vector
78 :return: normalized 3d vector.
79 :rtype: :class:`Vector`
81 from mathutils.geometry import intersect_point_line
83 persmat = rv3d.perspective_matrix.copy()
85 if _is_persp_matrix(persmat):
86 origin_start = rv3d.view_matrix.inverted()[3].to_3d()
88 dx = (2.0 * coord[0] / region.width) - 1.0
89 dy = (2.0 * coord[1] / region.height) - 1.0
90 persinv = persmat.inverted()
91 viewinv = rv3d.view_matrix.inverted()
92 origin_start = (persinv[0].xyz * dx) + (persinv[1].xyz * dy) + viewinv[3].xyz
94 origin_end = origin_start + region_2d_to_vector_3d(region, rv3d, coord)
96 return intersect_point_line(depth_location, origin_start, origin_end)[0]
99 def location_3d_to_region_2d(region, rv3d, coord):
101 Return the *region* relative 2d location of a 3d position.
103 :arg region: region of the 3D viewport, typically bpy.context.region.
104 :type region: :class:`Region`
105 :arg rv3d: 3D region data, typically bpy.context.space_data.region_3d.
106 :type rv3d: :class:`RegionView3D`
107 :arg coord: 3d worldspace location.
108 :type coord: 3d vector
110 :rtype: :class:`Vector`
112 prj = Vector((coord[0], coord[1], coord[2], 1.0)) * rv3d.perspective_matrix
114 width_half = region.width / 2.0
115 height_half = region.height / 2.0
117 return Vector((width_half + width_half * (prj.x / prj.w),
118 height_half + height_half * (prj.y / prj.w),