Skip to content

Instantly share code, notes, and snippets.

@johnjdavisiv
Created August 12, 2021 20:46
Show Gist options
  • Save johnjdavisiv/26b17b41afd7555e0c18f8cd84f38123 to your computer and use it in GitHub Desktop.
Save johnjdavisiv/26b17b41afd7555e0c18f8cd84f38123 to your computer and use it in GitHub Desktop.
Use MATLAB API to run the OpenSim muscle line of action plugin
% --- Demonstrate using the muscle line of action plugin via the MATLAB API ---
%John Davis
%john@johnjdavis.io
%The MATLAB path is assumed to be in the Arm26 directory distributed with the plugin
import org.opensim.modeling.*
%Load the custom library - you have to leave the .dll ending *off* the path to the plugin
opensimCommon.LoadOpenSimLibrary('../MuscleForceDirection');
%After installation you'd usually have the above line look like:
%opensimCommon.LoadOpenSimLibrary('C:/OpenSim 4.2/plugins/MuscleForceDirection');
%Load up an AnalyzeTool instance using the provided config
config_file = 'MySetupFile.xml';
analyze_tool = AnalyzeTool(config_file);
%Access the MuscleForceDirection analysis
muscle_force_direction = analyze_tool.getAnalysisSet.get('MuscleForceDirection');
%You can also get it by index, e.g. analyze_tool.getAnalysisSet.get(0);
%As far as MATLAB knows, muscle_force_direction is an instance of the generic Analysis class
disp(class(muscle_force_direction));
%As a result, we have access to some useful generic methods from that class
%(you can see all of them by doing methodsview(muscle_force_direction);
%Change start/end times and step interval (useful if analyzing CMC results with small timesteps)
muscle_force_direction.setStartTime(0.25);
muscle_force_direction.setEndTime(0.75);
muscle_force_direction.setStepInterval(2);
%We should also change start and end time of analyze_tool
analyze_tool.setStartTime(0.25);
analyze_tool.setFinalTime(0.75); %For some reason the method is called FinalTime instead of EndTime!
%Properties specific to the plugin, like print_muscle_attachments, can be changed using the new
%PropertyHelper class (in Opensim 4.2+)
%See https://simtk-confluence.stanford.edu/display/OpenSim/Scripting+with+Matlab#ScriptingwithMatlab-UsingPropertyHelpertosetPropertyvaluesforpluginclasses
prop = muscle_force_direction.getPropertyByName('print_muscle_attachments');
curr_value = PropertyHelper.getValueBool(prop);
new_value = true;
PropertyHelper.setValueBool(new_value, prop);
%These properties are all set by pointers, so the values in analyze_tool have changed as well.
disp(analyze_tool.getAnalysisSet.get('MuscleForceDirection').getPropertyByName('print_muscle_attachments'));
%Save as a new analysis config
analyze_tool.print('my_new_setup.xml');
%Important! To run the tool with the updated settings, we need a fresh version loaded from the new config file
new_analyze_tool = AnalyzeTool('my_new_setup.xml');
new_analyze_tool.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment