Creating SimTK Rotations from Numpy arrays

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Mhairi McInnes
Posts: 6
Joined: Tue Oct 18, 2022 7:40 am

Creating SimTK Rotations from Numpy arrays

Post by Mhairi McInnes » Thu Feb 15, 2024 5:38 am

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

Tags:

User avatar
Mhairi McInnes
Posts: 6
Joined: Tue Oct 18, 2022 7:40 am

Re: Creating SimTK Rotations from Numpy arrays

Post by Mhairi McInnes » Thu Feb 15, 2024 7:50 am

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

POST REPLY