[vtkusers] Problems with classes in python (maybe a programming mistake)

David Gobbi david.gobbi at gmail.com
Sat Jul 3 08:27:31 EDT 2010


Hi Luca,

Your code is good, the problem you are seeing is just due to the way
that the python wrappers do garbage collection.

In vtk/python, every vtk/python object has two parts: a VTK object,
and a python wrapper around that object.  Each part has its own
reference count for garbage collection.  So, if you do this:

def func():
  c = vtk.vtkCollection()
  b = vtk.vtkActor()
  b.myattr = "hello"
  c.AddItem(b)
  return c

print c.GetItemAsObject(0).myattr
... attribute error!!

The variable "b" goes out of scope at the end of the function, and the
python part of the actor object is garbage collected.  The C++ part of
the actor is still alive, because it is stored in a VTK data structure
(the vtkCollection).

If you want the attributes to stay around, then the python part of the
object must stay alive.  To do this, you must store the objects in a
python data structure like a tuple, list, etcetera.  If the objects
are stored in a VTK object, then only the C++ part of the object will
stay alive.

So if you store your objects in a python list, instead of in a
vtkCollection, everything will work.  Or, you can do a hybrid:

class RegionsContainer(vtk.vtkCollection):
  def __init__(self):
    self.pythonlist = []

  def CreateNewRegion(self):
    ...
    self.AddItem(new_region)
    self.pythonlist.append(new_region)

You can see how the hybrid approach is kind of ugly, though.

   David


On Sat, Jul 3, 2010 at 5:00 AM, Luca Penasa <luca.penasa at email.it> wrote:
> Hi everybody,
> I have some problems writing in python using vtk. Maybe is only a programmin
> issue.
>
> I have implemented an extended version of vtkIdList for doing some tasks i
> need (i am writing a segmentation algorithm for point clouds).
> when i call the Region class from ipyhton everithing works as expected.
> BUT when i create a new istance of the Region class from inside a function
> it only work when the function is running. If the function return multiple
> istances of the Region class what i obtain are istances without attributes
> and functions I added. I really dont understand why!
> I think is a simple python issue (i am not a good programmer).
>
>
> this is my classes:
>
> class Region(vtk.vtkIdList):
> def __init__(self):
> self.mean_value = 0
>
> def GetLastId(self):
> return self.GetId(self.GetNumberOfIds()-1)
>
> def GetFirstId(self):
> return self.GetId(0)
>
> def DeleteLastId(self):
> self.DeleteId(self.GetId(self.GetNumberOfIds()-1))
>
> def IsEmpty(self):
> '''
> return 0 if not empty, 1 if there are ids in list
> '''
> if self.GetNumberOfIds() == 0:
> return 1
> else:
> return 0
>
> def ExistId(self, id):
> '''
> return 0 if that id is not in list, 1 if exist
> '''
> #check if the list is empty, if so the specified id is obviuosly not in list
> if self.GetNumberOfIds() == 0:
> return 0
> else:
> if self.IsId(id) == id:
> return 1
> else:
> return 0
>
> def SetMean(self, value):
> self.mean_value = value
>
> def GetMean(self):
> return self.mean_value
>
>
> class RegionsContainer(vtk.vtkCollection):
> def CreateNewRegion(self):
> number_of_items = self.GetNumberOfItems()
> new_region = Region()
> self.AddItem(new_region)
> return self.GetItemAsObject(number_of_items)
>
> def SetAssociatedGrid(self, grid):
> self.AssociatedGrid = grid
>
> def GetAssociatedGrid(self):
> return self.AssociatedGrid
>
> def SetAssociatedArrayOfGrid(self, arrayname):
> self.AssociatedArrayOfGrid =
> self.GetAssociatedGrid().GetPointData().GetArray(arrayname)
>
> def GetAssociatedArrayOfGrid(self):
> return self.AssociatedArrayOfGrid
>
>
> in a function i called DoSegmentation i create a RegionContainer:
>
>     regions = RegionsContainer()
>
> then, in a while-loop i create multiple istances of my Region class:
>
>     current_region = regions.CreateNewRegion()
>
> then the code call some functions on this Region, like this:
>
>     current_region.SetMean(current_mean)
>
> at the end my function returns the variable 'regions' that contain multiple
> istances of the class Region()
> BUT, when i try to have acces to the items contained, with;
>
>     regions.GetItemAsObject(n)
>
> i have only a vtkIdList as return, and not my Region() class
>
>     if i try regions.GetItemAsObject(n).GetMean()
>
> it raise an attribute error!!
>
>
> anybody could spent some minutes and have a look at this code??
> I think I made a mistake, but i dont know where...
> Thank you!!
>
>
> ----
> Caselle da 1GB, trasmetti allegati fino a 3GB e in piu' IMAP, POP3 e SMTP
> autenticato? GRATIS solo con Email.it
>
> Sponsor:
> Ultime 2 stanze dal 18 al 25 luglio con offerta speciale aquafan e oltremare
> per 4 giorni all'hotel Poker, 3 stelle, di Riccione
> Clicca qui
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
>



More information about the vtkusers mailing list