klampt.vis.colorize module

Colorize an object to show heatmaps, false color images, etc.

klampt.vis.colorize.colorize(object, value, colormap=None, feature=None, vrange=None, lighting=None)[source]

Colorizes an object according to some value. Useful for making heatmaps, false color images, etc. Can only be used with point clouds and triangle meshes.


  • colorize(point_cloud,'z','plasma'): sets the point cloud to a rainbow colorization (the ‘plasma’ colormap in Matplotlib) depending on z.

  • colorize(trimesh,'nz'): colorizes the trimesh according to the z component of each triangle normal.

  • colorize(trimesh,[v1_val,v2_val,...,vm_val]): colorizes each vertex of the triangle mesh by the default colormap. (here m is the # of vertices)

  • colorize(trimesh,[t1_rgb,t2_rgb,...,tn_rgb]): colorizes each triangle of the triangle mesh to an assigned color. (here n is the # of triangles)

  • colorize(trimesh,'index','random',feature='vertices'): assigns random vertex colors.

  • colorize(trimesh,segments,'random'): if clusters is a list of segment IDs (len(segments) = # triangles), this assigns a random color to each segment.

  • colorize(trimesh,segments,'random',lighting=[0,0,-1]): similar to above, but each triangle is shaded as though the scene was lighted by a downward-facing directional light (noonday sun).

  • object

    either an object with both an appearance() and geometry() method, a PointCloud, a Geometry3D, or an Appearance.

    • In the first case, the associated appearance is updated.

    • For PointClouds, color attributes are added as an ‘rgb’ or ‘rgba’ channel, if color information is not already present.

    • For Geometry3Ds, the return value is an Appearance that can be used via Appearance.drawGL(object).

    • For Appearances, the face/vertex colors are assigned. Note that feature cannot be “auto”.

  • value (str or list) –

    a named feature of the geometry or a list of values or RGB/RGBA colors. Valid named features include:

    • ’p’ or ‘position’: assigns RGB color by position

    • ’n’ or ‘normal’: assigns RGB color by normal (range [-1,1] if vrange=None)

    • ’x’: assigns value by position x

    • ’y’: assigns value by position y

    • ’z’: assigns value by position z

    • ’nx’: assigns value by normal x (range [-1,1] if vrange=None)

    • ’ny’: assigns value by normal y (range [-1,1] if vrange=None)

    • ’nz’: assigns value by normal x (range [-1,1] if vrange=None)

    • ’index’: assigns value by feature index

    • Any point cloud feature name: assigns value by feature.

    If this is a list, its length should equal the number of features (either vertices or faces) in the object’s geometry.

  • colormap (str, optional) – either a Matplotlib colormap identifier or ‘random’. By default, ‘viridis’ is used. If ‘random’ is used, the value is discarded and instead random colors are assigned.

  • feature (str, optional) – if this cannot determine whether to use the vertices or faces of a triangle mesh, feature indicates which feature to use (either ‘vertices’ or ‘faces’).

  • vrange (pair, optional) – if values are 1D, this maps the value range to u=(v-vrange[0])/(vrange[1]-vrange[0]) before passing to the colormap. If not provided, the range is determined automatically by the min/max of values.

  • lighting (3-list or callable, optional) – if a 3-list, this is interpreted as the direction of a directional light. The items are shaded as though the light were illuminating the scene. If it’s a callable, this is a function f(p,n) of the position and normal of a point or triangle, which should return a value in the range [0,1] indicating the overall brightness of the item.