-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmot2quats.py
More file actions
289 lines (235 loc) · 12 KB
/
mot2quats.py
File metadata and controls
289 lines (235 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# For OpenSim support
from colorama import init as colorama_init
from colorama import Fore
from colorama import Style
import math, os, sys, getopt, numpy
import opensim as osim
import csv
import numpy as np
import quaternion
def saveOutputCSV(outputPath, times, outputNames, outputData):
file = open(outputPath, "w", newline='')
writer = csv.writer(file)
header = []
for i in range(len(times)):
row = []
if i == 0: # Write header
header.append("time")
for label in outputNames:
header.append(label)
writer.writerow(header)
row.append(str(times[i]))
for data in outputData[i]:
row.append(str(data))
writer.writerow(row)
file.close()
def saveMotionCSV(outputPath, bodyNames, times, poseTrajectories, rotationsOnly):
file = open(outputPath, "w", newline='')
writer = csv.writer(file)
header = []
lastBodyPoses = []
for i in range(len(times)):
row = [] # Row is a list of strings
if i == 0:
header.append("time")
row.append(str(times[i]))
bodyPoses = poseTrajectories[i]
for body in range(len(bodyPoses)):
if i == 0: # Add header info
lastBodyPoses.append(np.quaternion())
if rotationsOnly == False:
header.append(bodyNames[body] + "_x")
header.append(bodyNames[body] + "_y")
header.append(bodyNames[body] + "_z")
header.append(bodyNames[body] + "_qw")
header.append(bodyNames[body] + "_qx")
header.append(bodyNames[body] + "_qy")
header.append(bodyNames[body] + "_qz")
# Add position
if rotationsOnly == False:
row.append(str(bodyPoses[body][0][0]))
row.append(str(bodyPoses[body][0][1]))
row.append(str(bodyPoses[body][0][2]))
# Add rotation as quaternion
# Check Euclidean distance with previous timestep and select antipodal quaternion that minimizes the distance
# Better continuity of the orientation trajectory makes learning easier
lastQuat = lastBodyPoses[body]
currentQuat = bodyPoses[body][1]
testA = lastQuat.w
testW = currentQuat.w
negCurrentQuat = -currentQuat
distQ = math.sqrt(pow(currentQuat.w-lastQuat.w, 2.0) + pow(currentQuat.x-lastQuat.x, 2.0) + pow(currentQuat.y-lastQuat.y, 2.0) + pow(currentQuat.z-lastQuat.z, 2.0))
negDistQ = math.sqrt(pow(negCurrentQuat.w-lastQuat.w, 2.0) + pow(negCurrentQuat.x-lastQuat.x, 2.0) + pow(negCurrentQuat.y-lastQuat.y, 2.0) + pow(negCurrentQuat.z-lastQuat.z, 2.0))
chosenQuat = currentQuat
if negDistQ < distQ:
chosenQuat = negCurrentQuat
lastBodyPoses[body] = chosenQuat # Store the chosen quaternion this timestep to compare next frame
# Uncomment to test trajectory for a specific bone
# if bodyNames[body] == "ulna_r":
# print("Body ", bodyNames[body], " last: ", lastQuat, "choice1: ", currentQuat, " choice2: ", negCurrentQuat, " selected: ", chosenQuat)
row.append(str(chosenQuat.w))
row.append(str(chosenQuat.x))
row.append(str(chosenQuat.y))
row.append(str(chosenQuat.z))
if i == 0: # Write header before 1st timestamp data
writer.writerow(header)
writer.writerow(row)
file.close()
def mot2quats(motionPath, outputPath, jointParents, modelPath, muscleNames, optionsDict):
print(f"Input OpenSim motion path set to: {motionPath}")
print(f"Input OpenSim model path set to: {modelPath}")
model = osim.Model(modelPath)
model.initSystem()
motion = osim.Storage(motionPath)
activationStorage = None
if optionsDict["activationSTO"] == True:
stoPath = os.path.splitext(motionPath)[0] + "_StaticOptimization_activation.sto"
activationStorage = osim.Storage(stoPath)
else:
print("Activations are in .mot file.")
if motion.isInDegrees() == True:
if optionsDict["columnsInDegrees"]:
print("Convert given degree data columns to radians.")
for dof in optionsDict["columnsInDegrees"]:
print(f"Convert data {dof} to radians.")
dofIndex = motion.getStateIndex(dof)
motion.multiplyColumn(dofIndex, math.pi/180.0) # Convert to radians.
# Edit storage so angles are in radians.
motion.setInDegrees(False)
else:
print(f"{Fore.LIGHTYELLOW_EX}Warning: Motion set is in degrees. Provide columnsInDegrees list to convert. Radians are required.{Style.RESET_ALL}")
exit(-1)
else:
print("Data set is in radians already. No need to convert.")
motionName = os.path.splitext(os.path.basename(motionPath))[0]
print (f"Motion name: {motionName}")
# Results states in motionTrajectory are in SimTK:Stage:Instance
motionTrajectory = osim.StatesTrajectory.createFromStatesStorage(model, motion, True, True)
print("Trajectory size: ", motionTrajectory.getSize(), " is compatible: ", motionTrajectory.isCompatibleWith(model))
motionState = motionTrajectory.get(0)
model.realizePosition(motionState) # Need to do this to query positions.
jointList = model.getJointList() # Get the Model's BodyList
jointIter = jointList.begin() # Start the iterator at the beginning of the list
bodyList = model.getBodyList()
bodyIter = bodyList.begin()
assert(model.getNumBodies() == model.getNumJoints())
workingBodyDict = dict()
bodyNames= []
workingBodyList = []
while jointIter != jointList.end(): # Stay in the loop until the iterator reaches the end of the list
workingBodyList.append((bodyIter.getName(), bodyIter.deref(), jointIter.deref()))
workingBodyDict[bodyIter.getName()] = (bodyIter.deref(), jointIter.deref())
bodyNames.append(bodyIter.getName())
jointIter.next()
bodyIter.next()
poseTrajectories = []
times = []
print("Saving out motion frames...")
for i in range(motionTrajectory.getSize()):
motionState = motionTrajectory.get(i)
model.realizePosition(motionState)
times.append(motionState.getTime())
# Loop through bodies in model.
bodyPoses = []
for (name, body, joint) in workingBodyList:
positionGround = body.getPositionInGround(motionState)
rotationGround = body.getRotationInGround(motionState)
# Convert the OpenSim quaternion to the numpy quaternion which has more useful attributes.
localPosition = positionGround
bodyQuat = rotationGround.convertRotationToQuaternion()
#print("Body: ", name, "Pos: ", positionGround, "Rot: ", bodyQuat)
localRotation = np.quaternion(bodyQuat.get(0), bodyQuat.get(1), bodyQuat.get(2), bodyQuat.get(3))
if "motionFormat" in optionsDict:
if optionsDict["motionFormat"] == "localRotationsOnly":
# Find parent of body
parentName = jointParents[name]
if parentName != "ground":
(parentBody, parentJoint) = workingBodyDict[jointParents[name]]
parentRotationGround = parentBody.getRotationInGround(motionState)
parentPosition = parentBody.getPositionInGround(motionState)
parentQuat = parentRotationGround.convertRotationToQuaternion()
parentRotation = np.quaternion(parentQuat.get(0), parentQuat.get(1), parentQuat.get(2), parentQuat.get(3))
localRotation = parentRotation.conjugate() * localRotation
localPosition[0] = positionGround[0] - parentPosition[0]
localPosition[1] = positionGround[1] - parentPosition[1]
localPosition[2] = positionGround[2] - parentPosition[2]
else:
# TODO: We have not really tested the output positions. Try offer a transform wrt root output mode for usd animations.
print(f"{Fore.LIGHTRED_EX}Error: Motion format is not recognized..{Style.RESET_ALL}")
quit(-1)
else:
print(f"{Fore.LIGHTRED_EX}Error: No valid option localRotations for motion file set..{Style.RESET_ALL}")
quit(-1)
if math.isnan(localRotation.w) or math.isnan(localRotation.x) or math.isnan(localRotation.y) or math.isnan(localRotation.z):
localRotation = np.quaternion(1.0, 0.0, 0.0, 0.0) # Make identity
bodyPoses.append((localPosition, localRotation))
poseTrajectories.append(bodyPoses)
if outputPath != None:
saveMotionCSV(optionsDict["outputFolder"] + "/" + motionName + "_motion.csv", bodyNames, times, poseTrajectories, True)
outputNames = []
for force in model.getForceSet():
outputNames.append(force.getName())
outputData = []
# Filter out activations only.
names = model.getStateVariableNames()
activationLabels = []
for j in range(names.getSize()):
muscleName = names.get(j)
if "/activation" in muscleName:
activationLabels.append(muscleName)
elif muscleName in muscleNames:
activationLabels.append(muscleName)
print(f"Found {len(activationLabels)} activation state variables for output.")
outputNames = []
labelDict = dict()
for i in range(len(times)):
sample = []
state = motionTrajectory.get(i)
model.realizeVelocity(state)
for label in activationLabels:
if i == 0:
muscleLabel = label
if "/activation" in label:
muscleLabel = label.replace("/activation","")
muscleLabel = muscleLabel.replace("/forceset/","")
labelDict[label] = muscleLabel
outputNames.append(muscleLabel)
activation = 0.0
if activationStorage:
activationStates = activationStorage.getStateVector(i)
activationVector = activationStates.getData()
keyLabel = labelDict[label]
stateIndex = activationStorage.getStateIndex(keyLabel)
activation = activationVector.get(stateIndex)
else:
activation = model.getStateVariableValue(state, label)
sample.append(str(activation))
outputData.append(sample)
saveOutputCSV(optionsDict["outputFolder"] + "/" + motionName + "_output.csv", times, outputNames, outputData)
return motionName, (bodyNames, times, poseTrajectories)
def main(argv):
print(f"OpenSim version: {osim.GetVersionAndDate()}")
sessionPath=""
modelPath = "./Model/LaiArnoldModified2017_poly_withArms_weldHand_scaled_adjusted.osim"
motionPath = "./Motions/kinematics_activations_left_leg_squat_0.mot"
inputPath = sessionPath + motionPath
outputPath = os.path.splitext(inputPath)[0]
muscleNames = {} # Empty set
optionsDict = dict()
optionsDict["localRotations"] = True
opts, args = getopt.getopt(argv,"im:o:",["input=","model=","output="])
for opt, arg in opts:
if opt == "-h":
print("mot2quats.py -i <inputFile> -o <outputFile> -m <modelFile>")
sys.exit()
elif opt in ("-i", "--input"):
inputPath = arg
elif opt in ("-o", "--output"):
outputPath = arg
elif opt in ("-m", "--model"):
modelPath = arg
(name, bodyPoseTrajectories) = mot2quats(inputPath, outputPath, None, modelPath, muscleNames, optionsDict)
# Checks if running this file from a script vs. a module. Useful if planning to use this file also as a module
# to incorporate into other scripts.
if __name__ == "__main__":
main(sys.argv[1:])