Page 1 of 1

Creating SimTK Rotations from Numpy arrays

Posted: Thu Feb 15, 2024 5:38 am
by m_mcinnes
Hi there,

Using the OpenSim API through python scripting, how can I create a SimTK Rotation from a numpy array matrix?

More generally - I am trying to update the transform of an IMU PhysicalOffsetFrame associated with a body in my model (so I can manually apply my own calibrations, rather than rely on OpenSim's built-in IMUPlacer tool).
My code looks like this so far:

Code: Select all

    import opensim as osim
    import numpy as np 
    
    model = osim.Model(model_file)
    IMU_frame = model.getBodySet().get('thorax').getComponent('thorax_imu')
    rotation_array = np.array([[1, 0, 0],[0, 1, 0],[0, 0, 1]])  # Create an example numpy array
    osim_rotation = osim.Rotation(rotation_array)   # Create an opensim rotation (this doesn't work)
    transform = osim.Transform(osim_rotation)   # Create an opensim transform from the rotation
    IMU_frame.setOffsetTransform(transform)  # Update the IMU frame transform
I get an error message which says I have the wrong number or type of argument for creating a new rotation, and that I need a SimTK Rotation, or Mat33, or Quaternion, but I don't know how to create these in python, without reading in quaternion tables from an data file.

Thanks for any help,

Mhairi

Re: Creating SimTK Rotations from Numpy arrays

Posted: Thu Feb 15, 2024 7:50 am
by m_mcinnes
*** UPDATE***

I've found a solution (for anyone else interested).

Creating a rotation in python scripting can be as simple as this:

Code: Select all

    mat = osim.Mat33(1, 0, 0,
                     0, 1, 0, 
                     0, 0, 1)
    rot = osim.Rotation(mat)
I was then able to create my transform from the rotation and a translation vector:

Code: Select all

    transform = osim.Transform(rot, osim.Vec3(0, 0.7, 0))