OpenGL@Python in a OF created window

Hi
Is it possible to draw OpenGL models in a context initiated by Openframeworks window? I want part of the functionality of my app to be closed in an .exe and some parts possible to be modified by users able to write some python code.
I dodn’t have problems with embedding Python in C, i also found http://forum.openframeworks.cc/t/006-python-bindings-with-installer.-win32/3031/0 but i cant see any way of passing the window handle identifier from C to Python (if this is the correct way to do this). I wanted to get the window’s handle to Python which would later do window = glutSetWindow(HWND) and operate on the shared memory.
Right now i’m stuck with trying to pass the identifier around. I’d really be thankful for any hints.

The anwser to my question was not calingl ofRunApp() embed python script.
My modified version of an example from the example from http://code.google.com/p/openframeworks-…-loads/list looks like this

  
  
from openframeworks import *  
import math  
  
class testApp(ofBaseApp):  
    def setup(self):  
        self.pythonVar = 0;  
        self.cVar = 0;  
		  
    def update(self):  
        self.pythonVar += 0.1  
      
    def draw(self):  
        print 'draw:cVar=' + str(self.cVar) + ':pythonVar=' + str(self.pythonVar)  
        ofBackground(0,0,0)  
        ofSetColor(0xa0f0a0)  
        for i in range(0,30):  
            ofRect(i*20,300+10*math.sin(0.1*(self.cVar+i)),10,math.sin((i+self.pythonVar)/10)*30)  
  
    def updateCVar(self,newVal):  
        self.pythonVar = 0.1 * self.cVar  
        self.cVar = int(newVal);  
			  
app = testApp()	  
if __name__ == "__main__":     
    ofSetupOpenGL(800,700, OF_WINDOW)  
    ofRunApp(app)  
  

What is important is that the application is always created but it’s ofRunApp’ed only when running from console.
In this source there are two variables. One opdated by Python code and one updated by C code - pythonVar and cVar respectively.

That was animation.py which resides in bin directory of openframeworks binary code. The OF code which creates the window later used by python looks like this

  
  
void testApp::setup(){  
    const char *filename = "animation";  
    const char *objectName = "app";  
    const char *functionName = "setup";  
  
    PyObject *pName,*pModule,*pObject,*pFunc,*pArgs;  
  
    Py_Initialize();  
  
    pName = PyString_FromString(filename);  
    pModule = PyImport_Import(pName);  
    Py_DECREF(pName);  
  
    pObject = PyObject_GetAttrString(pModule,objectName);  
    Py_DECREF(pModule);  
  
    pFunc = PyObject_GetAttrString(pObject,functionName);  
    Py_DECREF(pObject);  
  
    pArgs = PyTuple_New(0);  
    PyObject_CallObject(pFunc,pArgs);  
  
    Py_DECREF(pFunc);  
  
    counter = 0;  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
    counter+=1;  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
    const char *filename = "animation";  
    const char *objectName = "app";  
    const char *functionNameDraw = "draw";  
    const char *functionNameUpdateCVar = "updateCVar";  
  
    PyObject *pName,*pModule,*pObject,*pFuncDraw,*pFuncSetVal,*pValue;  
    PyObject *pArgsUpdateCVar,*pArgsDraw;  
  
    pName = PyString_FromString(filename);  
    pModule = PyImport_Import(pName);  
    Py_DECREF(pName);  
  
    pObject = PyObject_GetAttrString(pModule,objectName);  
    Py_DECREF(pModule);  
  
    pFuncSetVal = PyObject_GetAttrString(pObject,functionNameUpdateCVar);  
    pFuncDraw = PyObject_GetAttrString(pObject,functionNameDraw);  
  
    Py_DECREF(pObject);  
  
    pArgsUpdateCVar = PyTuple_New(1);  
    PyTuple_SetItem(pArgsUpdateCVar,0,PyInt_FromLong(counter));  
    PyObject_CallObject(pFuncSetVal,pArgsUpdateCVar);  
    Py_DECREF(pArgsUpdateCVar);  
    Py_DECREF(pFuncSetVal);  
  
    pArgsDraw = PyTuple_New(0);  
    PyObject_CallObject(pFuncDraw,pArgsDraw);  
  
    Py_DECREF(pArgsDraw);  
    Py_DECREF(pFuncDraw);  
  
  
    ofSetColor(255,255,0);  
    ofRect(0,0, 100,100);  
  
}  
  
void testApp::exit(){  
    Py_Finalize();  
}  
  
//....  
  

When the python app is executed the rectangles just change their height. When ran from OF application they’re also waving.

It’s just a proof-of-concept. I hope somebody will find it useful someday :slight_smile:

If you think something could be done better I’d really appreciate any kind of suggestions since i’m yet far from being an expert in both Python and C.