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 disregard other installation instructions on this page and follow the link here https://anaconda.org/opensim-org/opensim
Python scripting is available from OpenSim 3.2, onward. Starting with 4.0, OpenSim is distributed as 64-bit only, so you must use 64-bit Python. If you are using 4.0, you need Python 2.7.x. If you are using 4.1 or 4.2, you need Python 3.7.x. Python version 3.8 is not supported for OpenSim version 4.2 or earlier; please use Python version 3.7 or earlier. Python version 3.8 is supported for OpenSim version 4.3+.
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:
- Python wikipedia
- python.org
- Python vs Matlab
- Google Python classes
- Python style guide (PEP8)
- Structuring a Python project
Note on Python 2 vs Python 3
- The Python package that comes with the OpenSim GUI distribution up to version 4.0 will only work with Python 2.7.x. OpenSim version 4.1 distribution upgrades to support Python version 3.7. If you want a different python version, you must build the OpenSim API (opensim-core) from scratch and set the CMake variable OPENSIM_PYTHON_VERSION to the desired version (2 or 3). In all text below that refers to Python version 2.7 (for versions 4.0 and earlier) the same applies to Python version 3 for version 4.1 and later. Starting with version 4.4, only Python version 3 is supported/maintained.
Setting up your Python scripting environment
Windows
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())