How to convert .c3d to .mot and .trc in python

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
corrine bao
Posts: 1
Joined: Thu Sep 27, 2018 1:59 am

How to convert .c3d to .mot and .trc in python

Post by corrine bao » Sun Feb 09, 2020 5:42 pm

Hello,

I checked this link https://simtk-confluence.stanford.edu:8 ... d%29+Files, in which .c3d can be written into two .sto files using python.

I know that MATLAB can convert .c3d to .mot and .trc files, is it possible to do so in python? If so, does anyone have any examples?

Many thanks,

Tags:

User avatar
Dimitar Stanev
Posts: 1096
Joined: Fri Jan 31, 2014 5:14 am

Re: How to convert .c3d to .mot and .trc in python

Post by Dimitar Stanev » Mon Feb 10, 2020 2:09 am

Hi,

I am attaching a script and some utility functions that I use in Python. Please note that some settings (rotation, mirror) should be changed to correspond to your lab's coordinate system.
scripts.zip
(7.21 KiB) Downloaded 60 times

User avatar
Mohammadreza Rezaie
Posts: 401
Joined: Fri Nov 24, 2017 12:48 am

Re: How to convert .c3d to .mot and .trc in python

Post by Mohammadreza Rezaie » Tue Feb 11, 2020 3:38 pm

mitkof6 wrote:
Mon Feb 10, 2020 2:09 am
scripts.zip
Hi Dimitar,

Many thanks for sharing these scripts.
Do you have any solution for python 3.7 (OpenSim 4.1)?
It seems that the BTK package has been only developed for python 2.7.

Cheers

User avatar
Dimitar Stanev
Posts: 1096
Joined: Fri Jan 31, 2014 5:14 am

Re: How to convert .c3d to .mot and .trc in python

Post by Dimitar Stanev » Wed Feb 12, 2020 6:52 pm

These should work with Python 3.7. Actually btk and any related functions can be removed from utils.py. I do some additional processing of c3d that is currently not supported in OpenSim. By the way I had a PR on btk so that it can be build with Python 3:

https://github.com/Biomechanical-ToolKi ... re/pull/29

User avatar
Kenneth Smale
Posts: 12
Joined: Thu Mar 05, 2015 10:11 am

Re: How to convert .c3d to .mot and .trc in python

Post by Kenneth Smale » Mon Mar 15, 2021 11:37 am

mitkof6 wrote:
Mon Feb 10, 2020 2:09 am
Hi,

I am attaching a script and some utility functions that I use in Python. Please note that some settings (rotation, mirror) should be changed to correspond to your lab's coordinate system.

scripts.zip
Hi @Dimitar,

I've set up python 3.7 and OpenSim 4.1 in my Anaconda-Spyder environment. When I try to read my c3d file through the adapter, I get the following error:

Code: Select all

static = adapter.read(os.path.join(c3d_dir, static_file), 1)
Traceback (most recent call last):

  File "<ipython-input-73-526983ac6a6d>", line 1, in <module>
    static = adapter.read(os.path.join(c3d_dir, static_file), 1)

TypeError: read() takes 2 positional arguments but 3 were given
This error is confusing because I believe I'm only passing two arguments (full file name, and 1) so I'm not sure what the issue is. If I simply pass the file path and name to adapter.read, it passes without error but my datatype is "common.StdMapStringAbstractDataTable", which I believe causes problems later on when trying rotate_data_table.

Any idea where I'm going awry here?

User avatar
Ayman Habib
Posts: 2244
Joined: Fri Apr 01, 2005 12:24 pm

Re: How to convert .c3d to .mot and .trc in python

Post by Ayman Habib » Thu Mar 18, 2021 11:15 pm

Hello,

Here's an example from our code base (version 4.2 (beta))
https://github.com/opensim-org/opensim- ... ter.py#L33

Hope this helps,
Ayman

User avatar
Mohammadreza Rezaie
Posts: 401
Joined: Fri Nov 24, 2017 12:48 am

Re: How to convert .c3d to .mot and .trc in python

Post by Mohammadreza Rezaie » Tue Mar 23, 2021 3:11 pm

Hi Kenneth,

First, I would like to thank Dr. Dimitar Stanev for sharing the scripts.

BTK can be installed in python 3.x via this conda distribution:

Code: Select all

conda install -c conda-forge btk
Regarding the error, please add this line before "static":

Code: Select all

adapter.setLocationForForceExpression(opensim.C3DFileAdapter.ForceLocation_CenterOfPressure)
and modify the "static" to:

Code: Select all

static = adapter.read(os.path.join(c3d_dir, static_file))
You can find more info here:
https://simtk.org/api_docs/opensim/api_ ... apter.html

Hope this helps.

Regards,
Mohammadreza

POST REPLY