enthought / mayavi

3D visualization of scientific data in Python
http://docs.enthought.com/mayavi/mayavi/
Other
1.3k stars 285 forks source link

Deriving a tvtk wrapped vtk class #846

Open AntoineD opened 4 years ago

AntoineD commented 4 years ago

Hello,

I would like to derive a tvtk class wrapped from vtk (say tvtk.Filter) in order to override a method, as I would do for a pure python class but this is not possible here. It seems this would require using the tvtk.VTKPythonAlgorithmBase class in collaboration with tvtk.Filter, but how to do it in such a way that the derived class has the same API as tvtk.Filter? I tried multiple inheritance but it failed.

Antoine.

AntoineD commented 4 years ago

It looks like the wrapped VTKPythonAlgorithmBase filter cannot work at all, I think because TVTKBase creates an instance from it instead of an instance from its derived class as it is intended to be used.

Has anybody successfully used tvtk.VTKPythonAlgorithmBase to create python filters?

prabhuramachandran commented 4 years ago

Yes, it doesn't make much sense to subclass the tvtk.VTKPythonAlgorithmBase to create a filter simply because of the inheritance hierarchy being entirely different. Instead you can subclass the original VTK-Python class and then wrap that as a TVTK object as done here:

https://github.com/enthought/mayavi/pull/695/files#diff-1ed25a3c0e324848467a18d5c090b267R871

AntoineD commented 4 years ago

In the meantime I did pretty much what you suggested, actually with reimplementing the original vtk.VTKPythonAlgorithmBase around tvtk.PythonAlgorithm.

Perhaps vtk.VTKPythonAlgorithmBase shall not be wrapped in tvtk? Would you be interested in a PR with the "hand made" tvtk.VTKPythonAlgorithmBase?

prabhuramachandran commented 4 years ago

Well, the reason we need it wrapped at all is so that we can convert these objects to TVTK objects for a variety of reasons including the automatic UI, display on the tree etc. For example you can easily add an algorithm to the Mayavi pipeline.

The tvtk.PythonAlgorithm is not quite the same thing as it is fairly limited in comparison with the VTKPythonAlgorithmBase, IIRC. If you think the hand wrapped one is useful, and would be useful to others, then sure please feel free to send a PR. Thanks!

AntoineD commented 4 years ago

It could be useful for people who want to use tvtk.VTKPythonAlgorithmBase with a pipeline defined the VTK way instead of the mayavi way.

I don't know where to put this class inside tvtk, so I will just put the code somewhere on my github account with a basic example.