You are viewing the documentation for OpenSim 3.x. Are you looking for the latest OpenSim 4.0 Documentation?
Scripting in Python
Python wrapping is available from OpenSim 3.2, onward. The system architecture of Python & OpenSim must match (install 64-bit Python and 64-bit OpenSim) Python setup and configuration instructions are below.
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 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:
Whats available?
Access to the OpenSim API to create and simulate models
Users can instantiate and run tools from setup files or programmatically
Repeated/batch runs of tools
Users can write "main" programs similar to those written by C++ developers, while taking advantage of the Python environment and tools
A visualizer for API users that can be invoked through Python
Limitations?
In this mode, there’s no access to the OpenSim plotter or the graphics window (use the model/API visualizer instead).
Some internal Simtk classes (that belong to the SimTK namespace and simbody internals) are not available for construction, but the most commonly used classes are available as of version 3.1 (SimTK::Vec3, Vector, Mat33, State, Inertia).
Available Example Scripts?
We are currently in the process of developing example Python scripts for OpenSim and will share these with the community soon.
Setting up your python scripting environment
Windows
There are a couple different 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 that bundle all the required packages and include an interface for scripting.
This setup guide assumes you will use Enthought Canopy. Canopy is relatively easy to setup and configure, has a well defined user interface, has extensive help directories and maintains many up-to-date libraries. Canopy is a proprietary python distribution that is free with an academic license. Continuum Anaconda is another free (to all) package that includes Python, NumPy, and SciPy. You can also just set-up Python and the associated packages manually. We have tested all three methods. Make sure that you get 64-bit python if you are using 64-bit OpenSim libraries (and 32-bit python if using 32-bit OpenSim).
Package Name | Downloads | Costs/licensing |
|---|---|---|
Enthought Canopy | Academic licenses can be granted with a '.edu' email address. Basic package costs $199 per/year. | |
Continuum Anaconda | Basic package is free to download and use, contains Python 2.7 and other dependent packages. Cleanly installs into a single directory on your machine. | |
Python 2.7 | Free to use and download. |
Installing Canopy and the "opensim" python package
Having trouble?
If you are having trouble with getting python wrapping to work on Windows, the issue is almost certainly one of the following:
You are trying to use 32-bit python with 64-bit OpenSim libraries. If you are using 64-bit OpenSim libraries, make sure to use 64-bit python.
OpenSim's dll's are not on your PATH. In a command window, you could achieve this with a command like "set PATH=C:\OpenSim 3.2\bin;%PATH%"
Ubuntu
We assume that you will use Python through the terminal. Open a new terminal window. First, we'll acquire the necessary dependencies.
$ sudo apt-get install python-setuptoolsNavigate to the sdk/python folder in your OpenSim installation. Assuming that OpenSim is installed into directory OPENSIM_HOME, perform the following:
$ cd $OPENSIM_HOME/sdk/python
$ sudo python setup.py installThe above assumes that you've defined an environment variable OPENSIM_HOME via a command like:
$ export OPENSIM_HOME=/home/<username>/opensimThe OpenSim libraries must be on your LD_LIBRARY_PATH:
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$OPENSIM_HOME/libYou can place the above `export` commands in your `~/.bashrc` 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:
$ python
...
>>> import opensim
>>> m = opensim.Model()