Navigation

importbpyclassCustomRenderEngine(bpy.types.RenderEngine):# These three members are used by blender to set up the# RenderEngine; define its internal name, visible name and capabilities.bl_idname='custom_renderer'bl_label='Flat Color Renderer'bl_use_preview=True# This is the only method called by blender, in this example# we use it to detect preview rendering and call the implementation# in another method.defrender(self,scene):scale=scene.render.resolution_percentage/100.0self.size_x=int(scene.render.resolution_x*scale)self.size_y=int(scene.render.resolution_y*scale)ifscene.name=='preview':self.render_preview(scene)else:self.render_scene(scene)# In this example, we fill the preview renders with a flat green color.defrender_preview(self,scene):pixel_count=self.size_x*self.size_y# The framebuffer is defined as a list of pixels, each pixel# itself being a list of R,G,B,A valuesgreen_rect=[[0.0,1.0,0.0,1.0]]*pixel_count# Here we write the pixel values to the RenderResultresult=self.begin_result(0,0,self.size_x,self.size_y)layer=result.layers[0]layer.rect=green_rectself.end_result(result)# In this example, we fill the full renders with a flat blue color.defrender_scene(self,scene):pixel_count=self.size_x*self.size_y# The framebuffer is defined as a list of pixels, each pixel# itself being a list of R,G,B,A valuesblue_rect=[[0.0,0.0,1.0,1.0]]*pixel_count# Here we write the pixel values to the RenderResultresult=self.begin_result(0,0,self.size_x,self.size_y)layer=result.layers[0]layer.rect=blue_rectself.end_result(result)# Register the RenderEnginebpy.utils.register_class(CustomRenderEngine)# RenderEngines also need to tell UI Panels that they are compatible# Otherwise most of the UI will be empty when the engine is selected.# In this example, we need to see the main render image button and# the material preview panel.frombl_uiimportproperties_renderproperties_render.RENDER_PT_render.COMPAT_ENGINES.add('custom_renderer')delproperties_renderfrombl_uiimportproperties_materialproperties_material.MATERIAL_PT_preview.COMPAT_ENGINES.add('custom_renderer')delproperties_material