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 12 13import os 14import pychrono as chrono 15import pychrono.postprocess as postprocess 16 17 18# The path to the Chrono data directory containing various assets (meshes, textures, data files) 19# is automatically set, relative to the default location of this demo. 20# If running from a different directory, you must change the path to the data directory with: 21#chrono.SetChronoDataPath('path/to/data') 22 23 24# --------------------------------------------------------------------- 25# 26# Create the simulation system. 27# (Do not create parts and constraints programmatically here, we will 28# load a mechanism from file) 29 30my_system = chrono.ChSystemNSC() 31 32 33# Set the collision margins. This is expecially important for very large or 34# very small objects (as in this example)! Do this before creating shapes. 35chrono.ChCollisionModel.SetDefaultSuggestedEnvelope(0.001); 36chrono.ChCollisionModel.SetDefaultSuggestedMargin(0.001); 37 38 39# --------------------------------------------------------------------- 40# 41# load the file generated by the SolidWorks CAD plugin 42# and add it to the system 43# 44 45print ("Loading Chrono scene..."); 46 47# Note that the ImportSolidWorksSystem() function requires the name of the 48# .py file generated by the Chrono::SolidWorks plugin, but without the ".py" 49# suffix. Here we use an example mechanism that we stored in the data/ directory, 50# but we could also use an absolute path as ..("C:/my_path/swiss_escapement") 51exported_items = chrono.ImportSolidWorksSystem(chrono.GetChronoDataFile('solid_works/swiss_escapement')) 52 53print ("...done!"); 54 55# Print exported items 56for my_item in exported_items: 57 print (my_item.GetName()) 58 59# Add items to the physical system 60for my_item in exported_items: 61 my_system.Add(my_item) 62 63 64# --------------------------------------------------------------------- 65# 66# Render a short animation by generating scripts 67# to be used with POV-Ray 68# 69 70pov_exporter = postprocess.ChPovRay(my_system) 71 72 # Sets some file names for in-out processes. 73pov_exporter.SetTemplateFile(chrono.GetChronoDataFile('_template_POV.pov')) 74pov_exporter.SetOutputScriptFile("rendering_frames.pov") 75if not os.path.exists("output"): 76 os.mkdir("output") 77if not os.path.exists("anim"): 78 os.mkdir("anim") 79pov_exporter.SetOutputDataFilebase("output/my_state") 80pov_exporter.SetPictureFilebase("anim/picture") 81 82 # Sets the viewpoint, aimed point, lens angle 83pov_exporter.SetCamera(chrono.ChVectorD(0.2,0.3,0.5), chrono.ChVectorD(0,0,0), 35) 84 85 # Sets the default ambient light and default light lamp 86pov_exporter.SetAmbientLight(chrono.ChColor(1,1,0.9)) 87pov_exporter.SetLight(chrono.ChVectorD(-2,2,-1), chrono.ChColor(0.9,0.9,1.1), True) 88 89 # Sets other settings 90pov_exporter.SetPictureSize(640,480) 91pov_exporter.SetAmbientLight(chrono.ChColor(2,2,2)) 92 93 # If wanted, turn on the rendering of COGs, reference frames, contacts: 94#pov_exporter.SetShowCOGs (1, 0.05) 95#pov_exporter.SetShowFrames(1, 0.02) 96#pov_exporter.SetShowLinks(1, 0.03) 97#pov_exporter.SetShowContacts(1, 98# postprocess.ChPovRay.SYMBOL_VECTOR_SCALELENGTH, 99# 0.01, # scale 100# 0.0007, # width 101# 0.1, # max size 102# 1,0,0.5 ) # colormap on, blue at 0, red at 0.5 103 104 # Add additional POV objects/lights/materials in the following way, entering 105 # an optional text using the POV scene description laguage. This will be 106 # appended to the generated .pov file. 107 # For multi-line strings, use the python ''' easy string delimiter. 108pov_exporter.SetCustomPOVcommandsScript( 109''' 110light_source{ <1,3,1.5> color rgb<0.9,0.9,0.8> } 111''') 112 113 # Tell which physical items you want to render 114pov_exporter.AddAll() 115 116 117 # 1) Create the two .pov and .ini files for POV-Ray (this must be done 118 # only once at the beginning of the simulation). 119pov_exporter.ExportScript() 120 121 # Configure the solver, if needed 122my_system.SetSolverType(chrono.ChSolver.Type_BARZILAIBORWEIN) 123my_system.SetSolverMaxIterations(40) 124my_system.SetMaxPenetrationRecoverySpeed(0.002) 125my_system.Set_G_acc(chrono.ChVectorD(0,-9.8,-9.80)) 126 127 # Perform a short simulation 128nstep =0 129while (my_system.GetChTime() < 1.2) : 130 131 my_system.DoStepDynamics(0.002) 132 133 #if math.fmod(nstep,10) ==0 : 134 print ('time=', my_system.GetChTime() ) 135 136 # 2) Create the incremental nnnn.dat and nnnn.pov files that will be load 137 # by the pov .ini script in POV-Ray (do this at each simulation timestep) 138 pov_exporter.ExportData() 139 140 nstep = nstep +1 141 142 143 144