Some Notes on Python and PyGame

A little note about Python 3D display, STL file format, and C++ Calling.

Python FAQ

PyGame Resources

TVTK STL Reader and Writer

  • This package is based on VTK.
    The Visualization Toolkit (VTK) is an open-source, freely available software system for 3D computer graphics, image processing and visualization. VTK consists of a C++ class library and several interpreted interface layers including Tcl/Tk, Java, and Python. Kitware, whose team created and continues to extend the toolkit, offers professional support and consulting services for VTK. VTK supports a wide variety of visualization algorithms including: scalar, vector, tensor, texture, and volumetric methods; and advanced modeling techniques such as: implicit modeling, polygon reduction, mesh smoothing, cutting, contouring, and Delaunay triangulation. VTK has an extensive information visualization framework, has a suite of 3D interaction widgets, supports parallel processing, and integrates with various databases on GUI toolkits such as Qt and Tk. VTK is cross-platform and runs on Linux, Windows, Mac and Unix platforms.

  • TVTK-三维可视化数据

  • The MayaVi Data Visualizer

  • 3D visualization of DXF STL file using mayavi python script

  • VTK/Examples/Python/STLReader

Python 调用外部 DLL

可惜 类成员函数调用类内成员变量或函数会造成访问越界。

HOPE - A solution to call C++ member function from Python

boost

Rewrite the .h file to a Python Class

Consider a C++ class/struct that we want to expose to Python:

struct World
{
    void set(std::string msg) { this->msg = msg; }
    std::string greet() { return msg; }
    std::string msg;
};

We can expose this to Python by writing a corresponding Boost.Python C++ Wrapper:

#include <boost/python.hpp>

using namespace boost::python;

BOOST_PYTHON_MODULE(hello)
{
    class_<World>("World")
        .def("greet", &World::greet)
        .def("set", &World::set)
    ;
}

Here, we wrote a C++ class wrapper that exposes the member functions greet and set. Now, after building our module as a shared library, we may use our class World in Python. Here’s a sample Python session:

>>> import hello
>>> planet = hello.World()
>>> planet.set('howdy')
>>> planet.greet()
'howdy'

Boost.Python

This is the actual package responsible for the interoperability between C++ and Python

SWIG

SWIG is another possible solution. Maybe later.