def construct_ik_task_set(model, marker_data, task_set): """Construct OpenSim Inverse Kinematics task set. In older versions of OpenSim (e.g. 3.3) IK will not execute when there are virtual markers that do not exist in the marker data. """ virtual_markers = model.getMarkerSet() marker_names = marker_data.getMarkerNames() for i in range(0, marker_names.getSize()): marker_name = marker_names.get(i) exists = False for j in range(0, virtual_markers.getSize()): if virtual_markers.get(j).getName() == marker_name: task = opensim.IKMarkerTask() task.setName(marker_name) task.setApply(True) task.setWeight(1) task_set.adoptAndAppend(task) exists = True break if not exists: task = opensim.IKMarkerTask() task.setName(marker_name) task.setApply(False) task.setWeight(1) task_set.adoptAndAppend(task)
def test_markAdoptedSets(): # Set's. fus = osim.FunctionSet() fu1 = osim.Constant() fus.adoptAndAppend(fu1) del fus del fu1 s = osim.ScaleSet() o = osim.Scale() s.adoptAndAppend(o) del s del o s = osim.ControlSet() o = osim.ControlLinear() s.adoptAndAppend(o) del s del o s = osim.BodyScaleSet() o = osim.BodyScale() s.adoptAndAppend(o) del s del o s = osim.PathPointSet() o = osim.PathPoint() s.adoptAndAppend(o) del s del o s = osim.IKTaskSet() o = osim.IKMarkerTask() s.adoptAndAppend(o) del s del o s = osim.MarkerPairSet() o = osim.MarkerPair() s.adoptAndAppend(o) del s del o s = osim.MeasurementSet() o = osim.Measurement() s.adoptAndAppend(o) del s del o s = osim.FrameSet() o = osim.Body() s.adoptAndAppend(o) del s del o s = osim.ForceSet() o = osim.CoordinateLimitForce() s.adoptAndAppend(o) del s del o s = osim.ForceSet() o = osim.SpringGeneralizedForce() s.append(o) s = osim.ProbeSet() o = osim.Umberger2010MuscleMetabolicsProbe() s.adoptAndAppend(o) del s del o a = osim.Model() body = osim.Body('body', 1.0, osim.Vec3(0, 0, 0), osim.Inertia(0, 0, 0) ) loc_in_parent = osim.Vec3(0, -0, 0) orient_in_parent = osim.Vec3(0, 0, 0) loc_in_body = osim.Vec3(0, 0, 0) orient_in_body = osim.Vec3(0, 0, 0) joint = osim.WeldJoint("weld_joint", a.getGround(), loc_in_parent, orient_in_parent, body, loc_in_body, orient_in_parent) a.addBody(body) constr = osim.ConstantDistanceConstraint() constr.setBody1ByName("ground") constr.setBody1PointLocation(osim.Vec3(0, 0, 0)) constr.setBody2ByName("body") constr.setBody2PointLocation(osim.Vec3(1, 0, 0)) constr.setConstantDistance(1) a.addConstraint(constr)
def test_markAdoptedSets(): # Set's. fus = osim.FunctionSet() fu1 = osim.Constant() fus.adoptAndAppend(fu1) del fus del fu1 gs = osim.GeometrySet() dg = osim.DisplayGeometry() gs.adoptAndAppend(dg) del gs del dg s = osim.ScaleSet() o = osim.Scale() s.adoptAndAppend(o) del s del o s = osim.ForceSet() o = osim.BushingForce() s.adoptAndAppend(o) del s del o cs = osim.ControllerSet() csc = osim.PrescribedController() cs.adoptAndAppend(csc) del cs del csc s = osim.ContactGeometrySet() o = osim.ContactHalfSpace() s.adoptAndAppend(o) del s del o s = osim.AnalysisSet() o = osim.MuscleAnalysis() s.adoptAndAppend(o) del s del o s = osim.ControlSet() o = osim.ControlLinear() s.adoptAndAppend(o) del s del o s = osim.MarkerSet() o = osim.Marker() s.adoptAndAppend(o) del s del o s = osim.BodySet() o = osim.Body() s.adoptAndAppend(o) del s del o s = osim.BodyScaleSet() o = osim.BodyScale() s.adoptAndAppend(o) del s del o s = osim.CoordinateSet() o = osim.Coordinate() s.adoptAndAppend(o) del s del o s = osim.JointSet() o = osim.BallJoint() s.adoptAndAppend(o) del s del o s = osim.PathPointSet() o = osim.PathPoint() s.adoptAndAppend(o) del s del o s = osim.IKTaskSet() o = osim.IKMarkerTask() s.adoptAndAppend(o) del s del o s = osim.MarkerPairSet() o = osim.MarkerPair() s.adoptAndAppend(o) del s del o s = osim.MeasurementSet() o = osim.Measurement() s.adoptAndAppend(o) del s del o # TODO # s = osim.ProbeSet() # o = osim.Umberger2010MuscleMetabolicsProbe() # s.adoptAndAppend(o) # del s # del o s = osim.ConstraintSet() a = osim.Model() body = osim.Body('body', 1.0, osim.Vec3(0, 0, 0), osim.Inertia(0, 0, 0)) loc_in_parent = osim.Vec3(0, -0, 0) orient_in_parent = osim.Vec3(0, 0, 0) loc_in_body = osim.Vec3(0, 0, 0) orient_in_body = osim.Vec3(0, 0, 0) joint = osim.WeldJoint("weld_joint", a.getGroundBody(), loc_in_parent, orient_in_parent, body, loc_in_body, orient_in_parent) a.addBody(body) constr = osim.ConstantDistanceConstraint() constr.setBody1ByName("ground") constr.setBody1PointLocation(osim.Vec3(0, 0, 0)) constr.setBody2ByName("body") constr.setBody2PointLocation(osim.Vec3(1, 0, 0)) constr.setConstantDistance(1) s.adoptAndAppend(constr)
TimeArray.set(1, final_time) # The static pose weights will be used to adjust the markers position in # the model from a static pose. The weights of the markers depend of the # confidence you have on its position.In this example, all marker weight # are fixed to one. scaleTool.getMarkerPlacer().setApply(1) # Ajustement placement de marqueurs(true or false) scaleTool.getMarkerPlacer().setStaticPoseFileName(TRC_file) # trc files for adjustements(usually the same as static) scaleTool.getMarkerPlacer().setTimeRange(TimeArray) # Time range scaleTool.getMarkerPlacer().setOutputModelFileName(scaled_MM_path2) scaleTool.getMarkerPlacer().setOutputMarkerFileName(XML_markers_path_move) scaleTool.getMarkerPlacer().setMaxMarkerMovement(-1.0) measurementTemp = osim.Measurement() ikMarkerTaskTemp = osim.IKMarkerTask() for i in range(0, markerList.shape[0]): ikMarkerTask = ikMarkerTaskTemp.clone() ikMarkerTask.setName(markerList[i]) # Name of the markers ikMarkerTask.setApply(1) ikMarkerTask.setWeight(1) scaleTool.getMarkerPlacer().getIKTaskSet().adoptAndAppend(ikMarkerTask) # Create the subject Scale Tool XML file scaleTool.printToXML(XML_ST_file) print('XML files : ' + XML_ST_file + ' created')
path, filename = os.path.split(TRC_file) filename, ext = os.path.splitext(filename) MOT_file = 'MOT\\' + filename + '.mot' # Path to the output .MOT file that will be created and contain the IK results XML_IK_file = 'XML\\' + filename + '_IK.xml' # Path to the IK XML file that will be created # Marker Data markerData = osim.MarkerData(TRC_file) initial_time = markerData.getStartFrameTime() final_time = markerData.getLastFrameTime() # Set the IK tool ikTool = osim.InverseKinematicsTool(XML_generic_IK_path) ikTool.setModel(osimModel) ikTool.setName(filename + ext) ikTool.setMarkerDataFileName(TRC_file) ikTool.setStartTime(initial_time) ikTool.setEndTime(final_time) ikTool.setOutputMotionFileName(MOT_file) # Set the ikTool with the MarkerTask for j in range(0, markerList.shape[0]): ikMarkerTask = osim.IKMarkerTask() ikMarkerTask.setName(markerList[j]) ikMarkerTask.setApply(1) ikMarkerTask.setWeight(markerWeight[j]) ikTool.getIKTaskSet().adoptAndAppend(ikMarkerTask) # Create the IK XML file ikTool.printToXML(XML_IK_file) ikTool.run()
def setup_IK_xml(trial: str, model: str, directory: str, time_range: list, marker_names: list): ''' Rewrites the IK setup xml file for a new trial Inputs: trial: trial name, e.g., "_12Mar_ss_12ms_01" model: model name, e.g., "AB08" directory: output directory name time_range: start and end times marker_names: list of the markers names which we are using ''' # Create an instance of the inverse kinematics tool IK_tool = osim.InverseKinematicsTool() # Set the name of the tool IK_tool.setName(model) # Set the input and results directory IK_tool.setInputsDir(directory + "\\" + model + "\\" + trial) IK_tool.setResultsDir(directory + "\\" + model + "\\" + trial) # Set the time range, NOTE: Must be a double (np.float64) IK_tool.setStartTime(np.float64(time_range[0])) IK_tool.setEndTime(np.float64(time_range[-1])) # Set the marker file marker_file_name = directory + "\\" + model + "\\" + trial + "\\" + trial + ".trc" IK_tool.setMarkerDataFileName(marker_file_name) # Set the coordinate file coordinate_file_name = '' IK_tool.setCoordinateFileName(coordinate_file_name) # Set the output motion file output_file_name = directory + "\\" + model + "\\" + trial + "\\" + trial + "IKResults.mot" IK_tool.setOutputMotionFileName(output_file_name) ''' Add markers and set weighting ''' # List of bony anatomical landmarkers to give high weighting bony_landmarks = ['LMMAL','RMMAL','LLMAL','RLMAL','LASI','RASI','LPSI','RPSI'] # Create IKTaskSet IK_task_set = IK_tool.getIKTaskSet() # Assign markers and weights for marker in marker_names: IK_marker_task = osim.IKMarkerTask() IK_marker_task.setName(marker) if marker in bony_landmarks: IK_marker_task.setApply(True) IK_marker_task.setWeight(10) else: IK_marker_task.setApply(True) IK_marker_task.setWeight(1) IK_task_set.cloneAndAppend(IK_marker_task) ''' Write changes to an XML setup file ''' xml_setup_path = directory + "\\" + model + "\\" + trial + "\\" + trial + "IKSetup.xml" IK_tool.printToXML(xml_setup_path) ''' Temporary fix for setting model name using XML parsing ''' dom = minidom.parse(xml_setup_path) dom.getElementsByTagName("model_file")[0].firstChild.nodeValue = directory + "\\" + model + "\\" + model + ".osim" with open(xml_setup_path, 'w') as xml_file: dom.writexml(xml_file, addindent='\t', newl='\n', encoding='UTF-8')