I am currently trying to find a way using the OpenSim and python, to calculate the x,y,z position of the right and of a given model and motion. The motion data I got by using the IK tool. The code I came up with so fare give me the position and orientation of the right hand, but it doesnot change overtime, so im wondering what goes wrong. Since, simulating the motion in open sim clearly shows a movement.
The code:
Code: Select all
model = osim.Model(model_file)
motion = osim.Storage(motion_file)
# Set up ForwardTool and run simulation
dt = motion.getMinTimeStep()
final_time = motion.getLastTime()
tool = osim.ForwardTool()
tool.setModel(model)
tool.setControlsFileName(motion_file)
tool.setResultsDir("path/to/results/")
tool.setInitialTime(0)
tool.setFinalTime(final_time)
tool.setMinDT(dt)
tool.run()
# Get the hand bodypart by name
hand = model.getBodySet().get("hand_r")
# Create a Vec3 object to specify the point on the body
point = hand.getPositionInGround(state)
print(point)
# Iterate over the frames of the motion data
for i in range(motion.getSize()):
state = model.initSystem()
# Set the model's state to match the motion frame
time = motion.getStateVector(i).getTime()
state.setTime(time)
# Get the global position of the wrist joint
hand_pos = osim.Vec3() # Create an empty Vec3 object to store the result
model.getSimbodyEngine().getPosition(state, hand, point, hand_pos)
model.getFrameList
# Extract the x, y, and z coordinates
x_coord = hand_pos.get(0)
y_coord = hand_pos.get(1)
z_coord = hand_pos.get(2)
# Print or store the coordinates
print("Frame {}: Time={}, Hand_r Coordinates: X={}, Y={}, Z={}".format(i, time, x_coord, y_coord, z_coord))
Code: Select all
for t in time_steps:
#time = motion.getStateVector(t).getTime()
state.setTime(t)
# Get the body transform for "hand_r" in the updated state
hand_r = model.getBodySet().get("hand_r")
body_transform = hand_r.getTransformInGround(state)
# Get the position and orientation
position = body_transform.p()
orientation = body_transform.R().convertRotationToQuaternion()
print("Time:", t)
print("Position:", position)
print("Orientation:", orientation)
print()
I hope anyone could help.
best regards,
joy