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 demo_python3.py 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
my_system.SetTol(2)
print (my_systemB.GetTol())

# Create a body
body_1= chrono.ChBodyAuxRefShared()
my_system.Add(body_1)

# Attach a visualization asset to the body (a Wavefront .obj mesh)
myasset = chrono.ChObjShapeFileShared()
myasset.SetFilename("body_1.obj")
myasset.SetColor(chrono.ChColor(1,1,0.8))
body_1.GetAssets().push_back(myasset)

# Assets can be shared, to save memory...
body_2= chrono.ChBodyAuxRefShared()
body_2.SetPos(chrono.ChVectorD(0.1,0,0))
my_system.Add(body_2)
body_2.GetAssets().push_back(myasset)

#
# 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"):
    os.mkdir("output")
if not os.path.exists("anim"):
    os.mkdir("anim")
pov_exporter.SetOutputDataFilebase("output/my_state")
pov_exporter.SetPictureFilebase("anim/picture")

 # Optional: tell selectively which physical items you want to render
pov_exporter.RemoveAll()
pov_exporter.Add(body_1)
pov_exporter.Add(body_2)

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

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

    my_system.DoStepDynamics(0.01)

    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)
    pov_exporter.ExportData()


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

  • execute the demo_python3.py 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 demo_python3.py 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