If using Conda environment we STRONGLY recommend utilizing the conda package directly as a self contained option unrelated to the OpenSim application/GUI. It is the only supported mechanism for versions other than python 3.8. If using Conda please disregard installation instructions on this page and follow the link here https://anaconda.org/opensim-org/opensim
As of 12/2023, python 3.7 is past end of life thus all instructions specific to versions before OpenSim 4.3 (first version built against python 3.8) are moved to a separate section (old OpenSim/Python combinations) near the bottom of this page, we recommend upgrading to the latest version available if starting to use the OpenSim API from python.
Introduction to Python
Python is a widely used general purpose programming language. Python is free and open source, with a large user community that encourages sharing and user contributions. The Python language is very flexible, supporting object-oriented and procedural styles of computing. The Python design philosophy also emphasizes code readability which makes sharing and using code easier than with some other languages.
Those from scientific and engineering backgrounds who are new to Python should check out the following resources to help get started with the language:
Note on Python versions
- The Python package that comes with the OpenSim GUI distribution is built for a specific python version (OpenSim version 4.3+ distributions bindings were built against python 3.8 exclusively). Users should try to use the Conda packages mentioned above for all variants of python, and let us know if additional variants are needed. If not possible to use Conda, users can follow the installation instructions below (will only work if using the same python version as in the table below) or build the python bindings using their preferred python version from source repository.
OpenSim versions and the corresponding python versions used to build distributions:
OpenSim version Python version Available conda packages 4.3 3.8 - 4.4, 4.4.1 3.8 3.7, 3.8, 3.9, 3.10, 3.11 4.5 3.8 3.9, 3.10, 3.11
Setting up your Python scripting environment (if not using conda)
Windows
Mac
Ubuntu
Old OpenSim/Python combinations
Windows:
Add a folder to PYTHONPATH variable (needed for OpenSim 4.2)
Mac:
Ubuntu:
Available Example Scripts
Scripts can are located in the OpenSim distribution in the sdk/Scripts/Python folder.
Script Name | Description |
---|---|
build_simple_arm_model.py | This script generates a simple arm model, runs a simulation, and visualizes the results. |
wiring_inputs_and_outputs_with_TableReporter.py | This script shows how to write model outputs (the position of a body) to a data file. |
Pythonic extensions of the OpenSim API
We have add some pythonic aspects to our Python interface. All examples assume that you have already imported the opensim package ("import opensim").
Initializing a Vector from a Python list
v = opensim.Vector([6, 7, 8, 9]) rv = opensim.RowVector([1, 2, 5])
Accessing elements of VecX and Vector using brackets
v = opensim.Vec3(1, 2, 3) v[0] = 1.5 + v[1] w = opensim.Vector(5, 1.5) w[0] = 3 * w[1]
Printing the contents of a VecX or Vector
>>> v = opensim.Vec3(1, 2, 3) >>> print v ~[1,2,3]
Getting the length of a VecX or Vector
You can use the built-in python function len() on VecX (e.g., Vec3) and Vector:
w = opensim.Vector(5) if len(w) < 5: raise Exception()
Iterate over elements of any Set
There are two iterators: one that treats the Set like a python list, and another that treats the Set like a python dict:
model = opensim.Model("my_model.osim") for force in model.getForceSet(): print(force.getName()) for name, muscle in model.getMuscles(): print(name, muscle.get_max_isometric_force())
Iterate over all bodies in a model (even bodies not in the model's BodySet)
model = opensim.Model("my_model.osim") for body in model.getBodyList(): print(body.getName())