[Bf-blender-cvs] [b02b543] framebuffer: Minor updates to the example

Campbell Barton noreply at git.blender.org
Tue Oct 20 03:57:43 CEST 2015


Commit: b02b54323f0284b70140d99c9986d75a317a80e2
Author: Campbell Barton
Date:   Tue Oct 20 12:47:44 2015 +1100
Branches: framebuffer
https://developer.blender.org/rBb02b54323f0284b70140d99c9986d75a317a80e2

Minor updates to the example

- Add cancel() callback so exiting doesn't leak memory.
- Use staticmethods where 'self' isnt used.
- One line comment explaining what it does.

===================================================================

M	doc/python_api/examples/gpu.offscreen.1.py

===================================================================

diff --git a/doc/python_api/examples/gpu.offscreen.1.py b/doc/python_api/examples/gpu.offscreen.1.py
index 071b88e..2febb97 100644
--- a/doc/python_api/examples/gpu.offscreen.1.py
+++ b/doc/python_api/examples/gpu.offscreen.1.py
@@ -1,87 +1,56 @@
+# Draws an off-screen buffer and display it in the corner of the view.
 import bpy
 from bgl import *
 
-class VIEW3D_OT_OffScreenDraw(bpy.types.Operator):
-    """"""
+
+class OffScreenDraw(bpy.types.Operator):
     bl_idname = "view3d.offscreen_draw"
-    bl_label = "Offscreen Draw"
+    bl_label = "View3D Offscreen Draw"
 
     _handle_calc = None
     _handle_draw = None
     is_enabled = False
 
-    @classmethod
-    def poll(cls, context):
-        return context.area.type == 'VIEW_3D'
+    # manage draw handler
+    @staticmethod
+    def draw_callback_px(self, context):
+        scene = context.scene
+        aspect_ratio = scene.render.resolution_x / scene.render.resolution_y
+
+        self._update_offscreen(context, self._offscreen)
+        self._opengl_draw(context, self._texture, aspect_ratio, 0.2)
 
     @staticmethod
     def handle_add(self, context):
-        VIEW3D_OT_OffScreenDraw._handle_draw = bpy.types.SpaceView3D.draw_handler_add(
-            self.draw_callback_px, (context, ), 'WINDOW', 'POST_PIXEL')
+        OffScreenDraw._handle_draw = bpy.types.SpaceView3D.draw_handler_add(
+                self.draw_callback_px, (self, context),
+                'WINDOW', 'POST_PIXEL',
+                )
 
     @staticmethod
     def handle_remove():
-        if VIEW3D_OT_OffScreenDraw._handle_draw is not None:
-            bpy.types.SpaceView3D.draw_handler_remove(VIEW3D_OT_OffScreenDraw._handle_draw, 'WINDOW')
-
-        VIEW3D_OT_OffScreenDraw._handle_draw = None
-
-    def modal(self, context, event):
-        if context.area:
-            context.area.tag_redraw()
+        if OffScreenDraw._handle_draw is not None:
+            bpy.types.SpaceView3D.draw_handler_remove(OffScreenDraw._handle_draw, 'WINDOW')
 
-        return {'PASS_THROUGH'}
+        OffScreenDraw._handle_draw = None
 
-    def invoke(self, context, event):
-        if VIEW3D_OT_OffScreenDraw.is_enabled:
-            VIEW3D_OT_OffScreenDraw.handle_remove()
-            VIEW3D_OT_OffScreenDraw.is_enabled = False
-
-            if context.area:
-                context.area.tag_redraw()
-
-            return {'FINISHED'}
-
-        else:
-            if not self.init(context):
-                self.report({'ERROR'}, "Error initializing offscreen buffer. More details in the console")
-                return {'CANCELLED'}
-
-            VIEW3D_OT_OffScreenDraw.handle_add(self, context)
-            VIEW3D_OT_OffScreenDraw.is_enabled = True
-
-            if context.area:
-                context.area.tag_redraw()
-
-            context.window_manager.modal_handler_add(self)
-            return {'RUNNING_MODAL'}
-
-    def init(self, context):
+    # off-screen buffer
+    @staticmethod
+    def _setup_offscreen(context):
         import gpu
         scene = context.scene
         aspect_ratio = scene.render.resolution_x / scene.render.resolution_y
 
         try:
-            self._offscreen = gpu.offscreen.new(512, int(512 / aspect_ratio), 0)
-            self._texture = self._offscreen.color_texture
-
-        except Exception as E:
-            print(E)
-            return False
+            offscreen = gpu.offscreen.new(512, int(512 / aspect_ratio))
+        except Exception as e:
+            print(e)
+            offscreen = None
 
-        if not self._offscreen:
-            return False
+        return offscreen
 
-        return True
-
-    def draw_callback_px(self, context):
-        scene = context.scene
-        aspect_ratio = scene.render.resolution_x / scene.render.resolution_y
-
-        self._update_offscreen(context, self._offscreen)
-        self._opengl_draw(context, self._texture, aspect_ratio, 0.2)
-
-    def _update_offscreen(self, context, offscreen):
+    @staticmethod
+    def _update_offscreen(context, offscreen):
         scene = context.scene
         camera = scene.camera
 
@@ -93,12 +62,15 @@ class VIEW3D_OT_OffScreenDraw(bpy.types.Operator):
                 context.space_data,
                 context.region,
                 projection_matrix,
-                modelview_matrix)
+                modelview_matrix,
+                )
 
-    def _opengl_draw(self, context, texture, aspect_ratio, scale):
+    @staticmethod
+    def _opengl_draw(context, texture, aspect_ratio, scale):
         """
         OpenGL code to draw a rectangle in the viewport
         """
+
         glDisable(GL_DEPTH_TEST)
 
         # view setup
@@ -111,7 +83,7 @@ class VIEW3D_OT_OffScreenDraw(bpy.types.Operator):
         glLoadIdentity()
 
         glOrtho(-1, 1, -1, 1, -15, 15)
-        gluLookAt(0.0, 0.0, 1.0, 0.0,0.0,0.0, 0.0,1.0,0.0)
+        gluLookAt(0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
 
         act_tex = Buffer(GL_INT, 1)
         glGetIntegerv(GL_TEXTURE_2D, act_tex)
@@ -131,10 +103,10 @@ class VIEW3D_OT_OffScreenDraw(bpy.types.Operator):
 
         glBindTexture(GL_TEXTURE_2D, texture)
 
-        texco = [(1, 1), (0, 1), (0, 0), (1,0)]
+        texco = [(1, 1), (0, 1), (0, 0), (1, 0)]
         verco = [(1.0, 1.0), (-1.0, 1.0), (-1.0, -1.0), (1.0, -1.0)]
 
-        glPolygonMode(GL_FRONT_AND_BACK , GL_FILL)
+        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
 
         glColor4f(1.0, 1.0, 1.0, 1.0)
 
@@ -159,13 +131,54 @@ class VIEW3D_OT_OffScreenDraw(bpy.types.Operator):
         glViewport(viewport[0], viewport[1], viewport[2], viewport[3])
         glScissor(viewport[0], viewport[1], viewport[2], viewport[3])
 
+    # operator functions
+    @classmethod
+    def poll(cls, context):
+        return context.area.type == 'VIEW_3D'
+
+    def modal(self, context, event):
+        if context.area:
+            context.area.tag_redraw()
+
+        return {'PASS_THROUGH'}
+
+    def invoke(self, context, event):
+        if OffScreenDraw.is_enabled:
+            self.cancel(context)
+
+            return {'FINISHED'}
+
+        else:
+            self._offscreen = OffScreenDraw._setup_offscreen(context)
+            if self._offscreen:
+                self._texture = self._offscreen.color_texture
+            else:
+                self.report({'ERROR'}, "Error initializing offscreen buffer. More details in the console")
+                return {'CANCELLED'}
+
+            OffScreenDraw.handle_add(self, context)
+            OffScreenDraw.is_enabled = True
+
+            if context.area:
+                context.area.tag_redraw()
+
+            context.window_manager.modal_handler_add(self)
+            return {'RUNNING_MODAL'}
+
+    def cancel(self, context):
+        OffScreenDraw.handle_remove()
+        OffScreenDraw.is_enabled = False
+
+        if context.area:
+            context.area.tag_redraw()
+
 
 def register():
-    bpy.utils.register_class(VIEW3D_OT_OffScreenDraw)
+    bpy.utils.register_class(OffScreenDraw)
 
 
 def unregister():
-    bpy.utils.unregister_class(VIEW3D_OT_OffScreenDraw)
+    bpy.utils.unregister_class(OffScreenDraw)
 
 
 if __name__ == "__main__":




More information about the Bf-blender-cvs mailing list