klampt.io package

klampt.io.html module

class klampt.io.html.HTMLSharePath(filename='path.html', name="Klamp't Three.js app", boilerplate='auto')[source]

An exporter that converts animations to shareable HTML files.

Examples:

sharer = HTMLSharePath("mypath.html",name="My spiffy path")
sharer.start(sim)  #or world
while [simulation is running]:
    #do whatever control you wish to do here
    sim.simulate(...)
    sharer.animate()
sharer.end() #this saves to the given filename
Parameters:
  • filename (str) – the HTML file to generate
  • name (str) – the title of the HTML page
  • boilerplate (str) – the location of the boilerplate HTML file. If ‘auto’, it’s automatically found in the klampt/data folder.
animate(time=None)[source]

Updates the path from the world. If the world wasn’t a simulator, the time argument needs to be provided

end()[source]
start(world)[source]

Begins the path saving with the given WorldModel or Simulator

klampt.io.html.make_fixed_precision(obj, digits)[source]

klampt.io.loader module

Functions for loading/saving Klampt’ objects to disk in the same format as the Klampt C++ bindings / JSON formats.

Can read/write objects using the general purpose reader/writer functions ‘read(type,text)’ and ‘write(x,type)’.

Can load/save objects using the general purpose loader/saver functions ‘load(type,fileName)’ and ‘save(x,type,fileName)’.

Json serialization/deserialization are handled using the toJson and fromJson functions.

klampt.io.loader.fromJson(jsonobj, type='auto')[source]

Converts from a JSON structure to a Klamp’t object of the appropriate type. If ‘type’ is not provided, the type of the object is inferred automatically.

A JSON structure can be created from a JSON string using json.loads().

klampt.io.loader.load(type, fn)[source]

General-purpose load of an arbitrary Klampt object from a file.

klampt.io.loader.loadGeometry3D(fn)[source]
klampt.io.loader.loadMultiPath(fn)[source]
klampt.io.loader.loadTrajectory(fn)[source]
klampt.io.loader.loadWorldModel(fn)[source]
klampt.io.loader.parseLines(text)[source]

Returns a list of lines from the given text. Understands end-of-line escapes ‘n’

klampt.io.loader.read(type, text)[source]

General-purpose read of an arbitrary Klampt object from a str.

klampt.io.loader.readContactPoint(text)[source]

Reads a contact point from a string ‘x1 x2 x3 n1 n2 n3 kFriction’

klampt.io.loader.readGeometricPrimitive(text)[source]
klampt.io.loader.readHold(text)[source]

Loads a Hold from a string

klampt.io.loader.readIKObjective(text)[source]

Reads an IKObjective from a string in the Klamp’t native format

link destLink posConstraintType [pos constraint items] ... rotConstraintType [rot constraint items]

where link and destLink are integers, posConstraintType is one of

  • N: no constraint
  • P: position constrained to a plane
  • L: position constrained to a line
  • F: position constrained to a point

and rotConstraintType is one of

  • N: no constraint
  • T: two-axis constraint (not supported)
  • A: rotation constrained about axis
  • F: fixed rotation

The [pos constraint items] contain a variable number of whitespace- separated items, dependending on posConstraintType:

  • N: 0 items
  • P: the local position xl yl zl, world position x y z on the plane, and plane normal nx,ny,nz
  • L: the local position xl yl zl, world position x y z on the line, and line axis direction nx,ny,nz
  • F: the local position xl yl zl and world position x y z

The [rot constraint items] contain a variable number of whitespace- separated items, dependending on rotConstraintType:

  • N: 0 items
  • T: not supported
  • A: the local axis xl yl zl and the world axis x y z
  • F: the world rotation matrix, in moment (aka exponential map) form mx my mz (see so3.from_moment())
klampt.io.loader.readIntArray(text)[source]

Reads a length-prepended vector from a string ‘n v1 … vn’

klampt.io.loader.readMatrix(text)[source]

Reads a matrix from a string in the format m n x11 x12 … x1n … xm1 xm2 … xmn

klampt.io.loader.readMatrix3(text)[source]

Reads a 3x3 matrix from a string

klampt.io.loader.readSe3(text)[source]

Reads an se3 element, i.e., rigid transformation, to string in the same format as written to by Klampt C++ bindings (row major R, followed by t).

klampt.io.loader.readSo3(text)[source]

Reads an so3 element, i.e., rotation matrix, from string in the same format as written to by Klampt C++ bindings (row major).

klampt.io.loader.readStringArray(text)[source]

Reads a length-prepended vector from a string ‘n v1 … vn’

klampt.io.loader.readVector(text)[source]

Reads a length-prepended vector from a string ‘n v1 … vn’

klampt.io.loader.readVectorList(text)[source]

Reads a list of endline-separated vectors from a string

klampt.io.loader.readVectorRaw(text)[source]

Reads a vector from a raw string ‘v1 … vn’

klampt.io.loader.save(obj, type, fn)[source]

General-purpose save of an arbitrary Klampt object to a file.

klampt.io.loader.toJson(obj, type='auto')[source]

Converts from a Klamp’t object to a JSON-compatible structure. If ‘type’ is not provided, the type of the object is inferred automatically.

The structure can be converted to a JSON string using json.dumps().

Not all objects are supported yet.

klampt.io.loader.write(obj, type)[source]

General-purpose write of an arbitrary Klampt object to a str.

klampt.io.loader.writeContactPoint(cp)[source]

Writes a contact point to a string ‘x1 x2 x3 n1 n2 n3 kFriction’

klampt.io.loader.writeGeometricPrimitive(g)[source]
klampt.io.loader.writeHold(h)[source]

Writes a Hold to a string

klampt.io.loader.writeIKObjective(obj)[source]
klampt.io.loader.writeMatrix(x)[source]

Writes a matrix to a string in the format m n x11 x12 … x1n … xm1 xm2 … xmn

klampt.io.loader.writeMatrix3(x)[source]

Writes a 3x3 matrix to a string

klampt.io.loader.writeSe3(x)[source]

Writes an se3 element, i.e., rigid transformation, to string in the same format as written to by Klampt C++ bindings (row major R, followed by t).

klampt.io.loader.writeSo3(x)[source]

Writes an so3 element, i.e., rotation matrix, to string in the same format as written to by Klampt C++ bindings (row major).

klampt.io.loader.writeVector(q)[source]

Writes a vector to a string in the length-prepended format ‘n v1 … vn’

klampt.io.loader.writeVectorList(x)[source]

Writes a list of vectors to string

klampt.io.loader.writeVectorRaw(x)[source]

Writes a vector to a string in the raw format ‘v1 … vn’

klampt.io.resource module

Easy I/O with resources stored on disk and visual editing.

Use the get(), set(), and edit() functions to retrieve / store / edit resources dynamically.

load() and save() launch a file browser (available in Qt only).

By default, resources are stored in the resources/ subdirectory of the current working directory. Use getDirectory() and setDirectory() to change where resources are stored Alternatively, the directory=[DIRNAME] keyword argument can be provided to get, set, load, and save.

Warning

Don’t use from klampt.io.resource import *, because this will override the built-in set class.

Example usage can be seen in Klampt-examples/Python/demos/resourcetest.py.

class klampt.io.resource.FileGetter(title='Open file')[source]
getOpen()[source]
getSave()[source]
klampt.io.resource.console_edit(name, value, type, description=None, world=None, frame=None)[source]
klampt.io.resource.edit(name, value, type='auto', description=None, editor='visual', world=None, referenceObject=None, frame=None)[source]

Launches an editor for the given value.

Parameters:
  • name (str) – the displayed name of the edited value. Can be None, in which case ‘Anonymous’ is displayed.
  • value – the value to be edited. Can be None, in which case ‘type’ must be specified and a default value is created.
  • type (str) – the type string of the value to be edited. Usually can be auto-detected from value.
  • description (str, optional) – a descriptive string, displayed to the person editing.
  • editor (str) – either ‘visual’ or ‘console’. If ‘visual’, will display a GUI for visually editing the item. If ‘console’, the user will have to type in the value.
  • world (WorldModel or str, optional) – either a WorldModel instance or a string specifying a world file. This is necessary for visual editing.
  • referenceObject (optional) – a RobotModel or other object to which the value “refers to”. For configurations and trajectories, this is the object that will be moved by the trajectory. In the case of a RigidTransform value, this can be an object or a list of objects that will be transformed by the transform.
  • frame (optional) – for Vector3, Matrix3, Point, Rotation, and RigidTransform types, the returned value will be given relative to this reference frame. The reference frame can be either an element of se3, an ObjectModel, a RobotModelLink, or a string indicating a named rigid element of the world.
Returns:

A pair (save, result):

  • save (bool): True if the user pressed OK, False if Cancel or the
    close box where chosen.
  • result: the edited value.

Return type:

(tuple)

klampt.io.resource.filenameToType(name)[source]
klampt.io.resource.get(name, type='auto', directory=None, default=None, doedit='auto', description=None, editor='visual', world=None, referenceObject=None, frame=None)[source]

Retrieve a resource of the given name from the current resources directory. If the resource does not already exist, an edit prompt will be shown, and the result from the prompt will be saved to disk under the given name.

Resources may be of type Config, Configs, IKGoal, Hold, Stance, MultiPath, Trajectory/LinearPath, and ContactPoint, or any of the basic Klampt object types, geometry types, and math types.

Resources can also be edited using RobotPose.

Parameters:
  • name (str) – the resource name. If type=’auto’, this is assumed to have a suffix of a file of the desired type. The name can also be nested of the form ‘group/subgroup/name.type’.
  • type (str) – the resource type, or ‘auto’ to determine it automatically.
  • directory (str, optional) – the search directory. If None, uses the current resource directory.
  • default (optional) – the default value if the resource does not exist on disk. If None, some arbitrary default value will be inferred.
  • doedit – if ‘auto’, if the resource does not exist on disk, an edit prompt will be displayed. If False, an RuntimeError will be raised if the resource does not exist on disk. If True, the user will be given an edit prompt to optionally edit the resource before this call returns.
  • description (str, optional) – an optional text description of the resource, for use in the edit prompt.
  • editor (str) – either ‘visual’ or ‘console’, determining whether to use the visual OpenGL or console editor.
  • world (WorldModel, optional) – for a visual editor, this world will be shown along with the item to edit. If this is a string it points to a file that will be loaded for the world (e.g., a world XML file, or a robot file).
  • referenceObject (optional) – to give visual reference points, one or more RobotModels, ObjectModels, Geometry3D’s, or RobotModelLink’s may be designated to follow the edited object. Currently works with Config’s / Configs’ / Trajectories / rigid transforms / rotations / points.
  • frame (optional) – for rigid transforms / rotations / points, the reference frame in which the quantity is represented. This is an element of se3, or an ObjectModel, or a RobotModelLink, or a string indicating a named rigid element of the world.
klampt.io.resource.getDirectory()[source]

Returns the current resource directory.

klampt.io.resource.knownExtensions()[source]

Returns all known file extensions

klampt.io.resource.knownTypes()[source]

Returns all known types

klampt.io.resource.load(type=None, directory=None)[source]

Pops up a dialog that asks the user to load a resource file of a given type.

Parameters:
  • type (str, optional) – The Klampt type the user should open. If not given, all resource file types are shown in the dialog as options.
  • directory (str, optional) – if given, overrides the current resources directory.
Returns:

a (filename,value) pair if OK is pressed, or

None if the operation was canceled

Return type:

(tuple or None)

klampt.io.resource.save(value, type='auto', directory=None)[source]

Pops up a dialog that asks the user to save a resource to a file of the correct type.

Parameters:
  • value – a Klamp’t object that has a resource type
  • type (str, optional) – The Klampt type the user should open. If ‘auto’, the type is auto-detected.
  • directory (str, optional) – if given, overrides the current resources directory.
Returns:

the file saved to, if OK is pressed, or

None if the operation was canceled.

Return type:

(str or None)

klampt.io.resource.set(name, value, type='auto', directory=None)[source]

Saves a resource to disk under the given name.

Parameters:
  • name (str) – the file name. Please make sure to get the right file extension. .json files are also OK for many types.
  • value – a Klamp’t resource (see list of compatible types in get())
  • type (str, optional) – The resource type. If ‘auto’, the type is determined by the file extension. If this fails, the type is determined by the value.
Returns:

True on success

Return type:

(bool)

klampt.io.resource.setDirectory(value)[source]

Sets the current resource directory.

klampt.io.resource.thumbnail(value, size, type='auto', world=None, frame=None)[source]

Retrieves an image of the given item, resized to the given size.

Parameters:
  • value – a resource type.
  • size (pair of ints) – the (width,height) of the thumbnail, in pixels.
  • type (str, optional) – the type of value
  • world (WorldModel, optional) – if given, the resource will be drawn with this world in the background. If the resource needs an associated object (e.g., Config, Configs, Trajectory), the object will be drawn with that given object.
  • frame (se3 element, optional) – not supported yet. Will eventually let you draw Points or RigidTransforms relative to some reference frame.
Returns:

A PIL Image if PIL is available, or just a raw RGBA

memory buffer otherwise.

Return type:

(Image or bytes)

klampt.io.resource.visualEditTypes()[source]

Returns types that can be visually edited

klampt.io.SubscribeToStream(*args)[source]

Subscribes a Geometry3D to a stream.

SubscribeToStream (g,protocol,name,type): bool

SubscribeToStream (g,protocol,name): bool

Parameters:
  • g (Geometry3D) – the geometry that will be updated
  • protocol (str) – only “ros” accepted for now.
  • name (str) – the name of the stream. E.g., ROS topic.
  • type (str, optional) – If provided, specifies the format of the data to be subscribed to. If not, tries to determine the type automatically.

Only ROS point clouds (PointCloud2) are supported for now. Note that you can also call Geometry3D.loadFile(“ros://[ROS_TOPIC]”) or Geometry3D.loadFile(“ros:PointCloud2//[ROS_TOPIC]”) to accomplish the same thing.

Returns: (bool): True if successful.

klampt.io.DetachFromStream(protocol, name)[source]

Unsubscribes from a stream previously subscribed to via SubscribeToStream()

Parameters:
  • protocol (str) –
  • name (str) –
Returns:

Return type:

(bool)

klampt.io.ProcessStreams(*args)[source]

Does some processing on stream subscriptions.

ProcessStreams (protocol): bool

ProcessStreams (): bool

Parameters:protocol (str) – either name the protocol to be updated, or “all” for updating all subscribed streams

Returns: (bool): True if any stream was updated.

klampt.io.WaitForStream(protocol, name, timeout)[source]

Waits up to timeout seconds for an update on the given stream.

Parameters:
  • protocol (str) –
  • name (str) –
  • timeout (float) –
Returns:

Return type:

(bool)

Returns:

True if the stream was updated.

Return type:

(bool)

klampt.io.ThreeJSGetScene(arg1)[source]

Exports the WorldModel to a JSON string ready for use in Three.js.

Parameters:arg1 (WorldModel) –
Returns:
Return type:(str)
klampt.io.ThreeJSGetTransforms(arg1)[source]

Exports the WorldModel to a JSON string ready for use in Three.js.

Parameters:arg1 (WorldModel) –
Returns:
Return type:(str)
klampt.io.load(type, fn)[source]

General-purpose load of an arbitrary Klampt object from a file.

klampt.io.save(obj, type, fn)[source]

General-purpose save of an arbitrary Klampt object to a file.

klampt.io.write(obj, type)[source]

General-purpose write of an arbitrary Klampt object to a str.

klampt.io.read(type, text)[source]

General-purpose read of an arbitrary Klampt object from a str.

klampt.io.toJson(obj, type='auto')[source]

Converts from a Klamp’t object to a JSON-compatible structure. If ‘type’ is not provided, the type of the object is inferred automatically.

The structure can be converted to a JSON string using json.dumps().

Not all objects are supported yet.

klampt.io.fromJson(jsonobj, type='auto')[source]

Converts from a JSON structure to a Klamp’t object of the appropriate type. If ‘type’ is not provided, the type of the object is inferred automatically.

A JSON structure can be created from a JSON string using json.loads().