Is it possible to use Python API without opening Sim4Life?
-
Hi all,
As stated in the subject title, I would like to know if it is possible to run simulations without opening the Sim4Life GUI? I have the *.smash file saved, with all the entities and models required for the simulations. I have the Python scripts ready with all simulation parameters. My question is, can I run the python scripts in a different IDE, e.g. Visual Studio, importing all the required packages?
Thanks,
gbh -
short answer: you can run most of the Python API without opening the Sim4Life user interface.
you have different options
- you can call
Sim4Life.exe --run your_script.py
- you can write scripts and run them using the python.exe in the Sim4Life installation
- you can create a virtual environment with the Sim4Life packages, e.g.
"C:\Program Files\Sim4Life_6.2.2.6592\Python\python.exe" -m venv .venv --system-site-packages
and then use the python from this venv to run your script
The first option opens Sim4Life, runs the script, and closes Sim4Life again. It probably is the easiest option to work robustly.
The latter two options run without the UI and by default without an application. In most cases, you will need an application though (e.g. the Application initializes the active model, which is needed for modeling). For Python scripting without the UI you can create a console application. We do this e.g., to run Python tests without opening the UI (which is slower). To create a console application you can usually call:
import XCore XCore.GetOrCreateConsoleApp() # now you can do some stuff import s4l_v1 as s4l sphere = s4l.model.CreateSolidSphere(s4l.model.Vec3(0.0), 1.0)
- you can call
-
Hi @bryn,
Thanks for your reply! I have two follow-up questions.
- How would I call/activate a model that has been created beforehand?
- How do I call (or refer to) a specific sim contained in a *.smash file, without opening the GUI.
For context:
(1). I have several models stored in different .sab files. The models are all similar, with the only difference being the phantom in use. I want to perform a 'sweep' through various phantoms, and for each phantom, another sweep through various values for electrical conductivity.
(2). Provided that I already have multiple *.smash files, each containing multiple finished simulations. I now need to extract the results and perform some analysis (e.g. field interpolation), then export to csv file.Currently, I already have scripts to generate -> voxel/run -> analyze -> export the simulation data. But since I am using 8 different phantoms, contained in 8 separate *.smash batch, each with about 150 simulations, the pipeline takes a while and need manual monitoring or save/load.
Thanks,
gbh -
You can open a smash file using
import s4l_v1 as s4l s4l.document.Open(smash_file_path)
You can save it using
s4l.document.Save(smash_file_path)
You can find a simulation using (assuming you have unique simulation names
sim = [sim for sim in s4l.document.AllSimulations if sim.Name == "the name of your simulation"][0]
If you want to load different models (from .sab file or .smash), but don't need to load e.g. simulations (in .smash file), you can import the file
entities = s4l.model.Import(file_path)
if you need to reset the document or model because you are accumulating too many entities and memory use is getting too high, you can use e.g.
# reset the model sl4.model.Clear() # or keep entities, but discard model history import XCoreModeling as xcm xcm.GetActiveModel().ClearHistory() # or reset entire document (reset model, simulations, analysis) s4l.document.New()