[vtkusers] Python wrapping a custom class

David Gobbi david.gobbi at gmail.com
Mon Aug 1 08:31:03 EDT 2016


Try declaring m_dict as a PyObject:

  PyObject *m_dict;
  PyObject *getDict();
  void setDict(PyObject *);


On Mon, Aug 1, 2016 at 5:55 AM, Kit Chambers <kit.chambers.kc at gmail.com>
wrote:

> Hi David/VTK'ers,
>
> Following on from my previous email i am now having diffcultly using a
> more complicated data structure in a derived class. In this case i am
> trying:
>
> #include "vtkImageData.h"
> #include <boost/python.hpp>
>
> class VTK_EXPORT vtkImageDataDict: public vtkImageData
> {
> public:
>
>   vtkTypeMacro(vtkImageDataDict, vtkImageData);
>
>   // New method
>   static vtkImageDataDict *New();
>
>   // Python dictionary
>   boost::python::dict m_dict;
>
>   // test function to set the dictionary
>   void test_set_dict();
>
>   // prints the dictionary
>   void printImageDict();
>
>   // Dictionary getter
>   boost::python::dict getDict(){return this->m_dict;}
>
>   // Dictionary setter
>   void setDict(boost::python::object dd);
> };
>
>
> When I wrap the above class the test_set_dict and printImageDict work
> fine but the wrapper seems to ignore getDict()
> and setDict(). I assume this is because vtkWrapperPython does not know
> what to do with boost::python objects. Maybe I am going about this the
> wrong way? I did try doing the wrapping through boost python rather than
> VTK but found I ran into compilation issues.
>
> Any help would be very much appreciated.
>
> Kit
>
>
>
>
> On 29 Jul 2016, at 17:07, David Gobbi <david.gobbi at gmail.com> wrote:
>
> 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/20160801/5b4fa271/attachment.html>


More information about the vtkusers mailing list