[Insight-users] Wrapping Problems

Carlos Phillips carlos.phillips at mail.mcgill.ca
Tue, 3 Feb 2004 04:19:16 -0500


--Apple-Mail-2-855992082
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed

Hi,
I was able to build a test python module for wrapped ITK code. The code 
used is attached. Unfortunately, The python module does not quite work. 
I get the following error:

-----------------------------------------------------------------
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "Test.py", line 4, in ?
ImportError: No module named TestPython
-----------------------------------------------------------------

This does not occur when I change Test.py to:

-----------------------------------------------------------------
from InsightToolkit import *

#__itk_import_data__ = itkbase.preimport()
from TestPython import *
#itkbase.postimport(__itk_import_data__)
-----------------------------------------------------------------

It seems that these two lines should not be commented out, but when I 
do, everything works. Do I really need these lines?

Carlos


--Apple-Mail-2-855992082
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	x-unix-mode=0644;
	name="CMakeLists.txt"
Content-Disposition: attachment;
	filename=CMakeLists.txt

PROJECT(Test)

FIND_PACKAGE(ITK)


# ITK
FIND_PACKAGE(ITK)
IF(ITK_FOUND)
  INCLUDE(${ITK_USE_FILE})
ELSE(ITK_FOUND)
  MESSAGE(FATAL_ERROR
          "Cannot build InsightApplications without ITK.  Please set ITK_DIR.")
ENDIF(ITK_FOUND)


# Load the CableSwig settings used by ITK.
SET(CableSwig_DIR ${ITK_CableSwig_DIR})
FIND_PACKAGE(CableSwig)

INCLUDE_DIRECTORIES(${ITK_INCLUDE_DIRS}
                    ${Test_SOURCE_DIR})
LINK_DIRECTORIES(${ITK_LIBRARY_DIRS})

SET_SOURCE_FILES_PROPERTIES(Test_wrapPython.cxx GENERATED)
ADD_LIBRARY(_TestPython MODULE Test_wrapPython.cxx)
# we don't want a "lib" prefix, thanks.
SET_TARGET_PROPERTIES(_TestPython PROPERTIES PREFIX "")
TARGET_LINK_LIBRARIES(_TestPython ITKCommon SwigRuntimePython ${PYTHON_LIBRARY})

ADD_CUSTOM_COMMAND(
  COMMENT "Test_wrapPython.cxx from Test.i"
  SOURCE ${Test_SOURCE_DIR}/Test.i
  COMMAND ${CableSwig_cswig_EXE}
  ARGS -I${Test_SOURCE_DIR}
       -nocable -c ${IGNORE_WARNINGS}
       -o Test_wrapPython.cxx
       -python -c++ ${Test_SOURCE_DIR}/Test.i
  TARGET _TestPython
  OUTPUTS ${Test_BINARY_DIR}/Test_wrapPython.cxx
  DEPENDS ${Test_SOURCE_DIR}/Test.i)

FIND_PROGRAM(PYTHON_EXECUTABLE
  NAMES python2.3 python2.2 python2.1 python2.0 python1.6 python1.5 python
  PATHS
  [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\2.3\\InstallPath]
  [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\2.2\\InstallPath]
  [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\2.1\\InstallPath]
  [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\2.0\\InstallPath]
  [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\1.6\\InstallPath]
  [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\1.5\\InstallPath]
  )
MARK_AS_ADVANCED(PYTHON_EXECUTABLE)
IF(PYTHON_EXECUTABLE)
  # Add a target to copy the Test.py module to the build tree.
  ADD_CUSTOM_TARGET(test_pyc ALL echo "...")

  # Now create a list of Python files.
  SET(ITK_PYTHON_FILES Test)

  # Now copy these files if necessary.
  SET(ITK_PYTHON_SOURCE_FILES)
  SET(ITK_PYTHON_OUTPUT_FILES)
  IF(CMAKE_CONFIGURATION_TYPES)
    FOREACH(file ${ITK_PYTHON_FILES})
      SET(src "${Test_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${file}.py")
      SET(ITK_PYTHON_SOURCE_FILES ${ITK_PYTHON_SOURCE_FILES} ${src})
    ENDFOREACH(file)
  ELSE(CMAKE_CONFIGURATION_TYPES)
    FOREACH(file ${ITK_PYTHON_FILES})
      SET(src "${Test_BINARY_DIR}/${file}.py")
      SET(ITK_PYTHON_SOURCE_FILES ${ITK_PYTHON_SOURCE_FILES} ${src})
    ENDFOREACH(file)
  ENDIF(CMAKE_CONFIGURATION_TYPES)
  IF ("${Test_BINARY_DIR}" MATCHES "^${Test_SOURCE_DIR}$")
    #MESSAGE("In source build -- no need to copy Python files.")
  ELSE ("${Test_BINARY_DIR}" MATCHES "^${Test_SOURCE_DIR}$")
    IF(CMAKE_CONFIGURATION_TYPES)
      FOREACH(file ${ITK_PYTHON_FILES})
        SET(src "${Test_SOURCE_DIR}/${file}.py")
        SET(tgt "${Test_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${file}.py")
        ADD_CUSTOM_COMMAND(SOURCE ${src}
                           COMMAND ${CMAKE_COMMAND}
                           ARGS -E copy ${src} ${tgt}
                           OUTPUTS ${tgt}
                           TARGET test_pyc
                           COMMENT "source copy")
      ENDFOREACH(file)
    ELSE(CMAKE_CONFIGURATION_TYPES)
      FOREACH(file ${ITK_PYTHON_FILES})
        SET(src "${Test_SOURCE_DIR}/${file}.py")
        SET(tgt "${Test_BINARY_DIR}/${file}.py")
        ADD_CUSTOM_COMMAND(SOURCE ${src}
          COMMAND ${CMAKE_COMMAND}
          ARGS -E copy ${src} ${tgt}
          OUTPUTS ${tgt}
          TARGET test_pyc
          COMMENT "source copy"
          )
      ENDFOREACH(file)
    ENDIF(CMAKE_CONFIGURATION_TYPES)
  ENDIF ("${Test_BINARY_DIR}" MATCHES "^${Test_SOURCE_DIR}$")  

  # Byte compile the Python files.
  WRITE_FILE(${Test_BINARY_DIR}/compile_all_test
    "import compileall\n"
    "compileall.compile_dir('${Test_BINARY_DIR}')\n"
    "file = open('${Test_BINARY_DIR}/test_compile_complete', 'w')\n"
    "file.write('Done')\n")

  ADD_CUSTOM_COMMAND(
    SOURCE ${Test_BINARY_DIR}/compile_all_test
    COMMAND ${PYTHON_EXECUTABLE}
    ARGS ${Test_BINARY_DIR}/compile_all_test
    DEPENDS ${ITK_PYTHON_SOURCE_FILES}
    OUTPUTS "${Test_BINARY_DIR}/test_compile_complete"
    TARGET test_pyc
    )

  # Add a driver target.
  ADD_CUSTOM_COMMAND(
    SOURCE test_pyc
    DEPENDS "${Test_BINARY_DIR}/test_compile_complete"
    TARGET test_pyc
    )
ENDIF(PYTHON_EXECUTABLE)


--Apple-Mail-2-855992082
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	x-unix-mode=0644;
	name="Test.h"
Content-Disposition: attachment;
	filename=Test.h

#ifndef __Test_h
#define __Test_h

template <typename ImageType>
const char* className(ImageType *img)
{
  return img->GetNameOfClass();
};

#endif

--Apple-Mail-2-855992082
Content-Transfer-Encoding: quoted-printable
Content-Type: application/octet-stream;
	x-unix-mode=0644;
	name="Test.i"
Content-Disposition: attachment;
	filename=Test.i

%module=20TestPython

%{
#include=20"itkImage.h"
%}

%{
#include=20"Test.h"
%}

%include=20"Test.h"

%template(classNameUS2)=20className<itk::Image<short=20unsigned=20int,=20=
2>=20>;


--Apple-Mail-2-855992082
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream;
	x-unix-mode=0644;
	name="Test.py"
Content-Disposition: attachment;
	filename=Test.py

from InsightToolkit import *

__itk_import_data__ = itkbase.preimport()
from TestPython import *
itkbase.postimport(__itk_import_data__)

--Apple-Mail-2-855992082--