fix for error reading gzip'd x3d/vrml files
[blender-addons.git] / modules / misc_utils.py
blob0eb60425f861159bbfae93da04e1aeed52c47b0e
1 # -*- coding: utf-8 -*-
2 # ##### BEGIN GPL LICENSE BLOCK #####
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software Foundation,
16 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # ##### END GPL LICENSE BLOCK #####
20 """
21 misc_util.py
23 Miscellaneous helper methods.
25 """
27 import bpy
28 from cursor_utils import *
29 from mathutils import Vector, Matrix
32 class BlenderFake:
34 @classmethod
35 def forceUpdate(cls):
36 if bpy.context.mode == 'EDIT_MESH':
37 bpy.ops.object.mode_set(mode='OBJECT')
38 bpy.ops.object.mode_set(mode='EDIT')
40 @classmethod
41 def forceRedraw(cls):
42 CursorAccess.setCursor(CursorAccess.getCursor())
45 # Converts 3D coordinates in a 3DRegion
46 # into 2D screen coordinates for that region.
47 # Borrowed from Buerbaum Martin (Pontiac)
48 def region3d_get_2d_coordinates(context, loc_3d):
49 # Get screen information
50 mid_x = context.region.width / 2.0
51 mid_y = context.region.height / 2.0
52 width = context.region.width
53 height = context.region.height
55 # Get matrices
56 view_mat = context.space_data.region_3d.perspective_matrix
57 total_mat = view_mat
59 # order is important
60 vec = total_mat * Vector((loc_3d[0], loc_3d[1], loc_3d[2], 1.0))
62 # dehomogenise
63 vec = Vector((
64 vec[0] / vec[3],
65 vec[1] / vec[3],
66 vec[2] / vec[3]))
68 x = int(mid_x + vec[0] * width / 2.0)
69 y = int(mid_y + vec[1] * height / 2.0)
70 z = vec[2]
72 return Vector((x, y, z))