Page History
...
Expand | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
There are a couple options for setting up Python on your Windows machine. You will need the main Python package, as well as NumPy and SciPy. Alternatively, there are applications such as Anaconda that bundle all the required packages. You can also just set-up Python and the associated packages manually. We have tested both methods, but currently advise you to use Anaconda. Make sure that you get 64-bit Python.
Special instructions for Python 3.8+ on Windows:Starting at version 3.8, Python changed how dll's are located on windows, in particular PATH is not used any more, instead every environment should set the path to locate the dlls using this snippet
This works for versions 4.2 and earlier, however for version 4.3+ you need to change directory to the sdk/Python folder under the install folder then invoke the following 2 commands in the Command Prompt or PowerShell:
Installing Anaconda and the "opensim" Python package
Add a folder to PYTHONPATH variable (needed for OpenSim 4.2)
Having trouble?If you are having trouble with getting python wrapping to work on Windows, the issue is almost certainly one of the following:
Step-by-step instructions when using Anaconda with OpenSim 4.3+
|
Mac
Expand | |||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
macOS comes with some version of Python, but you may also want to obtain Python through Homebrew or the Anaconda Python distribution. Note that the Python package that comes with the OpenSim GUI distribution was built to use the Python that comes with macOS, and it will not work with Homebrew's Python or with Anaconda Python; in the latter cases, you must compile OpenSim from the source code. Navigate to the location of the opensim python package within the OpenSim installation. If you are using OpenSim's GUI distribution, this location is likely /Applications/OpenSim 4.x/sdk/Python. If you built OpenSim from source, this location is likely <OPENSIM_INSTALL_DIR>/lib/python2.7/site-packages (or OPENSIM_INSTALL_DIR>/lib/python3.7/site-packages if using Python 3). Perform the following (modifying the path below if necessary):
The OpenSim libraries must be on your DYLD_LIBRARY_PATH:
Needed for OpenSim 4.2: You must also add OpenSim's python folder to PYTHONPATH, also by adding the following to the `~/.bashrc` file:
You can place the above `export` commands in your `~/.bash_profile` file so that they are set whenever you open up a new terminal. You should be ready to use the Python wrapping now. Try the following from any directory:
Step-by-step instructions when using Anaconda with OpenSim 4.3+
|
Ubuntu
Expand | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
We assume that you will use Python through the terminal. Open a new terminal window.
Navigate to the sdk/python folder in your OpenSim installation. Assuming that OpenSim is installed into directory OPENSIM_INSTALL_DIR, perform the following:
The OpenSim libraries must be on your LD_LIBRARY_PATH:
You can place the above `export` commands in your `~/.bashrc` file so that they are set whenever you open up a new terminal. Needed for OpenSim 4.2: You must also add OpenSim's python folder to PYTHONPATH, also by adding the following to the `~/.bashrc` file:
You should be ready to use the Python wrapping now. Try the following from any directory:
|
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
Code Block | ||
---|---|---|
| ||
v = opensim.Vector([6, 7, 8, 9]) rv = opensim.RowVector([1, 2, 5]) |
Accessing elements of VecX and Vector using brackets
Code Block | ||
---|---|---|
| ||
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
Code Block | ||
---|---|---|
| ||
>>> 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:
Code Block | ||
---|---|---|
| ||
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:
Code Block | ||
---|---|---|
| ||
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)
Code Block | ||
---|---|---|
| ||
model = opensim.Model("my_model.osim") for body in model.getBodyList(): print(body.getName()) |
...