Panda in PyQt

Current state:

Drawing into PyQt windows is faster now without image conversion getting in the way. If you wished to make a PyQt app with a native panda component (for GUI’s maybe?) this is a pretty good way to do it. Still not satisfied though as the alpha masking for the window still has to go through conversion and is therefore slow.

Still not satisfied though

from pandac.PandaModules import loadPrcFileData
#loadPrcFileData("", "window-type offscreen") # Set Panda to draw its main window in an offscreen buffer
# PyQt imports
from PyQt4 import QtGui,  QtCore
import sys
# Panda imports
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import GraphicsOutput,  Texture,  StringStream,  PNMImage
from direct.interval.LerpInterval import LerpHprInterval
from pandac.PandaModules import Point3
# Set up Panda environment
import direct.directbase.DirectStart
from struct import *

class QtPandaWidget(QtGui.QWidget):
    """
    This takes a texture from Panda and draws it as a QWidget
    """
    
    def __init__(self, texture,  parent=None):
        QtGui.QWidget.__init__(self,  parent)
        
        self.setGeometry(50, 50, 800, 600)
        self.setWindowTitle("PandaQt")
        self.setWindowFlags(self.windowFlags() | QtCore.Qt.FramelessWindowHint) # Setup the window so its frameless
        self.pandaTexture = texture
        
        # Setup a timer in Qt that runs taskMgr.step() to simulate Panda's own main loop
        pandaTimer = QtCore.QTimer(self)
        self.connect(pandaTimer,  QtCore.SIGNAL("timeout()"), taskMgr.step)
        pandaTimer.start(0)
        
        # Setup another timer that redraws this widget in a specific FPS
        redrawTimer = QtCore.QTimer(self)
        self.connect(redrawTimer,  QtCore.SIGNAL("timeout()"), self, QtCore.SLOT("update()"))
        redrawTimer.start(1000/60)
        
        self.paintSurface = QtGui.QPainter()
        self.rotate = QtGui.QTransform()
        self.rotate.rotate(180)
        
        self.out_image = QtGui.QImage()
    
    def showEvent(self, event):
        self.desktopBg = QtGui.QPixmap.grabWindow(QtGui.QApplication.desktop ().winId(), \
                self.geometry().x(),self.geometry().y(), self.rect().width(), self.rect().height())
    
    # Use the paint event to pull the contents of the panda texture to the widget
    def paintEvent(self,  event):
        if self.pandaTexture.mightHaveRamImage():
            self.pandaTexture.setFormat(Texture.FRgba32)
            #print "Should draw yes?"
            data = self.pandaTexture.getRamImage().getData()
            img = QtGui.QImage(data, self.pandaTexture.getXSize(), self.pandaTexture.getYSize(), QtGui.QImage.Format_ARGB32).mirrored()
            self.paintSurface.begin(self)
            self.paintSurface.drawPixmap(0, 0, self.desktopBg)
            self.paintSurface.drawImage(0, 0, img)
            self.paintSurface.end()
            pixmap = QtGui.QPixmap.fromImage(img)
            self.setMask(pixmap.mask())

class PandaHandler(DirectObject):
    
    def __init__(self):
        base.disableMouse()
        base.cam.setPos(0, -28, 6)
        self.testModel = loader.loadModel('panda')
        self.testModel.reparentTo(render)
        self.rotateInterval = LerpHprInterval(self.testModel, 3, Point3(360, 0, 0))
        self.rotateInterval.loop()
        
        self.screenTexture = Texture()
        self.screenTexture.setMinfilter(Texture.FTLinear)
        self.screenTexture.setFormat(Texture.FRgba32)
        print "Format is", self.screenTexture.getFormat()
        base.win.addRenderTexture(self.screenTexture, GraphicsOutput.RTMCopyRam)

if __name__ == "__main__":    
    #lol teh hobo
    panHandler = PandaHandler() 
    
    app = QtGui.QApplication(sys.argv)
    pandaWidget = QtPandaWidget(panHandler.screenTexture)
    pandaWidget.show()
    
    sys.exit(app.exec_())