Skip to content
  • Search
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

ZMT zurich med tech

  1. Home
  2. Sim4Life
  3. Python API
  4. Is it possible to use Python API without opening Sim4Life?

Is it possible to use Python API without opening Sim4Life?

Scheduled Pinned Locked Moved Python API
4 Posts 2 Posters 700 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gbgbha
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • brynB Offline
      brynB Offline
      bryn
      ZMT
      wrote on last edited by
      #2

      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)
      
      1 Reply Last reply
      3
      • G Offline
        G Offline
        gbgbha
        wrote on last edited by
        #3

        Hi @bryn,

        Thanks for your reply! I have two follow-up questions.

        1. How would I call/activate a model that has been created beforehand?
        2. 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

        1 Reply Last reply
        0
        • brynB Offline
          brynB Offline
          bryn
          ZMT
          wrote on last edited by
          #4

          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()
          
          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Search