parameters: (jsonobj,type='auto')
docstring: Converts from a JSON structure to a Klamp't object of the appropriate
      type. 
  
      Note: a JSON structure can be created from a JSON string using the
      ``json.loads()`` function in the Python builtin ``json`` module.
  
      Not all objects are supported yet, notably geometry-related objects and
      world entities.
  
      Args:
          jsonobj: A JSON structure (i.e., one coming from :func:`to_json`)
          type (str, optional): the type of the object (see
              :mod:`~klampt.model.types`) If 'auto' (default), the type of the
              object is inferred automatically.
docstring_summary: Converts from a JSON structure to a Klamp't object of the appropriate
      type.
docstring_tokens: Converts from a JSON structure to a Klamp t object of the appropriate type .
function: def from_json(jsonobj,type='auto'):
      """Converts from a JSON structure to a Klamp't object of the appropriate
      type. 
  
      Note: a JSON structure can be created from a JSON string using the
      ``json.loads()`` function in the Python builtin ``json`` module.
  
      Not all objects are supported yet, notably geometry-related objects and
      world entities.
  
      Args:
          jsonobj: A JSON structure (i.e., one coming from :func:`to_json`)
          type (str, optional): the type of the object (see
              :mod:`~klampt.model.types`) If 'auto' (default), the type of the
              object is inferred automatically.
  
      """
      if type == 'auto':
          if isinstance(jsonobj,(list,tuple)):
              return jsonobj
          elif isinstance(jsonobj,(bool,int,float,str)):
              return jsonobj
          elif isinstance(jsonobj,dict):
              if 'type' in jsonobj:
                  type = jsonobj["type"]
              elif 'times' in jsonobj and 'milestones' in jsonobj:
                  type = 'Trajectory'
              elif 'x' in jsonobj and 'n' in jsonobj and 'kFriction' in jsonobj:
                  type = 'ContactPoint'
          else:
              raise TypeError("Unknown JSON object of type "+jsonobj.__class__.__name)
  
      if type in ['Config','Configs','Vector','Matrix','Matrix3','Rotation','Value','IntArray','StringArray']:
          return jsonobj
      elif type == 'RigidTransform':
          return jsonobj
      elif type == 'ContactPoint':
          return ContactPoint(jsonobj['x'],jsonobj['n'],jsonobj['kFriction'])
      elif type == 'Trajectory' or type == 'LinearPath':
          return Trajectory(jsonobj['times'],jsonobj['milestones'])
      elif type == 'HermiteTrajectory':
          return HermiteTrajectory(jsonobj['times'],jsonobj['milestones'])
      elif type == 'SO3Trajectory':
          return SO3Trajectory(jsonobj['times'],jsonobj['milestones'])
      elif type == 'SE3Trajectory':
          return SO3Trajectory(jsonobj['times'],jsonobj['milestones'])
      elif type == 'IKObjective' or type == 'IKGoal':
          link = jsonobj['link']
          destlink = jsonobj['destLink'] if 'destLink' in jsonobj else -1
          posConstraint = 'free'
          rotConstraint = 'free'
          localPosition = endPosition = None
          direction = None
          endRotation = None
          localAxis = None
          if 'posConstraint' in jsonobj:
              posConstraint = jsonobj['posConstraint']
          if 'rotConstraint' in jsonobj:
              rotConstraint = jsonobj['rotConstraint']
          if posConstraint == 'planar' or posConstraint == 'linear':
              direction = jsonobj['direction']
          if posConstraint != 'free':
              localPosition = jsonobj['localPosition']
              endPosition = jsonobj['endPosition']
          if rotConstraint != 'free':
              endRotation = jsonobj['endRotation']
          if rotConstraint == 'axis' or rotConstraint == 'twoaxis':
              localAxis = jsonobj['localAxis']
          if posConstraint == 'free' and rotConstraint == 'free':
              #empty
              return IKObjective()
          elif posConstraint != 'fixed':
              raise NotImplementedError("Can't do non-fixed position constraints yet in Python API")
          if rotConstraint == 'twoaxis':
              raise NotImplementedError("twoaxis constraints are not implemented in Klampt")
          if rotConstraint == 'free':
              obj = IKObjective()
              if destlink >= 0:
                  obj.setRelativePoint(link,destlink,localPosition,endPosition)
              else:
                  obj.setFixedPoint(link,localPosition,endPosition)
              return obj
          elif rotConstraint == 'axis':
              obj = IKObjective()
              h = 0.1
              lpts = [vectorops.madd(localPosition,localAxis,-h),vectorops.madd(localPosition,localAxis,h)]
              wpts = [vectorops.madd(endPosition,endRotation,-h),vectorops.madd(endPosition,endRotation,h)]
              if destlink >= 0:
                  obj.setRelativePoints(link,destlink,lpts,wpts)
              else:
                  obj.setFixedPoints(link,lpts,wpts)
              return obj
          elif rotConstraint == 'fixed':
              obj = IKObjective()
              R = so3.from_moment(endRotation)
              obj.setFixedPosConstraint(localPosition,endPosition)
              obj.setFixedRotConstraint(R)
              return obj
          else:
              raise ValueError("Invalid IK rotation constraint "+rotConstraint)
      elif type == 'TriangleMesh':
          inds = sum(jsonobj['indices'],[])
          verts = sum(jsonobj['vertices'],[])
          mesh = TriangleMesh()
          mesh.indices.resize(len(inds))
          mesh.vertices.resize(len(verts))
          for i,v in enumerate(inds):
              mesh.indices[i] = v
          for i,v in enumerate(verts):
              mesh.vertices[i] = v
          return mesh
      elif type == 'PointCloud':
          pc = PointCloud()
          verts = sum(jsonobj['vertices'],[])
          pc.vertices.resize(len(verts))
          for i,v in enumerate(verts):
              pc.vertices[i] = v
          if 'propertyNames' in jsonobj:
              propNames = jsonobj['propertyNames']
              pc.propertyNames.resize(len(propNames))
              for i,v in enumerate(propNames):
                  pc.propertyNames[i] = v
              if 'properties' in jsonobj:
                  props = sum(jsonobj['properties'])
                  pc.properties.resize(len(props))
                  for i,v in enumerate(props):
                      pc.properties[i] = v
          #TODO: settings
          return pc
      elif type == 'VolumeGrid':
          vg = VolumeGrid()
          bbox = jsonobj['bmin'] + jsonobj['bmax']
          vg.bbox.resize(6)
          for i,v in enumerate(bbox):
              vg.bbox[i] = v
          vg.dims.resize(3)
          for i,v in enumerate(jsonobj['dims']):
              vg.dims[i] = v
          values = jsonobj['values']
          vg.values.resize(len(values))
          for i,v in enumerate(values):
              vg.values[i] = v
          return vg
      elif type == 'ConvexHull':
          ch = ConvexHull()
          points = sum(jsonobj['points'])
          ch.points.resize(len(points))
          for i,v in enumerate(points):
              ch.points[i] = v
          return ch
      elif type == 'Geometry3D':
          gtype = jsonobj['datatype']
          if gtype == '':
              return Geometry3D()
          return Geometry3D(from_json(jsonobj['data'],gtype))
      elif type in readers:
          return read(type,jsonobj["data"])
      else:
          raise ValueError("Unknown or unsupported type "+type)
function_tokens: def from_json ( jsonobj , type = 'auto' ) : if type == 'auto' : if isinstance ( jsonobj , ( list , tuple ) ) : return jsonobj elif isinstance ( jsonobj , ( bool , int , float , str ) ) : return jsonobj elif isinstance ( jsonobj , dict ) : if 'type' in jsonobj : type = jsonobj [ "type" ] elif 'times' in jsonobj and 'milestones' in jsonobj : type = 'Trajectory' elif 'x' in jsonobj and 'n' in jsonobj and 'kFriction' in jsonobj : type = 'ContactPoint' else : raise TypeError ( "Unknown JSON object of type " + jsonobj . __class__ . __name ) if type in [ 'Config' , 'Configs' , 'Vector' , 'Matrix' , 'Matrix3' , 'Rotation' , 'Value' , 'IntArray' , 'StringArray' ] : return jsonobj elif type == 'RigidTransform' : return jsonobj elif type == 'ContactPoint' : return ContactPoint ( jsonobj [ 'x' ] , jsonobj [ 'n' ] , jsonobj [ 'kFriction' ] ) elif type == 'Trajectory' or type == 'LinearPath' : return Trajectory ( jsonobj [ 'times' ] , jsonobj [ 'milestones' ] ) elif type == 'HermiteTrajectory' : return HermiteTrajectory ( jsonobj [ 'times' ] , jsonobj [ 'milestones' ] ) elif type == 'SO3Trajectory' : return SO3Trajectory ( jsonobj [ 'times' ] , jsonobj [ 'milestones' ] ) elif type == 'SE3Trajectory' : return SO3Trajectory ( jsonobj [ 'times' ] , jsonobj [ 'milestones' ] ) elif type == 'IKObjective' or type == 'IKGoal' : link = jsonobj [ 'link' ] destlink = jsonobj [ 'destLink' ] if 'destLink' in jsonobj else - 1 posConstraint = 'free' rotConstraint = 'free' localPosition = endPosition = None direction = None endRotation = None localAxis = None if 'posConstraint' in jsonobj : posConstraint = jsonobj [ 'posConstraint' ] if 'rotConstraint' in jsonobj : rotConstraint = jsonobj [ 'rotConstraint' ] if posConstraint == 'planar' or posConstraint == 'linear' : direction = jsonobj [ 'direction' ] if posConstraint != 'free' : localPosition = jsonobj [ 'localPosition' ] endPosition = jsonobj [ 'endPosition' ] if rotConstraint != 'free' : endRotation = jsonobj [ 'endRotation' ] if rotConstraint == 'axis' or rotConstraint == 'twoaxis' : localAxis = jsonobj [ 'localAxis' ] if posConstraint == 'free' and rotConstraint == 'free' : #empty return IKObjective ( ) elif posConstraint != 'fixed' : raise NotImplementedError ( "Can't do non-fixed position constraints yet in Python API" ) if rotConstraint == 'twoaxis' : raise NotImplementedError ( "twoaxis constraints are not implemented in Klampt" ) if rotConstraint == 'free' : obj = IKObjective ( ) if destlink >= 0 : obj . setRelativePoint ( link , destlink , localPosition , endPosition ) else : obj . setFixedPoint ( link , localPosition , endPosition ) return obj elif rotConstraint == 'axis' : obj = IKObjective ( ) h = 0.1 lpts = [ vectorops . madd ( localPosition , localAxis , - h ) , vectorops . madd ( localPosition , localAxis , h ) ] wpts = [ vectorops . madd ( endPosition , endRotation , - h ) , vectorops . madd ( endPosition , endRotation , h ) ] if destlink >= 0 : obj . setRelativePoints ( link , destlink , lpts , wpts ) else : obj . setFixedPoints ( link , lpts , wpts ) return obj elif rotConstraint == 'fixed' : obj = IKObjective ( ) R = so3 . from_moment ( endRotation ) obj . setFixedPosConstraint ( localPosition , endPosition ) obj . setFixedRotConstraint ( R ) return obj else : raise ValueError ( "Invalid IK rotation constraint " + rotConstraint ) elif type == 'TriangleMesh' : inds = sum ( jsonobj [ 'indices' ] , [ ] ) verts = sum ( jsonobj [ 'vertices' ] , [ ] ) mesh = TriangleMesh ( ) mesh . indices . resize ( len ( inds ) ) mesh . vertices . resize ( len ( verts ) ) for i , v in enumerate ( inds ) : mesh . indices [ i ] = v for i , v in enumerate ( verts ) : mesh . vertices [ i ] = v return mesh elif type == 'PointCloud' : pc = PointCloud ( ) verts = sum ( jsonobj [ 'vertices' ] , [ ] ) pc . vertices . resize ( len ( verts ) ) for i , v in enumerate ( verts ) : pc . vertices [ i ] = v if 'propertyNames' in jsonobj : propNames = jsonobj [ 'propertyNames' ] pc . propertyNames . resize ( len ( propNames ) ) for i , v in enumerate ( propNames ) : pc . propertyNames [ i ] = v if 'properties' in jsonobj : props = sum ( jsonobj [ 'properties' ] ) pc . properties . resize ( len ( props ) ) for i , v in enumerate ( props ) : pc . properties [ i ] = v #TODO: settings return pc elif type == 'VolumeGrid' : vg = VolumeGrid ( ) bbox = jsonobj [ 'bmin' ] + jsonobj [ 'bmax' ] vg . bbox . resize ( 6 ) for i , v in enumerate ( bbox ) : vg . bbox [ i ] = v vg . dims . resize ( 3 ) for i , v in enumerate ( jsonobj [ 'dims' ] ) : vg . dims [ i ] = v values = jsonobj [ 'values' ] vg . values . resize ( len ( values ) ) for i , v in enumerate ( values ) : vg . values [ i ] = v return vg elif type == 'ConvexHull' : ch = ConvexHull ( ) points = sum ( jsonobj [ 'points' ] ) ch . points . resize ( len ( points ) ) for i , v in enumerate ( points ) : ch . points [ i ] = v return ch elif type == 'Geometry3D' : gtype = jsonobj [ 'datatype' ] if gtype == '' : return Geometry3D ( ) return Geometry3D ( from_json ( jsonobj [ 'data' ] , gtype ) ) elif type in readers : return read ( type , jsonobj [ "data" ] ) else : raise ValueError ( "Unknown or unsupported type " + type )