How to generate Storage in Python

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Sébastien FRICKER
Posts: 2
Joined: Tue Jan 23, 2024 11:45 pm

How to generate Storage in Python

Post by Sébastien FRICKER » Wed Mar 27, 2024 12:27 am

Hello,
I would like to use a Python script to generate a .mot file, to use in Inverse Kinematics. I am using OpenSim 4.5.
I wanted to use the Storage class of the API, but I cannot make it work.

Here is my script:

Code: Select all

import opensim

storage = opensim.Storage(100)
storage.setColumnLabels(["a", "b", "c"])
storage.setDataColumn(0, [1, 2, 3])
The instruction setColumnLabels fails with TypeError: in method 'Storage_setColumnLabels', argument 2 of type 'OpenSim::Array< std::string > const &'

The instruction setDataColumn fails with TypeError: in method 'Storage_setDataColumn', argument 3 of type 'OpenSim::Array< double > const &'

What am I doing wrong?

Tags:

User avatar
Nicos Haralabidis
Posts: 196
Joined: Tue Aug 16, 2016 1:46 am

Re: How to generate Storage in Python

Post by Nicos Haralabidis » Wed Mar 27, 2024 9:04 am

Hello Sebastien,

storage.setColumnLabels takes a String Array as input: https://simtk.org/api_docs/opensim/api_ ... 4c9edbe51a

So you need to create a string array for your column labels like so (in Matlab):
col_labels = ArrayStr();
col_labels.set(0,'a');
col_labels.set(1,'b');

then you can set the column labels to the storage object:

sto = Storage();
sto.setColumnLabels(col_labels);

Hope that helps,

Nicos

User avatar
Sébastien FRICKER
Posts: 2
Joined: Tue Jan 23, 2024 11:45 pm

Re: How to generate Storage in Python

Post by Sébastien FRICKER » Thu Mar 28, 2024 3:35 am

Thanks, that helped a lot! I was able to generate a Storage and write to .sto file.
Here is an example Python script, in case that's helpful for anyone.

Code: Select all

import opensim

storage = opensim.Storage()
storage.setName("Test storage")
storage.setInDegrees(True)

columnLabels = opensim.ArrayStr()
columnLabels.set(0, "time")
columnLabels.set(1, "a")
columnLabels.set(2, "b")
columnLabels.set(3, "c")
storage.setColumnLabels(columnLabels)

row0 = opensim.ArrayDouble()
row0.append(1)
row0.append(2)
row0.append(3)
storage.append(0.0, row0)

row1 = opensim.ArrayDouble()
row1.append(2)
row1.append(3)
row1.append(4)
storage.append(0.5, row1)

row2 = opensim.ArrayDouble()
row2.append(6)
row2.append(6)
row2.append(6)
storage.append(1.0, row2)

storage.printToFile(r"C:\storage.sto", "w", "No comment")

User avatar
Riza Bayoglu
Posts: 26
Joined: Wed Aug 02, 2023 8:54 am

Re: How to generate Storage in Python

Post by Riza Bayoglu » Wed Apr 17, 2024 1:24 pm

Thanks for sharing. This is very helpful!

POST REPLY