[vtkusers] Python wrapping a custom class

David Gobbi david.gobbi at gmail.com
Fri Jul 29 12:07:18 EDT 2016


Hi Kit,

It's most likely because the class is missing vtkTypeMacro:

vtkTypeMacro(vtkNewData, vtkImageData);

 - David


On Fri, Jul 29, 2016 at 9:02 AM, Kit Chambers <kit.chambers.kc at gmail.com>
wrote:

> Hi,
>
> I am trying to wrap a custom class derived from vtkImageData in python.
> And it works except for when i try to access my custom methods. For example:
>
> import vtkNewDataPython as idp
>
> mydata = idp.vtkNewData()
>
> print "\nvtkImageData functionality"
> mydata.SetDimensions(2,3,1)
> dims = mydata.GetDimensions()
> print "image dimensions =",dims
> print "Number of points: ",mydata.GetNumberOfPoints()
> print "Number of cells: ",mydata.GetNumberOfCells()
>
> print "\ncustom functionality"
> mydata.set_myvalue()
> print "Myvalue = ", mydata.get_myvalue()
>
>
> Produces:
> vtkImageData functionality
> image dimensions = (2, 3, 1)
> Number of points:  6
> Number of cells:  2
>
> custom functionality
> Traceback (most recent call last):
>   File "./pytest.py", line 14, in <module>
>     mydata.set_myvalue()
> AttributeError: set_myvalue
>
>
> Basically it is doing just fine up until I try to call a custom method
>
> The class looks like this:
>
> #include "vtkImageData.h"
>
> class VTK_EXPORT vtkNewData: public vtkImageData
> {
> public:
>   static vtkNewData *New();
>   void set_myvalue(){
>   this->myvalue=3;
>   }
>   int get_myvalue(){
>   return this->myvalue;
>   }
>   int myvalue;
> };
>
>
> The CmakeLists.txt is adapted mostly from
> https://github.com/paulmelis/vtk-wrapping-example and looks like this
>
>
> #######################################
> # Pre-requisites
>
> # C++11 flags
> set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11")
>
> # VTK
> find_package(VTK REQUIRED
>                 vtkCommonCore
>                 vtkCommonDataModel
>                 vtkWrappingPythonCore
>             )
> include(${VTK_USE_FILE})
>
> # Python and vtk wrapping
> find_package(PythonLibs 2.7 EXACT REQUIRED)
> include(vtkWrapPython)
> include_directories("${PYTHON_INCLUDE_PATH}")
>
> #######################################
> # Building vtkNewData
> add_library(vtkNewData SHARED vtkNewData.cxx)
> TARGET_LINK_LIBRARIES(vtkNewData ${VTK_LIBRARIES} )
> if(APPLE)
>     set_target_properties(vtkNewData PROPERTIES SUFFIX ".so")
> endif()
>
> #######################################
> # Wrapping vtkNewData
> vtk_wrap_python3(vtkNewDataPython vtkNewDataPython_SRCS vtkNewData.cxx)
>
> add_library(vtkNewDataPythonD ${vtkNewDataPython_SRCS} vtkNewData.cxx)
> target_link_libraries(vtkNewDataPythonD
>         ${VTK_LIBRARIES}
>         vtkWrappingPythonCore
>         ${VTK_PYTHON_LIBRARIES})
>
> add_library(vtkNewDataPython MODULE vtkNewDataPythonInit.cxx)
>
> set(VTK_MODULES_USED vtkCommonDataModel vtkCommonCore)
> set(VTK_PYTHOND_LIBS)
> foreach(TMP_LIB ${VTK_MODULES_USED})
>    set(VTK_PYTHOND_LIBS ${VTK_PYTHOND_LIBS} ${TMP_LIB}PythonD)
> endforeach()
>
> target_link_libraries(vtkNewDataPython vtkNewDataPythonD
> ${VTK_PYTHOND_LIBS})
>
> set_target_properties(vtkNewDataPython PROPERTIES PREFIX "")
> if(WIN32 AND NOT CYGWIN)
>     set_target_properties(vtkNewDataPython PROPERTIES SUFFIX ".pyd")
> endif(WIN32 AND NOT CYGWIN)
>
>
> I suspect there is something very simple I am missing here but cannot for
> the life of me think what. Any help would be appreciated.
>
>
> Cheers
>
> Kit
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160729/248a08c3/attachment.html>


More information about the vtkusers mailing list