Commit e1a03c70 authored by David C. Lonie's avatar David C. Lonie
Browse files

Check for matplotlib at runtime.

This change allows the RenderingMatplotlib module to be built without
finding matplotlib at configure time.

Change-Id: Iee352157d533cdf0bd7a23fcd0807b4dd6547c89
parent 159fba3b
# Check that matplotlib is available:
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import matplotlib"
RESULT_VARIABLE IMPORT_MATPLOTLIB_EXITCODE
OUTPUT_VARIABLE IMPORT_MATPLOTLIB_OUTPUT
ERROR_VARIABLE IMPORT_MATPLOTLIB_ERROR
)
set(MATPLOTLIB_FOUND FALSE)
if(${IMPORT_MATPLOTLIB_EXITCODE} EQUAL 0)
set(MATPLOTLIB_FOUND TRUE)
endif()
if(NOT MATPLOTLIB_FOUND)
message(FATAL_ERROR "Matplotlib not found! ('${PYTHON_EXECUTABLE} -c \"import matplotlib\"' failed.)")
endif()
set(Module_SRCS
vtkMatplotlibMathTextUtilities.cxx
${CMAKE_CURRENT_BINARY_DIR}/${vtk-module}ObjectFactory.cxx
......
if(VTK_GHOSTSCRIPT_EXECUTABLE AND VTK_DATA_ROOT)
set(GL2PSTests
TestGL2PSMathTextActor.cxx
TestGL2PSMathTextActor3D.cxx
TestGL2PSMathTextOutput.cxx
TestGL2PSMathTextScaling.cxx
# Check that matplotlib is available:
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import matplotlib"
RESULT_VARIABLE IMPORT_MATPLOTLIB_EXITCODE
OUTPUT_VARIABLE IMPORT_MATPLOTLIB_OUTPUT
ERROR_VARIABLE IMPORT_MATPLOTLIB_ERROR
)
set(MATPLOTLIB_FOUND FALSE)
if(${IMPORT_MATPLOTLIB_EXITCODE} EQUAL 0)
set(MATPLOTLIB_FOUND TRUE)
endif()
if(VTK_DATA_ROOT)
# add tests that require data
set(MyTests
TestContextMathTextImage.cxx
TestMathTextActor.cxx
TestMathTextActor3D.cxx
TestRenderString.cxx
TestStringToPath.cxx
TestIndexedLookupScalarBar.cxx
${GL2PSTests}
${MixedTests}
if(NOT MATPLOTLIB_FOUND)
message("Matplotlib not found! MathText rendering will not be available until it is installed. Disabling tests.")
endif()
if(MATPLOTLIB_FOUND)
if(VTK_GHOSTSCRIPT_EXECUTABLE AND VTK_DATA_ROOT)
set(GL2PSTests
TestGL2PSMathTextActor.cxx
TestGL2PSMathTextActor3D.cxx
TestGL2PSMathTextOutput.cxx
TestGL2PSMathTextScaling.cxx
)
endif()
if(VTK_DATA_ROOT)
# add tests that require data
set(MyTests
TestContextMathTextImage.cxx
TestMathTextActor.cxx
TestMathTextActor3D.cxx
TestRenderString.cxx
TestStringToPath.cxx
TestIndexedLookupScalarBar.cxx
${GL2PSTests}
)
endif()
endif()
# Use the testing object factory, to reduce boilerplate code in tests.
......
......@@ -64,12 +64,46 @@ public:
};
//----------------------------------------------------------------------------
vtkObjectFactoryNewMacro(vtkMatplotlibMathTextUtilities)
vtkMatplotlibMathTextUtilities::Availablity
vtkMatplotlibMathTextUtilities::MPLMathTextAvailable =
vtkMatplotlibMathTextUtilities::NOT_TESTED;
//----------------------------------------------------------------------------
vtkMatplotlibMathTextUtilities* vtkMatplotlibMathTextUtilities::New()
{
// Attempt to import matplotlib to check for availability
switch (vtkMatplotlibMathTextUtilities::MPLMathTextAvailable)
{
default:
case vtkMatplotlibMathTextUtilities::UNAVAILABLE:
return NULL;
case vtkMatplotlibMathTextUtilities::AVAILABLE:
break;
case vtkMatplotlibMathTextUtilities::NOT_TESTED:
Py_Initialize();
if (PyErr_Occurred() ||
!PyImport_ImportModule("matplotlib") ||
PyErr_Occurred())
{
vtkMatplotlibMathTextUtilities::MPLMathTextAvailable = UNAVAILABLE;
}
else
{
vtkMatplotlibMathTextUtilities::MPLMathTextAvailable = AVAILABLE;
}
if (vtkMatplotlibMathTextUtilities::MPLMathTextAvailable == UNAVAILABLE)
{
return NULL;
}
break;
}
VTK_OBJECT_FACTORY_NEW_BODY(vtkMatplotlibMathTextUtilities)
}
vtkInstantiatorNewMacro(vtkMatplotlibMathTextUtilities)
//----------------------------------------------------------------------------
vtkMatplotlibMathTextUtilities::vtkMatplotlibMathTextUtilities()
: Superclass(), PythonIsInitialized(false), MaskParser(NULL),
PathParser(NULL), FontPropertiesClass(NULL)
: Superclass(), MaskParser(NULL), PathParser(NULL), FontPropertiesClass(NULL)
{
}
......@@ -79,32 +113,12 @@ vtkMatplotlibMathTextUtilities::~vtkMatplotlibMathTextUtilities()
Py_XDECREF(this->MaskParser);
Py_XDECREF(this->PathParser);
Py_XDECREF(this->FontPropertiesClass);
if (this->PythonIsInitialized)
{
Py_Finalize();
}
}
//----------------------------------------------------------------------------
bool vtkMatplotlibMathTextUtilities::InitializePython()
{
if (this->PythonIsInitialized)
{
return true;
}
Py_Initialize();
this->PythonIsInitialized = !this->CheckForError();
return this->PythonIsInitialized;
Py_Finalize();
}
//----------------------------------------------------------------------------
bool vtkMatplotlibMathTextUtilities::InitializeMaskParser()
{
if (!this->InitializePython())
{
return false;
}
SmartPyObject mplMathTextLib(PyImport_ImportModule("matplotlib.mathtext"));
if (this->CheckForError(mplMathTextLib.GetPointer()))
{
......@@ -133,11 +147,6 @@ bool vtkMatplotlibMathTextUtilities::InitializeMaskParser()
//----------------------------------------------------------------------------
bool vtkMatplotlibMathTextUtilities::InitializePathParser()
{
if (!this->InitializePython())
{
return false;
}
SmartPyObject mplTextPathLib(PyImport_ImportModule("matplotlib.textpath"));
if (this->CheckForError(mplTextPathLib.GetPointer()))
{
......@@ -164,11 +173,6 @@ bool vtkMatplotlibMathTextUtilities::InitializePathParser()
//----------------------------------------------------------------------------
bool vtkMatplotlibMathTextUtilities::InitializeFontPropertiesClass()
{
if (!this->InitializePython())
{
return false;
}
SmartPyObject mplFontManagerLib(
PyImport_ImportModule("matplotlib.font_manager"));
if (this->CheckForError(mplFontManagerLib.GetPointer()))
......@@ -779,7 +783,6 @@ void vtkMatplotlibMathTextUtilities::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "PythonIsInitialized: " << this->PythonIsInitialized << endl;
os << indent << "MaskParser: " << this->MaskParser << endl;
os << indent << "PathParser: " << this->PathParser << endl;
os << indent << "FontPropertiesClass: " << this->FontPropertiesClass << endl;
......
......@@ -59,7 +59,6 @@ protected:
vtkMatplotlibMathTextUtilities();
virtual ~vtkMatplotlibMathTextUtilities();
bool InitializePython();
bool InitializeMaskParser();
bool InitializePathParser();
bool InitializeFontPropertiesClass();
......@@ -72,7 +71,6 @@ protected:
// the vtkTextProperty tprop.
PyObject * GetFontProperties(vtkTextProperty *tprop);
bool PythonIsInitialized;
PyObject *MaskParser;
PyObject *PathParser;
PyObject *FontPropertiesClass;
......@@ -81,6 +79,16 @@ protected:
// origin and calculate the bounding box
void RotateCorners(double angleDeg, double corners[4][2], double bbox[4]);
// Description:
// Used for runtime checking of matplotlib's mathtext availability.
enum Availablity
{
NOT_TESTED = 0,
AVAILABLE,
UNAVAILABLE
};
static Availablity MPLMathTextAvailable;
private:
vtkMatplotlibMathTextUtilities(const vtkMatplotlibMathTextUtilities&); // Not implemented.
void operator=(const vtkMatplotlibMathTextUtilities&); // Not implemented.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment