Page 1 of 1

Add Physical Offset frame to Joint API

Posted: Thu May 05, 2022 7:36 am
by brycekillen05
HI all,

I am trying to programatically add a physiscal offset frame to an existing model which currently does not have any frame. My primary problem is that I cannot edit/update the " socket_parent " is the Physical Offset Frame - and the " socket_parent_frame " of the joint which I suspect is becuase of a related issue. The lines which " do not work" are markerd with # % DOES NOT UPDATE

My code is below

Code: Select all

import opensim as osim

model = osim.Model('lenhart2015_updMuscPath.osim')

# make a copy of an exist physcial offset frame
pfo = model.getJointSet().get('femur_femur_distal_r').get_frames(0).clone()
# rename the frame
pfo.setName('knee_align_r')
# set orientation
pfo.set_orientation(osim.Vec3(1.5,0,0))
# set translation
pfo.set_translation(osim.Vec3(0,0,0))
# update the socket_parent of the frame

# % DOES NOT UPDATE pfo.setParentFrame(model.getBodySet().get('femur_distal_r'))

# ATEMPT 2 to update the socket_parent of the frame
# % DOES NOT UPDATE pfo.connectSocket_parent(model.getBodySet().get('femur_distal_r'))

# add the Physcial offset frame to the joint
model.getJointSet().get('knee_r').set_frames(0,pfo)

# Try and change the socket_parent_frame of the joint 
# % DOES NOT UPDATEmodel.getJointSet().get('knee_r').connectSocket_parent_frame(model.getJointSet().get('knee_r').get_frames(0))

model.updJointSet()
model.printToXML('testModel.osim')
Using this code - the physcial frame is added to the joint BUT the socket_frame , and socket_parent_frame are still from the "cloned" physical frame.

Thanks in advance for any tips/advice

Bryce

Re: Add Physical Offset frame to Joint API

Posted: Fri May 06, 2022 11:17 am
by silvestros
Hey Bryce,

How's it going?

Have you tried downcasting the Body frame to a PhysicalFrame? It worked for me in Matlab as I was having a similar issue.

Here's my code:

Code: Select all

close all;
clear;
clc;

% Import OpenSim 4.3 libraries
import org.opensim.modeling.*

model_name='FullShoulderModel.osim';

osim_model=Model(['..\..\OpenSim\In\Models\' model_name]);
init_state=osim_model.initSystem();

% safeDownCast Body::<scapula> to PhysicalFrame
PF_scapula=PhysicalFrame.safeDownCast(osim_model.getBodySet.get('scapula'));

% Create Frame
POF_glenoid_centre = PhysicalOffsetFrame();
POF_glenoid_centre.setName('glenoid_centre');
POF_glenoid_centre.setParentFrame(PF_scapula);

% Centre of Glenoid fossa @ scapular in FullShoulderModel.osim
POF_glenoid_centre.set_translation(Vec3(-0.0300675, -0.0391136, -0.015)); 

% Rotate for x-axis as parallel as posible to glenoid surf
POF_glenoid_centre.set_orientation(Vec3(0, 0.55, 0)); % @ scapular in FullShoulderModel.osim

% Pass back to the Model
osim_model.updBodySet.get('scapula').addComponent(POF_glenoid_centre);

% Finalise Connections
osim_model.finalizeConnections;

osim_model.print(['..\..\OpenSim\In\Models\' model_name(1:end-5) 'glC.osim']);
Hope it helps!

Cheers,
Pavlos

Re: Add Physical Offset frame to Joint API

Posted: Mon May 09, 2022 12:39 am
by brycekillen05
Hi Pavlos,

Thanks for the tips and the code

I'll give it a try !

Hope all is well mate

Cheers