Cleanup: pep8
[blender-staging.git] / doc / python_api / examples / gpu.offscreen.1.py
1 # Draws an off-screen buffer and display it in the corner of the view.
2 import bpy
3 from bgl import *
4
5
6 class OffScreenDraw(bpy.types.Operator):
7     bl_idname = "view3d.offscreen_draw"
8     bl_label = "View3D Offscreen Draw"
9
10     _handle_calc = None
11     _handle_draw = None
12     is_enabled = False
13
14     # manage draw handler
15     @staticmethod
16     def draw_callback_px(self, context):
17         scene = context.scene
18         aspect_ratio = scene.render.resolution_x / scene.render.resolution_y
19
20         self._update_offscreen(context, self._offscreen)
21         self._opengl_draw(context, self._texture, aspect_ratio, 0.2)
22
23     @staticmethod
24     def handle_add(self, context):
25         OffScreenDraw._handle_draw = bpy.types.SpaceView3D.draw_handler_add(
26                 self.draw_callback_px, (self, context),
27                 'WINDOW', 'POST_PIXEL',
28                 )
29
30     @staticmethod
31     def handle_remove():
32         if OffScreenDraw._handle_draw is not None:
33             bpy.types.SpaceView3D.draw_handler_remove(OffScreenDraw._handle_draw, 'WINDOW')
34
35         OffScreenDraw._handle_draw = None
36
37     # off-screen buffer
38     @staticmethod
39     def _setup_offscreen(context):
40         import gpu
41         scene = context.scene
42         aspect_ratio = scene.render.resolution_x / scene.render.resolution_y
43
44         try:
45             offscreen = gpu.offscreen.new(512, int(512 / aspect_ratio))
46         except Exception as e:
47             print(e)
48             offscreen = None
49
50         return offscreen
51
52     @staticmethod
53     def _update_offscreen(context, offscreen):
54         scene = context.scene
55         render = scene.render
56         camera = scene.camera
57
58         modelview_matrix = camera.matrix_world.inverted()
59         projection_matrix = camera.calc_matrix_camera(
60                 render.resolution_x,
61                 render.resolution_y,
62                 render.pixel_aspect_x,
63                 render.pixel_aspect_y,
64                 )
65
66         offscreen.draw_view3d(
67                 scene,
68                 context.space_data,
69                 context.region,
70                 projection_matrix,
71                 modelview_matrix,
72                 )
73
74     @staticmethod
75     def _opengl_draw(context, texture, aspect_ratio, scale):
76         """
77         OpenGL code to draw a rectangle in the viewport
78         """
79
80         glDisable(GL_DEPTH_TEST)
81
82         # view setup
83         glMatrixMode(GL_PROJECTION)
84         glPushMatrix()
85         glLoadIdentity()
86
87         glMatrixMode(GL_MODELVIEW)
88         glPushMatrix()
89         glLoadIdentity()
90
91         glOrtho(-1, 1, -1, 1, -15, 15)
92         gluLookAt(0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
93
94         act_tex = Buffer(GL_INT, 1)
95         glGetIntegerv(GL_TEXTURE_2D, act_tex)
96
97         viewport = Buffer(GL_INT, 4)
98         glGetIntegerv(GL_VIEWPORT, viewport)
99
100         width = int(scale * viewport[2])
101         height = int(width / aspect_ratio)
102
103         glViewport(viewport[0], viewport[1], width, height)
104         glScissor(viewport[0], viewport[1], width, height)
105
106         # draw routine
107         glEnable(GL_TEXTURE_2D)
108         glActiveTexture(GL_TEXTURE0)
109
110         glBindTexture(GL_TEXTURE_2D, texture)
111
112         texco = [(1, 1), (0, 1), (0, 0), (1, 0)]
113         verco = [(1.0, 1.0), (-1.0, 1.0), (-1.0, -1.0), (1.0, -1.0)]
114
115         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
116
117         glColor4f(1.0, 1.0, 1.0, 1.0)
118
119         glBegin(GL_QUADS)
120         for i in range(4):
121             glTexCoord3f(texco[i][0], texco[i][1], 0.0)
122             glVertex2f(verco[i][0], verco[i][1])
123         glEnd()
124
125         # restoring settings
126         glBindTexture(GL_TEXTURE_2D, act_tex[0])
127
128         glDisable(GL_TEXTURE_2D)
129
130         # reset view
131         glMatrixMode(GL_PROJECTION)
132         glPopMatrix()
133
134         glMatrixMode(GL_MODELVIEW)
135         glPopMatrix()
136
137         glViewport(viewport[0], viewport[1], viewport[2], viewport[3])
138         glScissor(viewport[0], viewport[1], viewport[2], viewport[3])
139
140     # operator functions
141     @classmethod
142     def poll(cls, context):
143         return context.area.type == 'VIEW_3D'
144
145     def modal(self, context, event):
146         if context.area:
147             context.area.tag_redraw()
148
149         return {'PASS_THROUGH'}
150
151     def invoke(self, context, event):
152         if OffScreenDraw.is_enabled:
153             self.cancel(context)
154
155             return {'FINISHED'}
156
157         else:
158             self._offscreen = OffScreenDraw._setup_offscreen(context)
159             if self._offscreen:
160                 self._texture = self._offscreen.color_texture
161             else:
162                 self.report({'ERROR'}, "Error initializing offscreen buffer. More details in the console")
163                 return {'CANCELLED'}
164
165             OffScreenDraw.handle_add(self, context)
166             OffScreenDraw.is_enabled = True
167
168             if context.area:
169                 context.area.tag_redraw()
170
171             context.window_manager.modal_handler_add(self)
172             return {'RUNNING_MODAL'}
173
174     def cancel(self, context):
175         OffScreenDraw.handle_remove()
176         OffScreenDraw.is_enabled = False
177
178         if context.area:
179             context.area.tag_redraw()
180
181
182 def register():
183     bpy.utils.register_class(OffScreenDraw)
184
185
186 def unregister():
187     bpy.utils.unregister_class(OffScreenDraw)
188
189
190 if __name__ == "__main__":
191     register()