~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

Viewing changes to release/scripts/bpymodules/BPyWindow.py

  • Committer: Bazaar Package Importer
  • Author(s): Lukas Fittl
  • Date: 2006-09-20 01:57:27 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20060920015727-gmoqlxwstx9wwqs3
Tags: 2.42a-1ubuntu1
* Merge from Debian unstable (Closes: Malone #55903). Remaining changes:
  - debian/genpot: Add python scripts from Lee June <blender@eyou.com> to
    generate a reasonable PO template from the sources. Since gettext is used
    in a highly nonstandard way, xgettext does not work for this job.
  - debian/rules: Call the scripts, generate po/blender.pot, and clean it up
    in the clean target.
  - Add a proper header to the generated PO template.
* debian/control: Build depend on libavformat-dev >= 3:0.cvs20060823-3.1,
  otherwise this package will FTBFS

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import Blender
 
2
from Blender import Mathutils, Window, Scene, Draw, Mesh
 
3
from Blender.Mathutils import CrossVecs, Matrix, Vector, Intersect, LineIntersect
 
4
 
 
5
 
 
6
# DESCRIPTION:
 
7
# screen_x, screen_y the origin point of the pick ray
 
8
# it is either the mouse location
 
9
# localMatrix is used if you want to have the returned values in an objects localspace.
 
10
#    this is usefull when dealing with an objects data such as verts.
 
11
# or if useMid is true, the midpoint of the current 3dview
 
12
# returns
 
13
# Origin - the origin point of the pick ray
 
14
# Direction - the direction vector of the pick ray
 
15
# in global coordinates
 
16
epsilon = 1e-3 # just a small value to account for floating point errors
 
17
 
 
18
def mouseViewRay(screen_x, screen_y, localMatrix=None, useMid = False):
 
19
        
 
20
        # Constant function variables
 
21
        p = mouseViewRay.p
 
22
        d = mouseViewRay.d
 
23
        
 
24
        for win3d in Window.GetScreenInfo(Window.Types.VIEW3D): # we search all 3dwins for the one containing the point (screen_x, screen_y) (could be the mousecoords for example) 
 
25
                win_min_x, win_min_y, win_max_x, win_max_y = win3d['vertices']
 
26
                # calculate a few geometric extents for this window
 
27
 
 
28
                win_mid_x  = (win_max_x + win_min_x + 1.0) * 0.5
 
29
                win_mid_y  = (win_max_y + win_min_y + 1.0) * 0.5
 
30
                win_size_x = (win_max_x - win_min_x + 1.0) * 0.5
 
31
                win_size_y = (win_max_y - win_min_y + 1.0) * 0.5
 
32
 
 
33
                #useMid is for projecting the coordinates when we subdivide the screen into bins
 
34
                if useMid: # == True
 
35
                        screen_x = win_mid_x
 
36
                        screen_y = win_mid_y
 
37
                
 
38
                # if the given screencoords (screen_x, screen_y) are within the 3dwin we fount the right one...
 
39
                if (win_max_x > screen_x > win_min_x) and (  win_max_y > screen_y > win_min_y):
 
40
                        # first we handle all pending events for this window (otherwise the matrices might come out wrong)
 
41
                        Window.QHandle(win3d['id'])
 
42
                        
 
43
                        # now we get a few matrices for our window...
 
44
                        # sorry - i cannot explain here what they all do
 
45
                        # - if you're not familiar with all those matrices take a look at an introduction to OpenGL...
 
46
                        pm      = Window.GetPerspMatrix()   # the prespective matrix
 
47
                        pmi  = Matrix(pm); pmi.invert() # the inverted perspective matrix
 
48
                        
 
49
                        if (1.0 - epsilon < pmi[3][3] < 1.0 + epsilon):
 
50
                                # pmi[3][3] is 1.0 if the 3dwin is in ortho-projection mode (toggled with numpad 5)
 
51
                                hms = mouseViewRay.hms
 
52
                                ortho_d = mouseViewRay.ortho_d
 
53
                                
 
54
                                # ortho mode: is a bit strange - actually there's no definite location of the camera ...
 
55
                                # but the camera could be displaced anywhere along the viewing direction.
 
56
                                
 
57
                                ortho_d.x, ortho_d.y, ortho_d.z = Window.GetViewVector()
 
58
                                ortho_d.w = 0
 
59
                                
 
60
                                # all rays are parallel in ortho mode - so the direction vector is simply the viewing direction
 
61
                                #hms.x, hms.y, hms.z, hms.w = (screen_x-win_mid_x) /win_size_x, (screen_y-win_mid_y) / win_size_y, 0.0, 1.0
 
62
                                hms[:] = (screen_x-win_mid_x) /win_size_x, (screen_y-win_mid_y) / win_size_y, 0.0, 1.0
 
63
                                
 
64
                                # these are the homogenious screencoords of the point (screen_x, screen_y) ranging from -1 to +1
 
65
                                p=(hms*pmi) + (1000*ortho_d)
 
66
                                p.resize3D()
 
67
                                d[:] = ortho_d[:3]
 
68
                                
 
69
 
 
70
                        # Finally we shift the position infinitely far away in
 
71
                        # the viewing direction to make sure the camera if outside the scene
 
72
                        # (this is actually a hack because this function
 
73
                        # is used in sculpt_mesh to initialize backface culling...)
 
74
                        else:
 
75
                                # PERSPECTIVE MODE: here everything is well defined - all rays converge at the camera's location
 
76
                                vmi  = Matrix(Window.GetViewMatrix()); vmi.invert() # the inverse viewing matrix
 
77
                                fp = mouseViewRay.fp
 
78
                                
 
79
                                dx = pm[3][3] * (((screen_x-win_min_x)/win_size_x)-1.0) - pm[3][0]
 
80
                                dy = pm[3][3] * (((screen_y-win_min_y)/win_size_y)-1.0) - pm[3][1]
 
81
                                
 
82
                                fp[:] = \
 
83
                                pmi[0][0]*dx+pmi[1][0]*dy,\
 
84
                                pmi[0][1]*dx+pmi[1][1]*dy,\
 
85
                                pmi[0][2]*dx+pmi[1][2]*dy
 
86
                                
 
87
                                # fp is a global 3dpoint obtained from "unprojecting" the screenspace-point (screen_x, screen_y)
 
88
                                #- figuring out how to calculate this took me quite some time.
 
89
                                # The calculation of dxy and fp are simplified versions of my original code
 
90
                                #- so it's almost impossible to explain what's going on geometrically... sorry
 
91
                                
 
92
                                p[:] = vmi[3][:3]
 
93
                                
 
94
                                # the camera's location in global 3dcoords can be read directly from the inverted viewmatrix
 
95
                                #d.x, d.y, d.z =normalize_v3(sub_v3v3(p, fp))
 
96
                                d[:] = p.x-fp.x, p.y-fp.y, p.z-fp.z
 
97
                                
 
98
                                #print 'd', d, 'p', p, 'fp', fp
 
99
                                
 
100
                        
 
101
                        # the direction vector is simply the difference vector from the virtual camera's position
 
102
                        #to the unprojected (screenspace) point fp
 
103
                        
 
104
                        # Do we want to return a direction in object's localspace?
 
105
                        
 
106
                        if localMatrix:
 
107
                                localInvMatrix = Matrix(localMatrix)
 
108
                                localInvMatrix.invert()
 
109
                                p = p*localInvMatrix
 
110
                                d = d*localInvMatrix # normalize_v3
 
111
                                p.x += localInvMatrix[3][0]
 
112
                                p.y += localInvMatrix[3][1]
 
113
                                p.z += localInvMatrix[3][2]
 
114
                                
 
115
                        #else: # Worldspace, do nothing
 
116
                        
 
117
                        d.normalize()
 
118
                        return True, p, d # Origin, Direction   
 
119
        
 
120
        # Mouse is not in any view, return None.
 
121
        return False, None, None
 
122
 
 
123
# Constant function variables
 
124
mouseViewRay.d = Vector(0,0,0) # Perspective, 3d
 
125
mouseViewRay.p = Vector(0,0,0)
 
126
mouseViewRay.fp = Vector(0,0,0)
 
127
 
 
128
mouseViewRay.hms = Vector(0,0,0,0) # ortho only 4d
 
129
mouseViewRay.ortho_d = Vector(0,0,0,0) # ortho only 4d
 
130
 
 
131
def spaceRect():
 
132
        '''
 
133
        Returns the space rect
 
134
        xmin,ymin,width,height
 
135
        '''
 
136
        
 
137
        __UI_RECT__ = Blender.BGL.Buffer(Blender.BGL.GL_FLOAT, 4)
 
138
        Blender.BGL.glGetFloatv(Blender.BGL.GL_SCISSOR_BOX, __UI_RECT__) 
 
139
        __UI_RECT__ = __UI_RECT__.list
 
140
        __UI_RECT__ = int(__UI_RECT__[0]), int(__UI_RECT__[1]), int(__UI_RECT__[2])-1, int(__UI_RECT__[3]) 
 
141
        
 
142
        return __UI_RECT__
 
143
 
 
144
def mouseRelativeLoc2d(__UI_RECT__= None):
 
145
        if not __UI_RECT__:
 
146
                __UI_RECT__ = spaceRect()
 
147
        
 
148
        mco = Window.GetMouseCoords()
 
149
        if      mco[0] > __UI_RECT__[0] and\
 
150
        mco[1] > __UI_RECT__[1] and\
 
151
        mco[0] < __UI_RECT__[0] + __UI_RECT__[2] and\
 
152
        mco[1] < __UI_RECT__[1] + __UI_RECT__[3]:
 
153
        
 
154
                return (mco[0] - __UI_RECT__[0], mco[1] - __UI_RECT__[1])
 
155
                
 
156
        else:
 
157
                return None
 
158
        
 
159
 
 
160
 
 
161
 
 
162
        
 
163
 
 
164
 
 
165
 
 
166
        
 
 
b'\\ No newline at end of file'