Share

Using Python to Implement a Custom Renderer

The following Python script demonstrates how you can use Python to implement a custom renderer:

from pyfbsdk import *

class MyRenderCallback (FBRendererCallback):   
    def __init__(self, name):
        super(MyRenderCallback, self).__init__(name)

    def GetCallbackName(self):
        return "My Python Renderer"

    def GetCallbackDesc(self):
        return "This is a python render"

    def GetCallbackPrefCount(self):
        return 2

    def GetCallbackPrefName(self, pIndex):
        if pIndex == 0:
            return "Setting One"
        else: 
            return "Setting Two"

    def Attach(self):
        pass

    def Detach(self):
        pass        

    def DetachDisplayContext(self, options):
        pass

    def Render(self, options):
        lRender = FBSystem().Renderer
        lCount = lRender.DisplayableGeometryCount - 1
        while(lCount >= 0) :                
            lModel = lRender.GetDisplayableGeometry(lCount)
            lRender.OGLModelDisplay(options, lModel)
            lCount = lCount - 1

# Create python renderer
lMyRender = MyRenderCallback("My Python Renderer")
# Register this custom render to system
FBSystem().Renderer.RendererCallbacks.append(lMyRender)
# Turn it on
FBSystem().Renderer.CurrentPaneCallbackIndex = len (FBSystem().Renderer.RendererCallbacks) - 1

# Unregister and delete the python renderer
'''
if(len(FBSystem().Renderer.RendererCallbacks)):
    lMyRender = FBSystem().Renderer.RendererCallbacks.pop()           
    lMyRender.FBDelete()
'''

Although, this script looks trivial from a production perspective at first, but it actually indicates how to achieve greater flexibility. You can choose to use the PyOpenGL Python package to implement a more useful rendering. You can also implement the performance demanding routines in C++ first and then expose them in Python. You can eventually use them inside this Python based custom renderer. In this way, the flexibility of Python and efficiency of C++ can be utilized. The following figure shows the Python implementation of the custom renderer.

Was this information helpful?