ChronoPyEngine:Demo python3

From ChronoWiki

Jump to: navigation, search

Before executing this tutorial, be sure that your PyChrono::Engine is installed.

This tutorial uses Python as a language, so you can type it into PyScripter or IDLE or similar Python developing environments.

The original file is available in the source directory of the Chrono::Engine SDK.

In this simple lesson you learn how to create a simple animation of a falling cube on a floor, then you can use the postprocessing module for creating a script that can be used in POVray to render a 3D animation.

# Load the Chrono::Engine unit and the postprocessing unit!!!
import ChronoEngine_PYTHON_core as chrono
import ChronoEngine_PYTHON_postprocess as postprocess

# We will create two directories for saving some files, we need this:
import os

# Create a physical system,
my_system = chrono.ChSystem()
my_systemB = my_system
print (my_systemB.GetTol())

# Create a body
body_1= chrono.ChBodyAuxRefShared()

# Attach a visualization asset to the body (a Wavefront .obj mesh)
myasset = chrono.ChObjShapeFileShared()

# Assets can be shared, to save memory...
body_2= chrono.ChBodyAuxRefShared()

# Create an exporter to POVray !!!

pov_exporter = postprocess.ChPovRay(my_system)

 # Sets some file names for in-out processes.
pov_exporter.SetTemplateFile        ("../../../bin/data/_template_POV.pov")
pov_exporter.SetOutputScriptFile    ("rendering_frames.pov")
pov_exporter.SetOutputDataFilebase  ("my_state")
pov_exporter.SetPictureFilebase     ("picture")
 # Even better: save the .dat files and the .bmp files
 # in two subdirectories, to avoid cluttering the current
 # directory...
if not os.path.exists("output"):
if not os.path.exists("anim"):

 # Optional: tell selectively which physical items you want to render

 # 1) Create the two .pov and .ini files for POV-Ray (this must be done
 #    only once at the beginning of the simulation).

 # Perform a short simulation
while (my_system.GetChTime() < 0.2) :


    print ('time=', my_system.GetChTime() )

    # 2) Create the incremental nnnn.dat and nnnn.pov files that will be load
    #    by the pov .ini script in POV-Ray (do this at each simulation timestep)

That's all! Now: run this demo in this way:

  • execute the in your Python IDE or by double clicking on it,
  • on the console you will see a time counter showing that the system is being simulated, and for each timestep some files will be saved to be used later by POVray,
  • when the program ends, you must open POVray and open the rendering_frames.pov.ini file, using the Open menu or button, or drag&drop (you can find this .ini file and other POVray as they are saved in the same directory of the file)

  • press the Run button in POVray to execute the .ini file , and you should see that POVray will start rendering a short animation, saving the .BMP frames in the directory anim.
Personal tools