Page 1 of 1

BodyKinematics Tool in Python

Posted: Sun Jun 02, 2024 11:33 am
by yuusutiia
Cause I have to analyze a lot of motion files, I am trying to use BodyKinematics to calculate the center of mass from mot files by Python. But I don't know how to use the Python script about BodyKinematics and Analyze Tool. Here is my test code. How can I finish it? I want to output the sto file like the Analyze tools in opensim GUI.

Code: Select all

import opensim as osim

# model file and trc file
model = osim.Model(r'I:\model\Kitaura.osim')
state = model.initSystem()
motion = osim.Storage(r'I:\model\1.mot')


#analyze set
a_tool = osim.AnalyzeTool(motion)
a_tool.setModel(model)
a_tool.setLowpassCutoffFrequency(6)

#body kinematics
bk_tool = osim.BodyKinematics()
bk_tool.setModel(model)

#analyze
a_tool.run()

Re: BodyKinematics Tool in Python

Posted: Sun Jun 02, 2024 10:17 pm
by yuusutiia
I used the function setStatesFromMotion, then I successfully set Analysis Tool. But I can't run analysis tool.

Code: Select all

import opensim as osim

# model file and trc file
model = osim.Model(r'I:\model\Kitaura.osim')
state = model.initSystem()
motion = osim.Storage(r'I:\model\1.mot')


#analyze set
a_tool = osim.AnalyzeTool()
a_tool.setModel(model)
a_tool.setCoordinatesFileName(r'I:\model\1.mot')
a_tool.setLowpassCutoffFrequency(6)
a_tool.setStatesFromMotion(state,motion,True)

#body kinematics
bk_tool = osim.BodyKinematics()
bk_tool.setModel(model)
bk_tool.setName('test')

#analyze
a_tool.setResultsDir(r'I:\model')
a_tool.setCoordinatesFileName('pos.sto')
a_tool.setStatesFileName('states.sto')
a_tool.setSpeedsFileName('ves.sto')
a_tool.run()
It will show the error that no analyses have been set.How can I set the analyses?

Re: BodyKinematics Tool in Python

Posted: Tue Jun 04, 2024 3:06 am
by yuusutiia
I successfully obtianed the COM by python program.

Code: Select all

import opensim as osim

# model file and trc file
model = osim.Model(r'D:\OpenSim 4.5\Kitaura.osim')
state = model.initSystem()
motion = osim.Storage(r'D:\OpenSim 4.5\1.mot')


#analyze set
a_tool = osim.AnalyzeTool()
a_tool.setModel(model)
a_tool.setCoordinatesFileName(r'D:\OpenSim 4.5\1.mot')
a_tool.setLowpassCutoffFrequency(6)
a_tool.setStatesFromMotion(state,motion,True)

#body kinematics
bk_tool = osim.BodyKinematics()
bk_tool.setModel(model)
bk_tool.setName('test')

#analyze
analysis_set = a_tool.getAnalysisSet()
analysis_set.cloneAndAppend(bk_tool)
a_tool.addAnalysisSetToModel()
a_tool.setResultsDir(r'D:\OpenSim 4.5')
a_tool.setCoordinatesFileName('pos.sto')
a_tool.setStatesFileName('states.sto')
a_tool.setSpeedsFileName('ves.sto')

a_tool.run()