Changeset View
Changeset View
Standalone View
Standalone View
doc/python_api/examples/bpy.types.RenderEngine.py
| Show First 20 Lines • Show All 95 Lines • ▼ Show 20 Lines | def view_draw(self, context, depsgraph): | ||||
| region = context.region | region = context.region | ||||
| scene = depsgraph.scene | scene = depsgraph.scene | ||||
| # Get viewport dimensions | # Get viewport dimensions | ||||
| dimensions = region.width, region.height | dimensions = region.width, region.height | ||||
| # Bind shader that converts from scene linear to display space, | # Bind shader that converts from scene linear to display space, | ||||
| bgl.glEnable(bgl.GL_BLEND) | bgl.glEnable(bgl.GL_BLEND) | ||||
| bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ONE_MINUS_SRC_ALPHA); | bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ONE_MINUS_SRC_ALPHA) | ||||
| self.bind_display_space_shader(scene) | self.bind_display_space_shader(scene) | ||||
| if not self.draw_data or self.draw_data.dimensions != dimensions: | if not self.draw_data or self.draw_data.dimensions != dimensions: | ||||
| self.draw_data = CustomDrawData(dimensions) | self.draw_data = CustomDrawData(dimensions) | ||||
| self.draw_data.draw() | self.draw_data.draw() | ||||
| self.unbind_display_space_shader() | self.unbind_display_space_shader() | ||||
| Show All 17 Lines | def __init__(self, dimensions): | ||||
| bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA16F, width, height, 0, bgl.GL_RGBA, bgl.GL_FLOAT, pixels) | bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA16F, width, height, 0, bgl.GL_RGBA, bgl.GL_FLOAT, pixels) | ||||
| bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) | bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) | ||||
| bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) | bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) | ||||
| bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) | bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) | ||||
| # Bind shader that converts from scene linear to display space, | # Bind shader that converts from scene linear to display space, | ||||
| # use the scene's color management settings. | # use the scene's color management settings. | ||||
| shader_program = bgl.Buffer(bgl.GL_INT, 1) | shader_program = bgl.Buffer(bgl.GL_INT, 1) | ||||
| bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program); | bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program) | ||||
| # Generate vertex array | # Generate vertex array | ||||
| self.vertex_array = bgl.Buffer(bgl.GL_INT, 1) | self.vertex_array = bgl.Buffer(bgl.GL_INT, 1) | ||||
| bgl.glGenVertexArrays(1, self.vertex_array) | bgl.glGenVertexArrays(1, self.vertex_array) | ||||
| bgl.glBindVertexArray(self.vertex_array[0]) | bgl.glBindVertexArray(self.vertex_array[0]) | ||||
| texturecoord_location = bgl.glGetAttribLocation(shader_program[0], "texCoord"); | texturecoord_location = bgl.glGetAttribLocation(shader_program[0], "texCoord") | ||||
| position_location = bgl.glGetAttribLocation(shader_program[0], "pos"); | position_location = bgl.glGetAttribLocation(shader_program[0], "pos") | ||||
| bgl.glEnableVertexAttribArray(texturecoord_location); | bgl.glEnableVertexAttribArray(texturecoord_location) | ||||
| bgl.glEnableVertexAttribArray(position_location); | bgl.glEnableVertexAttribArray(position_location) | ||||
| # Generate geometry buffers for drawing textured quad | # Generate geometry buffers for drawing textured quad | ||||
| position = [0.0, 0.0, width, 0.0, width, height, 0.0, height] | position = [0.0, 0.0, width, 0.0, width, height, 0.0, height] | ||||
| position = bgl.Buffer(bgl.GL_FLOAT, len(position), position) | position = bgl.Buffer(bgl.GL_FLOAT, len(position), position) | ||||
| texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0] | texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0] | ||||
| texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord) | texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord) | ||||
| self.vertex_buffer = bgl.Buffer(bgl.GL_INT, 2) | self.vertex_buffer = bgl.Buffer(bgl.GL_INT, 2) | ||||
| Show All 15 Lines | def __del__(self): | ||||
| bgl.glDeleteVertexArrays(1, self.vertex_array) | bgl.glDeleteVertexArrays(1, self.vertex_array) | ||||
| bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) | bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) | ||||
| bgl.glDeleteTextures(1, self.texture) | bgl.glDeleteTextures(1, self.texture) | ||||
| def draw(self): | def draw(self): | ||||
| bgl.glActiveTexture(bgl.GL_TEXTURE0) | bgl.glActiveTexture(bgl.GL_TEXTURE0) | ||||
| bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0]) | bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0]) | ||||
| bgl.glBindVertexArray(self.vertex_array[0]) | bgl.glBindVertexArray(self.vertex_array[0]) | ||||
| bgl.glDrawArrays(bgl.GL_TRIANGLE_FAN, 0, 4); | bgl.glDrawArrays(bgl.GL_TRIANGLE_FAN, 0, 4) | ||||
| bgl.glBindVertexArray(0) | bgl.glBindVertexArray(0) | ||||
| bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) | bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) | ||||
| # RenderEngines also need to tell UI Panels that they are compatible with. | # RenderEngines also need to tell UI Panels that they are compatible with. | ||||
| # We recommend to enable all panels marked as BLENDER_RENDER, and then | # We recommend to enable all panels marked as BLENDER_RENDER, and then | ||||
| # exclude any panels that are replaced by custom panels registered by the | # exclude any panels that are replaced by custom panels registered by the | ||||
| # render engine, or that are not supported. | # render engine, or that are not supported. | ||||
| def get_panels(): | def get_panels(): | ||||
| exclude_panels = { | exclude_panels = { | ||||
| 'VIEWLAYER_PT_filter', | 'VIEWLAYER_PT_filter', | ||||
| 'VIEWLAYER_PT_layer_passes', | 'VIEWLAYER_PT_layer_passes', | ||||
| } | } | ||||
| panels = [] | panels = [] | ||||
| for panel in bpy.types.Panel.__subclasses__(): | for panel in bpy.types.Panel.__subclasses__(): | ||||
| if hasattr(panel, 'COMPAT_ENGINES') and 'BLENDER_RENDER' in panel.COMPAT_ENGINES: | if hasattr(panel, 'COMPAT_ENGINES') and 'BLENDER_RENDER' in panel.COMPAT_ENGINES: | ||||
| if panel.__name__ not in exclude_panels: | if panel.__name__ not in exclude_panels: | ||||
| panels.append(panel) | panels.append(panel) | ||||
| return panels | return panels | ||||
| def register(): | def register(): | ||||
| # Register the RenderEngine | # Register the RenderEngine | ||||
| bpy.utils.register_class(CustomRenderEngine) | bpy.utils.register_class(CustomRenderEngine) | ||||
| for panel in get_panels(): | for panel in get_panels(): | ||||
| panel.COMPAT_ENGINES.add('CUSTOM') | panel.COMPAT_ENGINES.add('CUSTOM') | ||||
| def unregister(): | def unregister(): | ||||
| bpy.utils.unregister_class(CustomRenderEngine) | bpy.utils.unregister_class(CustomRenderEngine) | ||||
| for panel in get_panels(): | for panel in get_panels(): | ||||
| if 'CUSTOM' in panel.COMPAT_ENGINES: | if 'CUSTOM' in panel.COMPAT_ENGINES: | ||||
| panel.COMPAT_ENGINES.remove('CUSTOM') | panel.COMPAT_ENGINES.remove('CUSTOM') | ||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||
| register() | register() | ||||