-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathO2MConverter.py
1513 lines (1182 loc) · 69.2 KB
/
O2MConverter.py
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import vtk
import sys
import os
from scipy.interpolate import interp1d
import math
import copy
import xmltodict
import admesh
from pyquaternion import Quaternion
from shutil import copyfile
from natsort import natsorted, ns
from operator import itemgetter
from sklearn.metrics import r2_score
from collections import OrderedDict
from scipy.optimize import minimize
import Utils
class Converter:
"""A class to convert OpenSim XML model files to MuJoCo XML model files"""
def __init__(self):
# Define input XML and output folder
self.input_xml = None
self.output_folder = None
# List of constraints
self.constraints = None
# Parse bodies, joints, muscles
self.bodies = dict()
self.joints = dict()
self.muscles = []
# We need to keep track of coordinates in joints' CoordinateSet, we might need to use them for setting up
# equality constraints
self.coordinates = dict()
# These dictionaries (or list of dicts) are in MuJoCo style (when converted to XML)
self.asset = dict()
self.tendon = []
self.actuator = {"general": [], "muscle": []}
self.equality = {"joint": [], "weld": []}
# Use mesh files if they are given
self.geometry_folder = None
self.output_geometry_folder = "Geometry/"
self.vtk_reader = vtk.vtkXMLPolyDataReader()
self.stl_writer = vtk.vtkSTLWriter()
# Setup writer
self.stl_writer.SetInputConnection(self.vtk_reader.GetOutputPort())
self.stl_writer.SetFileTypeToBinary()
# The root of the kinematic tree
self.origin_body = None
self.origin_joint = None
def reset(self):
self.constraints = None
self.bodies = dict()
self.joints = dict()
self.muscles = []
self.coordinates = dict()
self.asset = dict()
self.tendon = []
self.actuator = {"general": [], "muscle": []}
self.equality = {"joint": [], "weld": []}
self.origin_body = None
self.origin_joint = None
def convert(self, input_xml, output_folder, geometry_folder=None, for_testing=False):
"""Convert given OpenSim XML model to MuJoCo XML model"""
# Reset all variables
self.reset()
# Save input and output XML files in case we need them somewhere
self.input_xml = input_xml
# Set geometry folder
self.geometry_folder = geometry_folder
# Read input_xml and parse it
with open(input_xml) as f:
text = f.read()
p = xmltodict.parse(text)
# Set output folder
model_name = os.path.split(input_xml)[1][:-5] + "_converted"
self.output_folder = output_folder + "/" + model_name + "/"
# Create the output folder
os.makedirs(self.output_folder, exist_ok=True)
# Find and parse constraints
if "ConstraintSet" in p["OpenSimDocument"]["Model"] and p["OpenSimDocument"]["Model"]["ConstraintSet"]["objects"] is not None:
self.parse_constraints(p["OpenSimDocument"]["Model"]["ConstraintSet"]["objects"])
# Find and parse bodies and joints
if "BodySet" in p["OpenSimDocument"]["Model"]:
self.parse_bodies_and_joints(p["OpenSimDocument"]["Model"]["BodySet"]["objects"])
# Find and parse muscles, and CoordinateLimitForces
if "ForceSet" in p["OpenSimDocument"]["Model"]:
self.parse_muscles_and_tendons(p["OpenSimDocument"]["Model"]["ForceSet"]["objects"])
if "CoordinateLimitForce" in p["OpenSimDocument"]["Model"]["ForceSet"]["objects"]:
self.parse_coordinate_limit_forces(p["OpenSimDocument"]["Model"]["ForceSet"]["objects"]["CoordinateLimitForce"])
# If we're building this model for testing we need to unclamp all joints
if for_testing:
self.unclamp_all_mujoco_joints()
# Now we need to re-assemble all of the above in MuJoCo format
# (or actually a dict version of the model so we can use xmltodict to save the model into a XML file)
mujoco_model = self.build_mujoco_model(p["OpenSimDocument"]["Model"]["@name"])
# Add a camera for testing
mujoco_model["mujoco"]["worldbody"]["camera"] = {"@name": "for_testing", "@pos": "0 0 0", "@euler": "0 0 0"}
if for_testing:
mujoco_model["mujoco"]["option"]["@collision"] = "predefined"
del mujoco_model["mujoco"]["worldbody"]["geom"]
# Finally, save the MuJoCo model into XML file
output_xml = self.output_folder + model_name + ".xml"
with open(output_xml, 'w') as f:
f.write(xmltodict.unparse(mujoco_model, pretty=True, indent=" "))
# We might need to fix stl files (if converted from OpenSim Geometry vtk files)
if self.geometry_folder is not None:
self.fix_stl_files()
def parse_constraints(self, p):
# Go through all (possibly different kinds of) constraints
for constraint_type in p:
# Make sure we're dealing with a list
if isinstance(p[constraint_type], dict):
p[constraint_type] = [p[constraint_type]]
# Go through all constraints
for constraint in p[constraint_type]:
if "SimmSpline" in constraint["coupled_coordinates_function"] or \
"NaturalCubicSpline" in constraint["coupled_coordinates_function"]:
if "SimmSpline" in constraint["coupled_coordinates_function"]:
spline_type = "SimmSpline"
else:
spline_type = "NaturalCubicSpline"
# Get x and y values that define the spline
x_values = constraint["coupled_coordinates_function"][spline_type]["x"]
y_values = constraint["coupled_coordinates_function"][spline_type]["y"]
# Convert into numpy arrays
x_values = np.array(x_values.split(), dtype=float)
y_values = np.array(y_values.split(), dtype=float)
assert len(x_values) > 1 and len(y_values) > 1, "Not enough points, can't fit a spline"
# Fit a linear / quadratic / cubic / quartic function
fit = np.polynomial.polynomial.Polynomial.fit(x_values, y_values, min(4, len(x_values)-1))
# A simple check to see if the fit is alright
y_fit = fit(x_values)
assert r2_score(y_values, y_fit) > 0.5, "A bad approximation of the SimmSpline"
# Get the polynomial function's weights
polycoef = np.zeros((5,))
polycoef[:fit.coef.shape[0]] = fit.convert().coef
elif "LinearFunction" in constraint["coupled_coordinates_function"]:
# Get coefficients of the linear function
coefs = np.array(constraint["coupled_coordinates_function"]["LinearFunction"]["coefficients"].split(), dtype=float)
# Make a quartic representation of the linear function
polycoef = np.zeros((5,))
polycoef[0] = coefs[1]
polycoef[1] = coefs[0]
# Dummy linear fit function
fit = np.polynomial.polynomial.Polynomial.fit([0, 1], [0, 1], 1)
else:
raise NotImplementedError
# Create a constraint; note that 'fit' needs to be removed since it's not a valid mujoco keyword/string
self.equality["joint"].append({
"@name": constraint["@name"],
"@joint1": constraint["dependent_coordinate_name"],
"@joint2": constraint["independent_coordinate_names"],
"@active": "true" if constraint["isDisabled"] == "false" else "false",
"@polycoef": Utils.array_to_string(polycoef),
"@solimp": "0.9999 0.9999 0.001 0.5 2",
"fit": fit})
def parse_bodies_and_joints(self, p):
# Go through all bodies and their joints
for obj in p["Body"]:
b = Body(obj)
j = Joint(obj, self.equality)
# Add b to bodies
self.bodies[b.name] = b
# Get coordinates, we might need them for setting up equality constraints
self.coordinates = {**self.coordinates, **j.get_coordinates()}
# Ignore joint if it is None
if j.parent_body is None:
continue
# Add joint equality constraints
self.equality["joint"].extend(j.get_equality_constraints("joint"))
self.equality["weld"].extend(j.get_equality_constraints("weld"))
# There might be multiple joints per body
if j.parent_body not in self.joints:
self.joints[j.parent_body] = []
self.joints[j.parent_body].append(j)
def parse_muscles_and_tendons(self, p):
# Go through all muscle types (typically there are only one type of muscle)
for muscle_type in p:
# Skip some forces
if muscle_type == "CoordinateLimitForce":
# We'll handle these later
continue
elif muscle_type not in \
["Millard2012EquilibriumMuscle", "Thelen2003Muscle",
"Schutte1993Muscle_Deprecated", "CoordinateActuator", "Millard2012AccelerationMuscle"]:
print("Skipping a force: {}".format(muscle_type))
continue
# Make sure we're dealing with a list
if isinstance(p[muscle_type], dict):
p[muscle_type] = [p[muscle_type]]
# Go through all muscles
for muscle in p[muscle_type]:
m = Muscle(muscle, muscle_type)
self.muscles.append(m)
# Check if the muscle is disabled
if m.is_disabled():
continue
elif m.is_muscle:
self.actuator["muscle"].append(m.get_actuator())
self.tendon.append(m.get_tendon())
# Add sites to all bodies this muscle/tendon spans
for body_name in m.path_point_set:
self.bodies[body_name].add_sites(m.path_point_set[body_name])
else:
self.actuator["general"].append(m.get_actuator())
def parse_coordinate_limit_forces(self, forces):
# These parameters might be incorrect, but we'll optimize them later
# Go through each force and set corresponding joint parameters
for force in forces:
# Ignore disabled forces
if force["isDisabled"].lower() == "true":
continue
# Get joint name
joint_name = force["coordinate"]
# We need to search for this joint
target = None
for body in self.joints:
for joint in self.joints[body]:
for mujoco_joint in joint.mujoco_joints:
if mujoco_joint["name"] == joint_name:
target = mujoco_joint
# Check if joint was found
assert target is not None, "Cannot set CoordinateLimitForce params, couldn't find the joint"
# TODO for now let's ignore these forces -- they are too difficult to implement and optimize
# Let's just switch the joint limit on if it's defined; mark this so it won't be unclamped later
if "range" in target and target["range"][0] != target["range"][1]:
target["limited"] = True
target["user"] = 1
continue
# Take the average of stiffness
stiffness = 0.5*(float(force["upper_stiffness"]) + float(force["lower_stiffness"]))
# Stiffness / damping may be defined in two separate forces; we assume that we're dealing with damping
# if average stiffness is close to zero
if stiffness < 1e-4:
# Check if rotational stiffness
damping = float(force["damping"])
if target["motion_type"] == "rotational":
damping *= math.pi/180
# Set damping
target["damping"] = damping
else:
# We need to create a soft joint coordinate limit, but we can't use separate limits like in OpenSim;
# this is something we'll need to approximate
# Limits in CoordinateLimitForce should be in degrees
force_coordinate_limits = np.array([float(force["lower_limit"]), float(force["upper_limit"])]) * math.pi/180
# Check if there are hard limits defined for this joint
if target["limited"]:
# Range should be given if joint is limited; use range to calculate width param of solimp
range = target.get("range")
width = np.array([force_coordinate_limits[0] - range[0], range[1] - force_coordinate_limits[1]])
# If either width is > 0 create a soft limit
pos_idx = width > 0
if np.any(pos_idx):
# Mark this joint for optimization
target["user"] = 1
# Define the soft limit
target["solimplimit"] = [0.0001, 0.99, np.mean(width[pos_idx])]
else:
# Use force_coordinate_limits as range
# Calculate width with the original range if it was defined
width = 0.001
if "range" in target:
width_range = np.array([force_coordinate_limits[0] - target["range"][0],
target["range"][1] - force_coordinate_limits[1]])
pos_idx = width_range > 0
if np.any(pos_idx):
width = np.mean(width_range[pos_idx])
# Mark this joint for optimization
target["user"] = 1
# Define the soft limit
target["limited"] = True
target["solimplimit"] = [0.0001, 0.99, width, 0.5, 1]
def build_mujoco_model(self, model_name):
# Initialise model
model = {"mujoco": {"@model": model_name}}
# Set defaults
# Note: balanceinertia is set to true, and boundmass and boundinertia are > 0 to ignore poorly designed models
# (that contain incorrect inertial properties or massless moving bodies)
model["mujoco"]["compiler"] = {"@inertiafromgeom": "auto", "@angle": "radian", "@balanceinertia": "true",
"@boundmass": "0.001", "@boundinertia": "0.001"}
model["mujoco"]["compiler"]["lengthrange"] = {"@inttotal": "50", "@useexisting": "true"}
model["mujoco"]["default"] = {
"joint": {"@limited": "true", "@damping": "0.5", "@armature": "0.1", "@stiffness": "2"},
"geom": {"@rgba": "0.8 0.6 .4 1", "@margin": "0.001"},
"site": {"@size": "0.001"},
"tendon": {"@width": "0.001", "@rgba": ".95 .3 .3 1", "@limited": "false"}}
model["mujoco"]["default"]["default"] = [
{"@class": "muscle", "muscle": {"@ctrllimited": "true", "@ctrlrange": "0 1", "@scale": "200"}},
{"@class": "motor", "general": {"@gainprm": "5 0 0 0 0 0 0 0 0 0"}}
]
model["mujoco"]["option"] = {"@timestep": "0.002"}
model["mujoco"]["size"] = {"@njmax": "1000", "@nconmax": "400", "@nuser_jnt": 1}
# Start building the worldbody
worldbody = {"geom": {"@name": "floor", "@pos": "0 0 0", "@size": "10 10 0.125",
"@type": "plane", "@material": "MatPlane", "@condim": "3"}}
# We should probably find the "origin" body, where the kinematic chain begins
self.origin_body, self.origin_joint = self.find_origin()
# Rotate self.origin_joint.orientation_in_parent so the model is upright
# Rotation is done along an axis that goes through (0,0,0) coordinate
T_origin_joint = Utils.create_transformation_matrix(
self.origin_joint.location_in_parent,
quat=self.origin_joint.orientation_in_parent)
T_rotation = Utils.create_rotation_matrix(axis=[1, 0, 0], rad=math.pi/2)
self.origin_joint.set_transformation_matrix(np.matmul(T_rotation, T_origin_joint))
# Add sites to worldbody / "ground" in OpenSim
worldbody["site"] = self.bodies[self.origin_joint.parent_body].sites
# Add some more defaults
worldbody["body"] = {"light":
{"@mode": "trackcom", "@directional": "false", "@pos": "0 0 4.0", "@dir": "0 0 -1"}}
# Build the kinematic chains
worldbody["body"] = self.add_body(worldbody["body"], self.origin_body,
self.joints[self.origin_body.name])
# Add worldbody to the model
model["mujoco"]["worldbody"] = worldbody
# We might want to use a weld constraint to fix the origin body to worldbody for experiments
self.equality["weld"].append({"@name": "origin_to_worldbody",
"@body1": self.origin_body.name, "@active": "false"})
# Set some asset defaults
self.asset["texture"] = [
{"@name": "texplane", "@type": "2d", "@builtin": "checker", "@rgb1": ".2 .3 .4",
"@rgb2": ".1 0.15 0.2", "@width": "100", "@height": "100"}]
self.asset["material"] = [
{"@name": "MatPlane", "@reflectance": "0.0", "@texture": "texplane",
"@texrepeat": "1 1", "@texuniform": "true"}]
# Add assets to model
model["mujoco"]["asset"] = self.asset
# Add tendons and actuators
model["mujoco"]["tendon"] = {"spatial": self.tendon}
model["mujoco"]["actuator"] = self.actuator
# Add equality constraints between joints; note that we may need to remove some equality constraints
# that were set in ConstraintSet but then overwritten or not used. While we're iterating through constraints
# we also need to remove the "fit" keyword which is not a mujoco keyword/string
remove_idxs = []
for idx, constraint in enumerate(self.equality["joint"]):
constraint_found = False
if "fit" in constraint:
del constraint["fit"]
for parent_body in self.joints:
for joint in self.joints[parent_body]:
for mujoco_joint in joint.mujoco_joints:
if mujoco_joint["name"] == constraint["@joint1"]:
constraint_found = True
if not constraint_found:
remove_idxs.append(idx)
#self.equality["joint"].remove(constraint)
# Remove constraints that aren't used
for idx in sorted(remove_idxs, reverse=True):
del self.equality["joint"][idx]
# Add equality constraints into the model
model["mujoco"]["equality"] = self.equality
return model
def unclamp_all_mujoco_joints(self):
# Unclamp (set limited=false) all joints except those that have limites that need to be optimized
for joint_name in self.joints:
for j in self.joints[joint_name]:
for mujoco_joint in j.mujoco_joints:
# Don't unclamp dependent joints or joints that have limits that need to be optimized
if mujoco_joint["motion_type"] not in ["dependent", "coupled"] \
and not ("user" in mujoco_joint and mujoco_joint["user"] == 1):
mujoco_joint["limited"] = False
def add_body(self, worldbody, current_body, current_joints):
# Create a new MuJoCo body
worldbody["@name"] = current_body.name
# We need to find this body's position relative to parent body:
# since we're progressing down the kinematic chain, each body
# should have a joint to parent body
joint_to_parent = self.find_joint_to_parent(current_body.name)
# Update location and orientation of child body
T = Utils.create_transformation_matrix(joint_to_parent.location, quat=joint_to_parent.orientation)
joint_to_parent.set_transformation_matrix(
np.matmul(joint_to_parent.get_transformation_matrix(), np.linalg.inv(T)))
# Define position and orientation
worldbody["@pos"] = Utils.array_to_string(joint_to_parent.location_in_parent)
worldbody["@quat"] = "{} {} {} {}"\
.format(joint_to_parent.orientation_in_parent.w,
joint_to_parent.orientation_in_parent.x,
joint_to_parent.orientation_in_parent.y,
joint_to_parent.orientation_in_parent.z)
# Add geom
worldbody["geom"] = self.add_geom(current_body)
# Add inertial properties -- only if mass is greater than zero and eigenvalues are positive
# (if "inertial" is missing MuJoCo will infer the inertial properties from geom)
if current_body.mass > 0:
values, vectors = np.linalg.eig(Utils.create_symmetric_matrix(current_body.inertia))
if np.all(values > 0):
worldbody["inertial"] = {"@pos": Utils.array_to_string(current_body.mass_center),
"@mass": str(current_body.mass),
"@fullinertia": Utils.array_to_string(current_body.inertia)}
# Add sites
worldbody["site"] = current_body.sites
# Go through joints
worldbody["joint"] = []
for mujoco_joint in joint_to_parent.mujoco_joints:
# Define the joint
j = {"@name": mujoco_joint["name"], "@type": mujoco_joint["type"], "@pos": "0 0 0",
"@axis": Utils.array_to_string(mujoco_joint["axis"])}
if "limited" in mujoco_joint:
j["@limited"] = "true" if mujoco_joint["limited"] else "false"
if "range" in mujoco_joint:
j["@range"] = Utils.array_to_string(mujoco_joint["range"])
if "ref" in mujoco_joint:
j["@ref"] = str(mujoco_joint["ref"])
if "springref" in mujoco_joint:
j["@springref"] = str(mujoco_joint["springref"])
if "stiffness" in mujoco_joint:
j["@stiffness"] = str(mujoco_joint["stiffness"])
if "damping" in mujoco_joint:
j["@damping"] = str(mujoco_joint["damping"])
if "solimplimit" in mujoco_joint:
j["@solimplimit"] = Utils.array_to_string(mujoco_joint["solimplimit"])
if "user" in mujoco_joint:
j["@user"] = str(mujoco_joint["user"])
# If the joint is between origin body and it's parent, which should be "ground", set
# damping, armature, and stiffness to zero
if joint_to_parent is self.origin_joint:
j.update({"@armature": 0, "@damping": 0, "@stiffness": 0})
# Add to joints
worldbody["joint"].append(j)
# And we're done if there are no joints
if current_joints is None:
return worldbody
worldbody["body"] = []
for j in current_joints:
worldbody["body"].append(self.add_body(
{}, self.bodies[j.child_body],
self.joints.get(j.child_body, None)
))
return worldbody
def add_geom(self, body):
# Collect all geoms here
geom = []
if self.geometry_folder is None:
# By default use a capsule
# Try to figure out capsule size by mass or something
size = np.array([0.01, 0.01])*np.sqrt(body.mass)
geom.append({"@name": body.name, "@type": "capsule",
"@size": Utils.array_to_string(size)})
else:
# Make sure output geometry folder exists
os.makedirs(self.output_folder + self.output_geometry_folder, exist_ok=True)
# Grab the mesh from given geometry folder
for m in body.mesh:
# Get file path
geom_file = self.geometry_folder + "/" + m["geometry_file"]
# Check the file exists
assert os.path.exists(geom_file) and os.path.isfile(geom_file), "Mesh file {} doesn't exist".format(geom_file)
# Transform vtk into stl or just copy stl file
mesh_name = m["geometry_file"][:-4]
stl_file = self.output_geometry_folder + mesh_name + ".stl"
# Transform a vtk file into an stl file and save it
if geom_file[-3:] == "vtp":
self.vtk_reader.SetFileName(geom_file)
self.stl_writer.SetFileName(self.output_folder + stl_file)
self.stl_writer.Write()
# Just copy stl file
elif geom_file[-3:] == "stl":
copyfile(geom_file, self.output_folder + stl_file)
else:
raise NotImplementedError("Geom file is not vtk or stl!")
# Add mesh to asset
self.add_mesh_to_asset(mesh_name, stl_file, m)
# Create the geom
geom.append({"@name": mesh_name, "@type": "mesh", "@mesh": mesh_name})
return geom
def add_mesh_to_asset(self, mesh_name, mesh_file, mesh):
if "mesh" not in self.asset:
self.asset["mesh"] = []
self.asset["mesh"].append({"@name": mesh_name,
"@file": mesh_file,
"@scale": mesh["scale_factors"]})
def find_origin(self):
# Start from a random joint and work your way backwards until you find
# the origin body (the body that represents ground)
# Make sure there's at least one joint
assert len(self.joints) > 0, "There are no joints!"
# Choose a joint, doesn't matter which one
current_joint = next(iter(self.joints.values()))[0]
# Follow the kinematic chain
while True:
# Move up in the kinematic chain as far as possible
new_joint_found = False
for parent_body in self.joints:
for j in self.joints[parent_body]:
if j.child_body == current_joint.parent_body:
current_joint = j
new_joint_found = True
break
# No further joints, child of current joint is the origin body
if not new_joint_found:
return self.bodies[current_joint.child_body], current_joint
def find_joint_to_parent(self, body_name):
joint_to_parent = None
for parent_body in self.joints:
for j in self.joints[parent_body]:
if j.child_body == body_name:
joint_to_parent = j
# If there are multiple child bodies with the same name, the last
# one is returned
if joint_to_parent is not None:
break
assert joint_to_parent is not None, "Couldn't find joint to parent body for body {}".format(body_name)
return joint_to_parent
def fix_stl_files(self):
# Loop through geometry folder and fix stl files
for mesh_file in os.listdir(self.output_folder + self.output_geometry_folder):
if mesh_file.endswith(".stl"):
mesh_file = self.output_folder + self.output_geometry_folder + mesh_file
stl = admesh.Stl(mesh_file)
stl.remove_unconnected_facets()
stl.write_binary(mesh_file)
class Joint:
def __init__(self, obj, constraints):
joint = obj["Joint"]
self.parent_body = None
self.coordinates = dict()
# 'ground' body does not have joints
if joint is None or len(joint) == 0:
return
# This code assumes there's max one joint per object
assert len(joint) == 1, 'TODO Multiple joints for one body'
# We need to figure out what kind of joint this is
self.joint_type = list(joint)[0]
# Step into the actual joint information
joint = joint[self.joint_type]
# Get names of bodies this joint connects
self.parent_body = joint["parent_body"]
self.child_body = obj["@name"]
# And other parameters
self.location_in_parent = np.array(joint["location_in_parent"].split(), dtype=float)
self.location = np.array(joint["location"].split(), dtype=float)
orientation = np.array(joint["orientation"].split(), dtype=float)
x = Quaternion(axis=[1, 0, 0], radians=orientation[0]).rotation_matrix
y = Quaternion(axis=[0, 1, 0], radians=orientation[1]).rotation_matrix
z = Quaternion(axis=[0, 0, 1], radians=orientation[2]).rotation_matrix
self.orientation = Quaternion(matrix=np.matmul(np.matmul(x, y), z))
# Calculate orientation in parent
orientation_in_parent = np.array(joint["orientation_in_parent"].split(), dtype=float)
x = Quaternion(axis=[1, 0, 0], radians=orientation_in_parent[0]).rotation_matrix
y = Quaternion(axis=[0, 1, 0], radians=orientation_in_parent[1]).rotation_matrix
z = Quaternion(axis=[0, 0, 1], radians=orientation_in_parent[2]).rotation_matrix
self.orientation_in_parent = Quaternion(matrix=np.matmul(np.matmul(x, y), z))
# Not sure if we should update child body location and orientation before or after parsing joints;
# at the moment we're doing it after
#T = Utils.create_transformation_matrix(self.location, quat=self.orientation)
#self.set_transformation_matrix(
# np.matmul(self.get_transformation_matrix(), np.linalg.inv(T)))
# Some joint values are dependent on other joint values; we need to create equality constraints between those
# Also we might need to use weld constraints on locked joints
self.equality_constraints = {"joint": [], "weld": []}
# CustomJoint can represent any joint, we need to figure out
# what kind of joint we're dealing with
self.mujoco_joints = []
if self.joint_type == "CustomJoint":
T_joint = self.parse_custom_joint(joint, constraints)
# Update joint location and orientation
T = self.get_transformation_matrix()
T = np.matmul(T, T_joint)
self.set_transformation_matrix(T)
elif self.joint_type == "WeldJoint":
# Don't add anything to self.mujoco_joints, bodies are by default
# attached rigidly to each other in MuJoCo
pass
elif self.joint_type == "PinJoint":
self.parse_pin_joint(joint)
elif self.joint_type == "UniversalJoint":
self.parse_universal_joint(joint)
else:
raise NotImplementedError
def get_transformation_matrix(self):
T = self.orientation_in_parent.transformation_matrix
T[:3, 3] = self.location_in_parent
return T
def set_transformation_matrix(self, T):
self.orientation_in_parent = Quaternion(matrix=T)
self.location_in_parent = T[:3, 3]
def parse_custom_joint(self, joint, constraints):
# A CustomJoint in OpenSim model can represent any type of joint.
# Try to parse the CustomJoint into a set of MuJoCo joints
# Get transform axes
transform_axes = joint["SpatialTransform"]["TransformAxis"]
# We might need to create a homogeneous transformation matrix from
# location_in_parent to actual joint location
T = np.eye(4, 4)
#T = self.orientation_in_parent.transformation_matrix
# Start by parsing the CoordinateSet
coordinate_set = self.parse_coordinate_set(joint)
# NOTE! Coordinates in CoordinateSet parameterize this joint. In theory all six DoFs could be dependent
# on one Coordinate. Here we assume that only one DoF is equivalent to a Coordinate, that is, there exists an
# identity mapping between a Coordinate and a DoF, which is different to OpenSim where there might be no
# identity mappings. In OpenSim a Coordinate is just a value and all DoFs might have some kind of mapping with
# it, see e.g. "flexion" Coordinate in MoBL_ARMS_module6_7_CMC.osim model. MuJoCo doesn't have such abstract
# notion of a "Coordinate", and thus there cannot be a non-identity mapping from a joint to itself
# Go through axes; there's something wrong with the order of transformations, this is the order
# that works for leg6dof9musc.osim and MoBL_ARMS_module6_7_CMC.osim models, but it's so weird
# it's likely to be incorrect
transforms = ["rotation1", "rotation2", "rotation3", "translation1", "translation2", "translation3"]
order = [5, 4, 3, 0, 1, 2]
#order = [0, 1, 2, 3, 4, 5]
dof_designated = []
for idx in order:
t = transform_axes[idx]
if t["@name"] != transforms[idx]:
raise IndexError("Joints are parsed in incorrect order")
# Use the Coordinate parameters we parsed earlier; note that these do not exist for all joints (e.g
# constant joints)
if t.get("coordinates", None) in coordinate_set:
params = copy.deepcopy(coordinate_set[t["coordinates"]])
else:
params = {"name": "{}_{}".format(joint["@name"], t["@name"]), "limited": False,
"transform_value": 0, "coordinates": "unspecified"}
params["original_name"] = params["name"]
# Set default reference position/angle to zero. If this value is not zero, then you need
# more care while calculating quartic functions for equality constraints
params["ref"] = 0
# By default add this joint to MuJoCo model
params["add_to_mujoco_joints"] = True
# See the comment before this loop. We have to designate one DoF per Coordinate as an independent variable,
# i.e. make its dependence linear
if "coordinates" in t and t["coordinates"] == params["name"] \
and t["@name"].startswith(params["motion_type"][:8]) and not params["name"] in dof_designated:
# This is not necessary if the coordinate is dependent on another coordinate... starting to get
# complicated
ignore = False
if "joint" in constraints:
for c in constraints["joint"]:
if params["name"] == c["@joint1"]:
ignore = True
break
if not ignore:
# Check if we need to modify limits, TODO not sure if this is correct or needed
if Utils.is_nested_field(t, "SimmSpline", ["function"]):
# Fit a line/spline and check limit values within that fit
x_values = np.array(t["function"]["SimmSpline"]["x"].split(), dtype=float)
y_values = np.array(t["function"]["SimmSpline"]["y"].split(), dtype=float)
assert len(x_values) > 1 and len(y_values) > 1, "Not enough points, can't fit a polynomial"
fit = np.polynomial.polynomial.Polynomial.fit(x_values, y_values, min(4, len(x_values) - 1))
y_fit = fit(x_values)
assert r2_score(y_values, y_fit) > 0.5, "A bad approximation of the SimmSpline"
# Update range as min/max of the approximated range
if "range" in params:
params["range"] = fit(params["range"])
else:
params["range"] = np.array([min(y_fit), max(y_fit)])
# Make this into an identity mapping
t["function"] = dict({"LinearFunction": {"coefficients": '1 0'}})
elif Utils.is_nested_field(t, "LinearFunction", ["function"]):
coefficients = np.array(t["function"]["LinearFunction"]["coefficients"].split(), dtype=float)
assert abs(coefficients[0]) == 1 and coefficients[1] == 0, "Should we modify limits?"
x_values = np.array([0, 1])
y_values = np.array([0, 1])
fit = np.polynomial.polynomial.Polynomial.fit(x_values, y_values, 1)
y_fit = fit(x_values)
else:
raise NotImplementedError
# Joint values may have been mapped to a different range already, need to update constraints
if "joint" in constraints:
for c in constraints["joint"]:
if "@joint2" in c and params["name"] == c["@joint2"]:
assert c["fit"].degree() == fit.degree(), "Degrees must match"
fit = np.polynomial.polynomial.Polynomial.fit(y_fit, c["fit"](x_values), deg=c["fit"].degree())
polycoef = np.zeros((5,))
polycoef[:fit.coef.shape[0]] = fit.convert().coef
c["@polycoef"] = Utils.array_to_string(polycoef)
# Mark this dof as designated
dof_designated.append(params["name"])
elif params["name"] in dof_designated:
# A DoF has already been designated for a coordinate with params["name"], rename this joint
params["name"] = "{}_{}".format(params["name"], t["@name"])
# Handle a "Constant" transformation. We're not gonna create this joint
# but we need the transformation information to properly align the joint
flip_axis = False
if Utils.is_nested_field(t, "Constant", ["function"]) or \
Utils.is_nested_field(t, "Constant", ["function", "MultiplierFunction", "function"]):
# Get the value
if "MultiplierFunction" in t["function"]:
value = float(t["function"]["MultiplierFunction"]["function"]["Constant"]["value"])
elif "Constant" in t["function"]:
value = float(t["function"]["Constant"]["value"])
else:
raise NotImplementedError
# If the value is near zero don't bother creating this joint
if abs(value) < 1e-6:
continue
# Otherwise define a limited MuJoCo joint (we're not really creating this (sub)joint, we just update the
# joint position)
params["limited"] = True
params["range"] = np.array([value])
params["transform_value"] = value
params["add_to_mujoco_joints"] = False
# Handle a "SimmSpline" or "NaturalCubicSpline" transformation with a quartic approximation
elif Utils.is_nested_field(t, "SimmSpline", ["function", "MultiplierFunction", "function"]) or \
Utils.is_nested_field(t, "NaturalCubicSpline", ["function", "MultiplierFunction", "function"]) or \
Utils.is_nested_field(t, "SimmSpline", ["function"]) or \
Utils.is_nested_field(t, "NaturalCubicSpline", ["function"]):
# We can't model the relationship between two joints using a spline, but we can try to approximate it
# with a quartic function. So fit a quartic function and check that the error is small enough
# Get spline values
if Utils.is_nested_field(t, "SimmSpline", ["function"]):
x_values = t["function"]["SimmSpline"]["x"]
y_values = t["function"]["SimmSpline"]["y"]
elif Utils.is_nested_field(t, "NaturalCubicSpline", ["function"]):
x_values = t["function"]["NaturalCubicSpline"]["x"]
y_values = t["function"]["NaturalCubicSpline"]["y"]
elif Utils.is_nested_field(t, "SimmSpline", ["function", "MultiplierFunction", "function"]):
x_values = t["function"]["MultiplierFunction"]["function"]["SimmSpline"]["x"]
y_values = t["function"]["MultiplierFunction"]["function"]["SimmSpline"]["y"]
else:
x_values = t["function"]["MultiplierFunction"]["function"]["NaturalCubicSpline"]["x"]
y_values = t["function"]["MultiplierFunction"]["function"]["NaturalCubicSpline"]["y"]
# Convert into numpy arrays
x_values = np.array(x_values.split(), dtype=float)
y_values = np.array(y_values.split(), dtype=float)
assert len(x_values) > 1 and len(y_values) > 1, "Not enough points, can't fit a spline"
# Fit a linear / quadratic / cubic / quartic function
fit = np.polynomial.polynomial.Polynomial.fit(x_values, y_values, min(4, len(x_values) - 1))
# A simple check to see if the fit is alright
y_fit = fit(x_values)
assert r2_score(y_values, y_fit) > 0.5, "A bad approximation of the SimmSpline"
# Get the weights
polycoef = np.zeros((5,))
polycoef[:fit.coef.shape[0]] = fit.convert().coef
# Update name; since this is a dependent joint variable the independent joint variable might already
# have this name
if params["name"] == params["original_name"]:
params["name"] = "{}_{}".format(params["name"], t["@name"])
params["limited"] = True
# Get min and max values
y_fit = fit(x_values)
params["range"] = np.array([min(y_fit), max(y_fit)])
# Add a joint constraint between this joint and the independent joint, which we assume to be named
# t["coordinates"]
independent_joint = t["coordinates"]
# Some dependent joint values may be coupled to another joint values. We need to find the name of
# the independent joint
# TODO We could do this after the model has been built since we just swap joint names,
# then we wouldn't need to pass constraints into body/joint parser
# params["motion_type"] is typically "coupled" for dependent joints, but not always, so let's just loop
# through constraints and check
#constraint_found = False
# Go through all joint equality constraints
for c in constraints["joint"]:
if c["@joint1"] != t["coordinates"]:
continue
else:
#constraint_found = True
# Check if this constraint is active
if c["@active"] != "true":
break
# Change the name of the independent joint
independent_joint = c["@joint2"]
# We're handling only an identity transformation for now
coeffs = np.array(c["@polycoef"].split(), dtype=float)
assert np.allclose(coeffs, np.array([0, 1, 0, 0, 0])), \
"We're handling only identity transformations for now"
break
#assert constraint_found, "Couldn't find an independent joint for a coupled joint"
#else:
# Update motion type to dependent for posterity
params["motion_type"] = "dependent"
# These joint equality constraints don't seem to work properly. Is it because they're soft constraints?
# E.g. the translations between femur and tibia should be strictly defined by knee angle, but it seems
# like they're affected by gravity as well (tibia drops to translation range limit value when
# leg6dof9musc is hanging from air) -> seems to work when solimp limits are very tight
params["add_to_mujoco_joints"] = True
# Add the equality constraint
if params["add_to_mujoco_joints"]:
# We don't want to create a transform so set transform_value to zero
params["transform_value"] = 0
self.equality_constraints["joint"].append({"@name": params["name"] + "_constraint",
"@active": "true", "@joint1": params["name"],
"@joint2": independent_joint,
"@polycoef": Utils.array_to_string(polycoef),
"@solimp": "0.9999 0.9999 0.001 0.5 2"})
elif Utils.is_nested_field(t, "LinearFunction", ["function"]):
# I'm not sure how to handle a LinearFunction with coefficients != [1, 0] (the first one is slope,
# second intercept), except for [-1, 0] when we can just flip the axis
coefficients = np.array(t["function"]["LinearFunction"]["coefficients"].split(), dtype=float)
assert abs(coefficients[0]) == 1 and coefficients[1] == 0, "How do we handle this linear function?"
# If first coefficient is negative, flip the joint axis
if coefficients[0] < 0: