[PYTHON MATRIX-SIG] FW: My Python/Vtk Method

Zachary_Roadhouse@brown.edu Zachary_Roadhouse@brown.edu
Tue, 28 Jan 1997 09:57:41 -0500 (EST)


Here is something I did a while ago on Python/Vtk integration.  Hopefully
this will help.

-----FW: My Python/Vtk Method-----

Date: Wed, 11 Dec 1996 19:55:32 -0500 (EST)
From: Zachary_Roadhouse@brown.edu
To: vtkusers@mr.med.ge.com
Subject: My Python/Vtk Method


Oh well, I need a study break.  Here is a synopsis of how to use Vtk
with Python.  This has only been attempted on Solaris 2.5.1 using Vtk
1.2 and Mesa 2.0 (I found that it was faster than GLX on our machines)

1. Build Vtk with shared libraries.  This will produce a libvtk.so.

2. Build a shared library from the tclsrc code.  I added this to my
Makefile in the tclsrc directory.

libvtkTcl.so:
${TCL_SRC_OBJ} ${TCL_SBR_OBJ} ${TCL_XGLR_OBJ} ${TCL_OGLR_OBJ} \
        ${TCL_GLR_OBJ} ${TCL_NEWS}
        ld -G -o libvtkTcl.so ${TCL_SRC_OBJ} ${TCL_SBR_OBJ} \
                ${TCL_XGLR_OBJ} ${TCL_OGLR_OBJ} ${TCL_GLR_OBJ}
${TCL_NEWS}

Do a make and then a make libvtkTcl.so.  This should make the vtk
binary as well and the shared library.

3. Install these two libraries somewhere along your LD_LIBRARY_PATH

4. Create a new directory in vtk distribution called Python.  Copy
the following files there:

vtkAutoInit.o
vtkTclUtil.o
vtkXRenderWindowInteractor.o

Create a shared library from these three files:

ld -G -o libvtkTkinter.so vtkAutoInit.o vtkTclUtil.o \
        vtkXRenderWindowInteractor.o -lvtkTcl -lvtk -lC

You now have a library with all of Vtk's Tcl interface included. Put
this library somewhere in your LD_LIBRARY_PATH.

5. Now for the python side.  In tkappinit.c in Python(version)/Modules
add the following lines:

(at the top)

extern int VTK_Init(Tcl_Interp*);

(Inside Tcl_AppInit after Tcl_Init)

  if (VTK_Init(interp) == TCL_ERROR) 
    return TCL_ERROR;

6. Modify the tkinter entry in Python(version)/Modules/Setup.  Mine
looks like this (using OpenGL, Python Imaging Library, and now Vtk)

_tkinter _tkinter.c tkappinit.c togl.c tkImaging.c -DSOLARIS_BUG
-DWITH_APPINIT 
-I/usr/openwin/include -I$(EN3_PACKAGES)/include
-I$(PYEXTENSIONS)/Imaging/libIm
aging -L/usr/openwin/lib -L$(EN3_PACKAGES)/lib
-L$(PYEXTENSIONS)/Imaging/libImag
ing -lvtkTkinter -lMesaGL -lImaging -ltk4.2 -ltcl7.6 -lXext -lXmu
-lX11

The only extra libraries for vtk are vtkTkinter and MesaGL.

7. You can either install _tkinter.so in the standard Python place or
copy it to the directory in which you are working with vtk so that it
doesn't hog extra memory if not needed.

Here is an example program for the very low level interface to tcl
that this provides.  Some students here are working on wrapping this
interface in Python objects.

None of this would be neccessary we could generate the Python
interface and code automatically.  I'm not versed enough in yacc/etc.
to do this.  Good luck -- you may have to fiddle to get it to work.

import _tkinter
 
 
vtk = _tkinter.create() ## create a new _tkinter widget with VTK
 
## create a rendering window and a renderer
## first, create the RenderMaster
vtk.call('vtkRenderMaster', 'rm')
## then create a RenderWindow in which to render the object
renWin = vtk.call('rm', 'MakeRenderWindow')
## now create the actual thing that will do the rendering
ren = vtk.call(renWin, 'MakeRenderer')
## and now create the mouse/keyboard interactor for the RenderWindow
iren = vtk.call(renWin, 'MakeRenderWindowInteractor')
 
## now that we have everything we need to do the rendering, we
## have to create the actual object which we want to render: a cone
 
## this will create a cone actor and give it geometry for rendering
## i chose vtkConeSource, but change to vtkSphereSource works, too
vtk.call('vtkEarthSource', 'cone' ) ## an actor called cone
## now we decide on the resolution in which to display our actor
## not all vtk____Sources support this call, but Cone does
vtk.call('cone','OutlineOff')
vtk.call('vtkPolyMapper','coneMapper') ## creates a PolygonMapper
## now we need to get the information to give to the PolyMapper
Info = vtk.call('cone', 'GetOutput') ## creates output from the cone
vtk.call('coneMapper','SetInput', Info)
## here is the actual formal Actor
vtk.call('vtkActor', 'coneActor')
## the actor needs a mapper to render it, so we give it ConeMapper
vtk.call('coneActor', 'SetMapper', 'coneMapper')
 
## now we tell the renderer which actors to render
vtk.call(ren, 'AddActors', 'coneActor')
 
## now we initialize the mouse interactor and keyinteractors
vtk.call(iren, 'SetUserMethod', '{wm deiconify .vtkInteract}')
vtk.call(iren, 'Initialize')
 
## we don't want a tk window to show up, so we remove it
vtk.call('wm', 'withdraw', '.')
 
## now we make the program execute...
_tkinter.mainloop()



     - Zack

E-MAIL: Zachary_Roadhouse@brown.edu  WEB: http://althor.netspace.org/~zack/
Brown University, Box 220, Providence, RI 02912
Phone: (401) 863 - 5435

-------------End of forwarding message-------------------------

     - Zack

E-MAIL: Zachary_Roadhouse@brown.edu  WEB: http://althor.netspace.org/~zack/
Brown University, Box 220, Providence, RI 02912
Phone: (401) 863 - 5435

_______________
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________