12 Importing OpenSim Models

12.2 Importing the OpenSim model

To import an OpenSim model, an application uses the class OpenSimParser. Its primary constructor is

OpenSimParser (File osimFile, File geometryDir)

where osimFile references the .osim file and geometryPath references the folder where the geometry is located. If geometryPath is specified as null, the parser will look for the geometry under either "Geometry" or "geometry", in the same folder as the .osim file.

After creating the parser, the model can be loaded by calling createModel(), which reads the model file, generates the equivalent ArtiSynth components, and arranges them in the specified MechModel:

import artisynth.core.opensim.OpenSimParser;
...
   MechModel mech;
   File osimFile;
   File osimGeometry;
   ...
   OpenSimParser parser = new OpenSimParser (osimFile, osimGeometry);
   parser.createModel (mech);

If the mech argument is null, createModel() generates a fresh MechModel and returns it.

The listing below shows a complete example given in

  artisynth.demos.opensim.OpenSimArm26

which imports the Arm26 model by Reinbolt, Seth, Habib, and Hamner, and based on an earlier model Kate Holzbaur.

1 package artisynth.demos.opensim;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import artisynth.core.gui.ControlPanel;
7 import artisynth.core.mechmodels.MechModel;
8 import artisynth.core.opensim.OpenSimParser;
9 import artisynth.core.workspace.RootModel;
10 import maspack.matrix.AxisAlignedRotation;
11
12 /**
13  * Basic model that reads in the OpenSim "arm26" model by Reinbolt, Seth,
14  * Habib, and Hamner, and based on an earlier model Kate Holzbaur.
15  */
16 public class OpenSimArm26 extends RootModel {
17
18    // Store MechModel and parser references for use by subclasses
19    MechModel myMech;
20    OpenSimParser myParser;
21
22    public void build (String[] args) throws IOException {
23       // create mech model to contain the model
24       myMech = new MechModel ("mech");
25       addModel (myMech);
26
27       // model files are located relative the source of this model
28       String osimDir = getSourceRelativePath("osim/");
29       File osimFile = new File (osimDir + "arm26_v4.osim"); // V4 version
30       // create the parser and build the model. Geometry is located
31       // in parent folder of the .osim file.
32       myParser = new OpenSimParser (osimFile);
33       myParser.createModel (myMech);
34
35       // set view orientation so that Y is up
36       setDefaultViewOrientation (AxisAlignedRotation.X_Y);
37       // set basic damping factor for rigid bodies
38       myMech.setInertialDamping (1.0);
39       // create panels to control coordinates and excitations
40       ControlPanel panel = myParser.createCoordinatePanel();
41       addControlPanel (panel);
42       panel = myParser.createExcitationPanel();
43       addControlPanel (panel);
44    }
45 }
Figure 12.1: OpenSimArm26 loaded into ArtiSynth.

When loaded into ArtiSynth, the model appears as in Figure 12.1. The OpenSim model contains a number of components that are not implemented by ArtiSynth, as seen by the console messages

OpenSimParser: ignoring ControllerSet
OpenSimParser: ignoring ContactGeometrySet
OpenSimParser: ignoring ProbeSet

After calling createModel(), the build() method changes the view orientation, sets damping for the rigid bodies. and creates control panels to adjust the excitations and joint coordinates. These are actions described further in Section 12.4.