Introduction to Python (3)

Create a postprocessing system based on POVray, using PyChrono.

  • create a basic system with two bodies
  • create a postprocessor object
  • add asset objects to rigid bodies, for visualization
  • generate POVray scripts for rendering a 3D animation
1 #-------------------------------------------------------------------------------
2 # Name: pychrono example
3 # Purpose:
4 #
5 # Author: Alessandro Tasora
6 #
7 # Created: 1/01/2019
8 # Copyright: (c) ProjectChrono 2019
9 #
10 #
11 # This file shows how to use POV ray for postprocessing, thanks to the
12 # utility functions in the unit_POSTPROCESS of Chrono::Engine.
13 #
14 # Note. Since this file requires a template file ( "_template_POV.pov" in the
15 # bin/data/ directory) whose position is set with a relative path, please
16 # make sure that the current directory of Python is the one where this demo
17 # resides, otherwise if you launch it from another directory it does not
18 # find the POV template.
19 #
20 #-------------------------------------------------------------------------------
21 
22 print ("Third tutorial: use the postprocess module.");
23 
24 
25 # Load the Chrono::Engine core module and the postprocessing module!
26 import pychrono as chrono
27 import pychrono.postprocess as postprocess
28 
29 # We will create two directories for saving some files, we need this:
30 import os
31 
32 
33 # Create a physical system,
34 my_system = chrono.ChSystemNSC()
35 my_systemB = my_system
36 my_system.SetTol(2)
37 print (my_systemB.GetTol())
38 
39 # Create a body
40 body_1= chrono.ChBodyAuxRef()
41 my_system.Add(body_1)
42 
43 # Attach a visualization asset to the body (ex.: a sphere)
44 myasset = chrono.ChSphereShape()
45 myasset.GetSphereGeometry().rad =0.2
46 body_1.GetAssets().push_back(myasset)
47 
48 # Assets can be shared, ex. to save memory...
49 body_2= chrono.ChBodyAuxRef()
50 body_2.SetPos(chrono.ChVectorD(0.5,0,0))
51 my_system.Add(body_2)
52 body_2.GetAssets().push_back(myasset)
53 
54 #
55 # Create an exporter to POVray !!!
56 #
57 
58 pov_exporter = postprocess.ChPovRay(my_system)
59 
60  # Sets some file names for in-out processes.
61 pov_exporter.SetTemplateFile ("../../../data/_template_POV.pov")
62 pov_exporter.SetOutputScriptFile ("rendering_frames.pov")
63 pov_exporter.SetOutputDataFilebase ("my_state")
64 pov_exporter.SetPictureFilebase ("picture")
65  # Save the .dat files and the .bmp files in two subdirectories,
66  # to avoid cluttering the current directory...
67 if not os.path.exists("output"):
68  os.mkdir("output")
69 if not os.path.exists("anim"):
70  os.mkdir("anim")
71 pov_exporter.SetOutputDataFilebase("output/my_state")
72 pov_exporter.SetPictureFilebase("anim/picture")
73 
74  # Tell selectively which physical items you want to render, or use AddAll()
75 pov_exporter.Add(body_1)
76 pov_exporter.Add(body_2)
77 
78 
79  # 1) Create the two .pov and .ini files for POV-Ray (this must be done
80  # only once at the beginning of the simulation).
81 pov_exporter.ExportScript()
82 
83  # Perform a short simulation
84 while (my_system.GetChTime() < 0.2) :
85 
86  my_system.DoStepDynamics(0.01)
87 
88  print ('time=', my_system.GetChTime() )
89 
90  # 2) Create the incremental nnnn.dat and nnnn.pov files that will be load
91  # by the pov .ini script in POV-Ray (do this at each simulation timestep)
92  pov_exporter.ExportData()
93 
94 
95 
96 # That's all! If all worked ok, this python script should
97 # have created a "rendering_frames.pov.ini" file that you can
98 # load in POV-Ray, then when you press 'RUN' you will see that
99 # POV-Ray will start rendering a short animation, saving the frames
100 # in the directory 'anim'.
101 
102