...
- Common Scripting Commands (a list and description of the most commonly used scripting commands)
- Scripting Versions of OpenSim C++ API Calls (information to help you use what are called "templatized" classes in OpenSim through the scripting interface)
- Scripting with Matlab (set-up instructions, examples and utilities, and other Matlab-specific scripting information)
- Converting C3D Files (a guide to working with C3D files through scripting)
...
Code Block |
---|
>>> import org.opensim.modeling.* >>> vec3 = osimVec3FromArray([1 2 3]); % Converts a Matlab Array to a OpenSim Vec3 >>> matLabArray = osimVec3ToArray(vec3); % Converts an OpenSim Vec3 to a Matlab Array |
Two additional functions that may be helpful to many are:
...
Often the best way of figuring out how to code is to use examples from already written scripts or functions. Many example scripts and functions can be found in the OpenSim 4.0 resources directory (Typically /Documents/OpenSim/4.0/Code/Matlab). You can also download some additional example scripts that use the current API. ;
plotMuscleFLCurves.m | Function to compute and plot the active and passive force-length muscle curves |
createActuatorsFile.m | Function to generate a generic OpenSim Actuator File from a Model |
build_and_simulate_simple_arm.m | Demonstrates building and simulating a simple arm model |
pendulum_marker_positions.m | Build, simulate, and generate outputs for a double-pendulum model. Writes the results to .sto and .trc files. |
...
Troubleshooting Tips
In addition to looking at example code, another great way to learn more about scripting in Matlab is to review the OpenSim Doxygen. The doxygen documents all the functionality of the OpenSim API. Guide to Using Doxygen.
...
If you are calling a method or function (e.g., getting or setting properties) that you are pretty sure should work, but you are getting an error that it the method doesn't exist, this may mean that you need to do something called downcasting. In the C++ programming language, programmers can use a concept called "inheritance" to build up complexity without re-using writing the same code multiple times. For example, in OpenSim Thelen2003Muscle and MillardMuscle both rely on code in the common parent Muscle class that they "inherit" from.
Matlab doesn't use this concept of inheritance so If you have a handle to a base class object (e.g. Muscle) you may need to downcast an the object like Muscle to one of its derived (or concrete) classes, like the Thelen2003Muscle, in order to gain access to properties and methods specific to the concrete class.
In this code example, we do XXXXXget a reference to a muscle in the model and return the class name and concrete class name.
Code Block |
---|
>>> model = Model(path2model) >>> muscle = musclemodel.getMuscles().get(0); >>> muscle.getClassName() muscle >>> muscle.getConcreteClassName() Thelen2003Muscle |
Then to get a version of the object that you can call the functions of interest on, you use safeDownCast()
Code Block |
---|
>>> musclesmuscle = model.getMuscles().get(0); #the object you get here is of base class Muscle >>> thelenMuscle = Thelen2003Muscle.safeDownCast(muscle) >>> ADD CODE TO DO SOMETHING # to use a method specific to Thelen2003Muscle you need to safeDownCast >>> timeConstant = thelenMuscle.getActivationTimeConstant() |
Case 2: The thing (class) I want to create has a name like Array_<double>.
As you look through the OpenSim doxygen or if you look at OpenSim API examples, in C++, you may see objects/classes with names like Array_<double> or Vec<3>. These are called templatized classes; this is another functionality that exists in C++ to help programmers simplify and reuse common code. But this doesn't exist in Matlab. So we have created scripting versions of the most commonly used templatized classes. You can find a list of all of these on the page Scripting Versions of OpenSim C++ API Calls. If you see a class name with angle brackets (< >), you can look it up on this page to find the class name to use in Matlab.
...
Adding Geometry Paths to Matlab
OpenSim verified verifies that Model's Geometry mesh files exists when you load a model and whenever you subsequently operate on it. If you have a Geometry file defined in your model, and the Geometry path is not set, you will get a warning for each piece of Geometry that can't be found, as shown in the image below.
...
Alternatively, a 'workaround' that if you are not visualizing the model is to remove the geometry from the model. This should only be used if you are doing many model instantiations or initSystem() calls and don't need to visualize the model.
...
Outputs
OpenSim 4.0 uses component outputs and reporters to collect variables of interest and print them to file. To display the output names for a component, use the method getOutputNames();
Code Block |
---|
>>> muscle.getOutputNames(); |