[vtkusers] urgent

Prabhu Ramachandran prabhu at aero.iitm.ernet.in
Wed Oct 16 05:39:02 EDT 2002


>>>>> "D" == dharmendra  <dharmendra at strandgenomics.com> writes:

    D> Hello, Let me make myself clear.  I have a renderwindow in
    D> python/Tkinter( ren_widget = vtkRenderWidget.vtkTkRenderWidget(
    D> frame ) ren_win = ren_widget.GetRenderWindow()

    D> Now i want to pass this ren_win to a c++ class which adds
    D> renderers to it and returns ren_win.  In my python code i will
    D> say ren_win.render().

BTW, why do you want to return ren_win from the c++ code?

    D> how to acheive this.  with some code example.

Here is what you need to do.

 1. You need one entry point to your C++ code from Python.  i.e. you
 need to hand off your ren_win to a C++ function in your C++ code
 where you are adding renderers etc.  To do this you need to wrap
 atleast the one function that will be given a ren_win object.  I'd
 suggest that you read up the documentation (Extending and Embedding
 the Python Interpreter) and also the Python C/API and wrap one single
 function that hooks into your C++ code.  Expose this function to
 Python as a module.  Lets call the file simple.c and the resulting
 python module simple with the single function

   static PyObject* simple_vtk_function(PyObject *obj, PyObject *args);

 or whatever.  This function would in turn use the function I
 mentioned earlier to get the vtkRenderWindow from the argument passed
 to it.  How to write the complete function is beyond the scope of
 this email.  Its not that hard.  Alternatively use SWIG
 (http://www.swig.org) or boost.python (http://www.boost.org)to do the
 job.  Either way you'd have to pick something and figure out how to
 use it.  I've attached an extremely simple example below of a simple
 function call in C that is called from Python.  More on that follows.

 2. Now from Python, import this module (simple) and then call your
 function with the ren_win.  You should be all set.

Anyway here is some actual working code that I wrote just for the
purpose of illustration.  This question has come up quite a few times.
I thought I might as well write a small demo.  It took less than half
an hour.

// --------------- cut here ----------
// simple.c
#include "Python.h"
#include "vtkPythonUtil.h"
#include "vtkObject.h"
#include "vtkObjectBase.h"
#include <stdio.h>

extern "C" 
{
   PyObject* simple_vtk_function(PyObject *self, PyObject *args);
}

static PyObject* simple_vtk_function(PyObject *self, PyObject *args)
{
   PyObject *vtkpy;
   if (!PyArg_ParseTuple(args, "O", &vtkpy))
     return NULL;
   
   vtkObject *obj = (vtkObject*)vtkPythonGetPointerFromObject(vtkpy, 
                                                              "vtkObject");
   /* Do what you want to do here. */
   printf("%s\n", obj->GetClassName());
   
   Py_INCREF(Py_None);
   return Py_None;
}

static PyMethodDef simple_methods[] = 
{
     {"vtk_function", simple_vtk_function, METH_VARARGS, 
          "Simple VTK function"},
   {NULL, NULL, 0, NULL} /* sentinel */
};

extern "C" 
{
   void initsimple();
}

DL_EXPORT(void) initsimple()
{
   PyObject *m, *d;
   m = Py_InitModule("simple", simple_methods);
   d = PyModule_GetDict(m);
}
// --------------- cut here ----------


Lets call this simple.c.  Now compile it like so:

$ g++ simple.c -shared -fPIC -I/usr/include/python2.1 \
-Ipath/to/your/vtk/headers -Lpath/to/your/vtk/libs/ \
-lvtkCommon -lvtkCommonPython -o simple.so

Now run this from python:
>>> import vtk
>>> import simple
>>> o = vtk.vtkRenderWindow()
>>> simple.vtk_function (o)
vtkXOpenGLRenderWindow

It works!


Anyway, I wonder why you are not using Python to do all the VTK
related stuff and want to hand off everything to C++?  Whats the
point?  VTK-Python is only a thin wrapper so should work ok.

cheers,
prabhu



More information about the vtkusers mailing list