I like to change the coordinate values in my model using Python scripting. I am analyzing only one state. Reading through the API documents and the forum questions, I used setValue() property of the coordinates and assemble() afterwards. However, my code below does not seem to update the default coordinate values.
Code: Select all
model = osim.Model(modelpath)
actuators = osim.ForceSet('mymodel.xml')
forceset = model.updForceSet()
for i in range(0, actuators.getSize() -1 ):
forceset.cloneAndAppend(actuators.get(i))
analyze_tool = osim.AnalyzeTool('staticOpt_Setup.xml')
analyze_tool.setModel(model)
analyze_tool.setStartTime(0)
analyze_tool.setFinalTime(0)
state = model.initSystem()
coord_set = model.getCoordinateSet()
coords = ['pelvis_tilt', 'Abs_FE', 'Abs_LB', 'Abs_AR', 'L5_S1_FE', 'L5_S1_LB', 'L5_S1_AR', 'L4_L5_FE', 'L4_L5_LB', 'L4_L5_AR', 'L3_L4_FE', 'L3_L4_LB', 'L3_L4_AR', 'L2_L3_FE', 'L2_L3_LB', 'L2_L3_AR', 'L1_L2_FE', 'L1_L2_LB', 'L1_L2_AR', 'T12_L1_FE', 'T12_L1_LB', 'T12_L1_AR', 'T11_T12_FE', 'T11_T12_LB', 'T11_T12_AR', 'T10_T11_FE', 'T10_T11_LB', 'T10_T11_AR', 'T9_T10_FE', 'T9_T10_LB', 'T9_T10_AR', 'T8_T9_FE', 'T8_T9_LB', 'T8_T9_AR', 'T7_T8_FE', 'T7_T8_LB', 'T7_T8_AR', 'T6_T7_FE', 'T6_T7_LB', 'T6_T7_AR', 'T5_T6_FE', 'T5_T6_LB', 'T5_T6_AR', 'T4_T5_FE', 'T4_T5_LB', 'T4_T5_AR', 'T3_T4_FE', 'T3_T4_LB', 'T3_T4_AR', 'T2_T3_FE', 'T2_T3_LB', 'T2_T3_AR', 'T1_T2_FE', 'T1_T2_LB', 'T1_T2_AR', 'T1_head_neck_FE', 'T1_head_neck_LB', 'T1_head_neck_AR', 'T12_r12R_X', 'T12_r12R_Y', 'T12_r12R_Z', 'T11_r11R_X', 'T11_r11R_Y', 'T11_r11R_Z', 'T10_r10R_X', 'T10_r10R_Y', 'T10_r10R_Z', 'T9_r9R_X', 'T9_r9R_Y', 'T9_r9R_Z', 'T8_r8R_X', 'T8_r8R_Y', 'T8_r8R_Z', 'T7_r7R_X', 'T7_r7R_Y', 'T7_r7R_Z', 'T6_r6R_X', 'T6_r6R_Y', 'T6_r6R_Z', 'T5_r5R_X', 'T5_r5R_Y', 'T5_r5R_Z', 'T4_r4R_X', 'T4_r4R_Y', 'T4_r4R_Z', 'T3_r3R_X', 'T3_r3R_Y', 'T3_r3R_Z', 'T2_r2R_X', 'T2_r2R_Y', 'T2_r2R_Z', 'T1_r1R_X', 'T1_r1R_Y', 'T1_r1R_Z', 'T12_r12L_X', 'T12_r12L_Y', 'T12_r12L_Z', 'T11_r11L_X', 'T11_r11L_Y', 'T11_r11L_Z', 'T10_r10L_X', 'T10_r10L_Y', 'T10_r10L_Z', 'T9_r9L_X', 'T9_r9L_Y', 'T9_r9L_Z', 'T8_r8L_X', 'T8_r8L_Y', 'T8_r8L_Z', 'T7_r7L_X', 'T7_r7L_Y', 'T7_r7L_Z', 'T6_r6L_X', 'T6_r6L_Y', 'T6_r6L_Z', 'T5_r5L_X', 'T5_r5L_Y', 'T5_r5L_Z', 'T4_r4L_X', 'T4_r4L_Y', 'T4_r4L_Z', 'T3_r3L_X', 'T3_r3L_Y', 'T3_r3L_Z', 'T2_r2L_X', 'T2_r2L_Y', 'T2_r2L_Z', 'T1_r1L_X', 'T1_r1L_Y', 'T1_r1L_Z', 'SternumX', 'SternumY', 'SternumZ', 'SternumRotX', 'SternumRotY', 'SternumRotZ', 'shoulder_elv_r', 'shoulder_rot_r', 'elv_angle_r', 'elbow_flexion_r', 'pro_sup_r', 'wrist_dev_r', 'wrist_flex_r', 'shoulder_elv_l', 'shoulder_rot_l', 'elv_angle_l', 'elbow_flexion_l', 'pro_sup_l', 'wrist_dev_l', 'wrist_flex_l']
coord_vals = [0,0,0,0,-0.01745,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
for i in range(len(coords)):
curr_coord = coord_set.get(coords[i])
curr_coord.setValue(state, coord_vals[i], False)
state = model.initSystem()
model.assemble(state)
analyze_tool.run()
Afterwards, I use these codes to display if the values are updated, but they do not.
Code: Select all
coord_set = model.getCoordinateSet()
print(coord_set.get(coords[4]).getName())
print(coord_set.get(coords[4]).getValue(state))