[vtkusers] Running python VTK script repeatedly in the same python session

Aleksey Naumov naumov at acsu.buffalo.edu
Sat Aug 12 20:09:14 EDT 2000


Dear VTK pythoneers,

I would like to be able to run the same python VTK script repeatedly
within the same
python session. This would come really handy for rapid development of a
small
GUI interface I am creating for my vis. problem, because time consuming
steps such
as loading of VTK and PyQt libs are avoided.

In other words, I would like to create my pipeline and GUI interface,
play with it,
exit from my script, and recreate them again without having to quit
python shell.

Sounds simple, but I can't do that. Below is a simple example based on
Cone.py:
I just wrapped the pipeline in a class (I need that for binding to the
GUI) and
commented out the vtkRenderWindowInteractor (because upon quitting from
it,
it takes me out of python as well). There's no interface in this simple
test.

So I run it from inside python with
    execfile("Cone.py")

then I delete the whole class with
    del c

and try to rerun the script with execfile("Cone.py") again. And get a
segmentation fault.

My guess is that 'del c' does not delete the VTK objects properly, but
how to do that?
I tried explicitly deleting every attribute in 'dir(c)' or setting
ReferenceCount of
pipeline objects to 0 or 1, but with no success.

What do I need to do to delete and recreate this pipeline without
quitting python? Would
appreaciate any comments!

Thanks
Aleksey

----------- "Cone.py" ------------
from libVTKCommonPython import *
from libVTKGraphicsPython import *

class Cone:
 def __init__(self):

  # create a rendering window and renderer
  self.ren = vtkRenderer()
  self.renWin = vtkRenderWindow()
  self.renWin.DebugOn()
  self.renWin.AddRenderer(self.ren)
  self.renWin.SetSize(300,300)
  #renWin.StereoCapableWindowOn()

  #iren = vtkRenderWindowInteractor()
  #iren.SetRenderWindow(renWin)

  # create an actor and give it cone geometry
  self.cone = vtkConeSource()
  self.cone.DebugOn()
  self.cone.SetResolution(8)
  self.coneMapper = vtkPolyDataMapper()
  self.coneMapper.SetInput(self.cone.GetOutput())
  self.coneActor = vtkActor()
  self.coneActor.SetMapper(self.coneMapper)

  # assign our actor to the renderer
  self.ren.AddActor(self.coneActor)
  self.renWin.Render()
  # enable user interface interactor
  #iren.Initialize()
  #iren.Start()

 def ref(self):
  for key in self.__dict__.keys():
   print key + ":", self.__dict__[key].GetReferenceCount()

 def setref(self, count):
  for key in self.__dict__.keys():
   self.__dict__[key].SetReferenceCount(count)


c = Cone()
c.ref()





More information about the vtkusers mailing list