BodyKinematics Tool in Python

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Chen Songyun
Posts: 4
Joined: Fri Oct 15, 2021 6:21 am

BodyKinematics Tool in Python

Post by Chen Songyun » Sun Jun 02, 2024 11:33 am

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()

Tags:

User avatar
Chen Songyun
Posts: 4
Joined: Fri Oct 15, 2021 6:21 am

Re: BodyKinematics Tool in Python

Post by Chen Songyun » Sun Jun 02, 2024 10:17 pm

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?

User avatar
Chen Songyun
Posts: 4
Joined: Fri Oct 15, 2021 6:21 am

Re: BodyKinematics Tool in Python

Post by Chen Songyun » Tue Jun 04, 2024 3:06 am

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()

POST REPLY