ChronoEngine:Unit PYTHON

From ChronoWiki

Jump to: navigation, search




Build depends:

Python SDK

Run depends:


Main developers:

Alessandro Tasora

This is an optional unit that adds Python support in Chrono::Engine. Read the introduction to units for a technical background on the modularity of the Chrono::Engine project.



The unit_PYTHON allows users to use the the famous Python language for creating simulations, with the typical advantages of scripting languages (immediate execution, no compilers, ease of use, etc.). This units, in particular, builds the Chrono::PyEngine wrapper.

This unit consists of two main sets of build targets:

  • The Python modules for Chrono::PyEngine. Currently, the PyChrono::Engine Python modules that are built are:
    • ChronoEngine_PYTHON_core, that wraps all the core Chrono::Engine,
    • ChronoEngine_PYTHON_postprocess, that wraps the unit_POSTPROCESS.
    • ChronoEngine_PYTHON_irrlicht, that wraps the unit_IRRLICHT.
  • A unit_PYPARSER is built, that is a c++ unit for parsing / executing / interpreting Python instructions from C++ programs.


  • To run applications based on this unit:
    • you must have Python installed.
  • To build this unit:
    • you must have Python installed,
    • you must have Swig wrapper generator installed.

Building instructions

  • Install SWIG on your system. (If needed, add its directory to your PATH so that swig.exe can be run from the shell).
  • Repeat the instructions for the full installation, but when you see the CMake window, you must add the following steps:
  • Set the ENABLE_UNIT_PYTHON as 'on', then press 'Configure' (to refresh the variable list)
  • Set the CH_PYTHONDIR variable to the directory where you have your copy of Python. For example, it could be C:/Python33
  • When you pressed 'Configure', CMake should have detected your SWIG tools: this should create two variables in CMake called SWIG_DIR and SWIG_EXECUTABLE. If all is fine, they should automatically contain meaningful values, for example C:/swigwin-2.0.4/Lib and C:/swigwin-2.0.4/swig.exe , so you do not need to touch them.
  • Press 'Configure' again, then 'Generate', and proceed as usual in the installation instructions.

Read the notes below for details and troubleshooting related to compilation of this unit.

After the successful compilation, the Chrono::PyEngine modules are built and installed in your Python environment, so you will be able to open a Python editor and to type Python commands that call Chrono::Engine functions.

See the intro to Python scripting to see how to use Chrono::PyEngine.


For your knowledge, the build process of the PyChrono::Engine Python modules, as generated by CMake, internally consists of these automatic steps:

- SWIG preprocesses many C++ source code files in order to generate a .cxx wrapper file that contains the code for accessing C++ functions through the Python API, and some .py files

- the C++ compiler compiles the .cxx file and generates one or more dll,

- these dll are copied into the /DLLs directory of your Python distribution, but their .dll suffix will be changed to .pyd; those files represent the Python modules,

- other required dll such as ChronoEngine.dll are copied in the /DLLs directory, with .dll suffix.

- auxiliary .py files will be placed in the /Lib directory of your Python distribution.

Note that the SWIG tool requires few minutes to process the source and to generate the .cxx wrapper file; this means that when you start the compilation of the entire Chrono::Engine project, the process might look 'frozen' for one or two minutes when SWIG does its job; just have patience and wait.

Important! if you installed Python for 32 bit, you must compile Chrono::Engine in 32 bit mode; viceversa if you installed Python for 64bit, you must compile Chrono::Engine in 64 bit mode. (this is because a 32 bit program cannot call functions in a 64 bit dll, and viceversa).

This unit is tested with Python 3.3 and 3.2. Support of the previous version Python 2.7 is discontinued.

In some distributions of Python, the debug library python33_d.lib (the debug version of the python33.lib library) is not included by default. If you need it because you recompile the python unit in debug mode, either you recompile the entire Python source, or you modify pyconfig.h to force the use of python33.lib anyway, doing this way:

1) comment out the line:

//#define Py_DEBUG

2) modify

#if defined(_DEBUG)
  #pragma comment(lib,"python33_d.lib")


#if defined(_DEBUG)
  #pragma comment(lib,"python33.lib")

3) press 'Advanced' in CMake, set the PYTHON_DEBUG_LIBRARY to the same lib that you have in PYTHON_LIBRARY, and press 'Generate' so that your project will link python33.lib instead than python33_d.lib.

Personal tools