kaolin.io.utils

API

exception kaolin.io.utils.NonHomogeneousMeshError(message)

Bases: Exception

Raised when expecting a homogeneous mesh but a heterogenous mesh is encountered.

message
class kaolin.io.utils.TextureExporter(base_dir, relative_dir='', file_prefix='', image_extension='.png', overwrite_files=False)

Bases: object

Utility functor that encapsulates logic about overwriting image files. Useful for e.g. saving textures without overwriting existing textures in a directory.

Example

export_fn = TextureExporter(base_dir, ‘textures’, overwrite_textures=False) rel_path = export_fn(image1, ‘image’) print(rel_path) # prints “textures/image.png”

# Save another image with same basename rel_path = export_fn(image2, ‘image’) print(rel_path) # prints “textures/image_0.png”

Parameters
  • base_dir (str) – base directory where to write images, must exist

  • relative_dir (str, optional) – if set, will create this subdirectory under base_dir and images will be saved there

  • file_prefix (str, optional) – prefix to add to filenames

  • image_extension (str) – extension to use for images; default is .png

  • overwrite_files (bool) – set to true to overwrite existing images; if False (default), will add an index to filename to ensure no image file is overwritten.

kaolin.io.utils.heterogeneous_mesh_handler_naive_homogenize(*args, **kwargs)

Same as mesh_handler_naive_triangulate(), see docs. .. deprecated:: 0.14.0

kaolin.io.utils.heterogeneous_mesh_handler_skip(*args, **kwargs)

Skip heterogeneous meshes.

kaolin.io.utils.mesh_handler_naive_triangulate(vertices, face_vertex_counts, *features, face_assignments=None)

Triangulate a list of faces containing polygons of varying number of edges using naive fan triangulation.

Parameters
  • vertices (torch.FloatTensor) – Vertices with shape (N, 3).

  • face_vertex_counts (torch.LongTensor) – Number of vertices for each face with shape (M) for M faces.

  • features – Variable length features that need to be handled as 1D Tensor (num_face_vertices), with one feature per face vertex. For example, faces as a tensor [face0_vertex0_id, face0_vertex1_id, face0_vertex2_id, face1_vertex0_id...] or as UV indices: [face0_vertex0_uv_idx, face0_vertex1_uv_idx, ...].

  • face_assignments (dict) – mapping from key to torch.LongTensor, where each value of the tensor corresponds to a face index. These indices will be expanded and rewritten to include triangulated face indices. Two modes are supported for face_assignments: 1) if 1D tensor, each face idx will be replaced with indices of faces it was split into 2) if 2D tensor, expects shape (K, 2), where [x, i] will be replaced with index of the first face [x, i] was split into, effectively supporting tensors containing (start,end].

Returns

Homogeneous list of attributes with exactly same type and number as function inputs.

  • vertices (torch.Tensor): unchanged vertices of shape (N, 3)

  • face_vertex_counts (torch.LongTensor): tensor of length new_num_faces filled with 3.

  • features (torch.Tensor): of same type as input and shape (new_num_faces, 3)

  • face_assignments (dict): returned only if face_assignments is set, with each value containing

    new face indices equivalent to the prior assignments (see two modes for face_assignments)

Return type

(tuple)

kaolin.io.utils.read_image(path)

Reads image from path. Note that this way is order of magnitude faster than some other ways; use this function rather than writing your own.

Parameters

path (str) – path where to read image from

Returns

(torch.FloatTensor) in range 0..1 with shape (height, width, num_channels)

kaolin.io.utils.write_image(img_tensor, path)

Writes PyTorch image tensor to file. Will create containing directory if does not exist.

Parameters
  • img_tensor (torch.Tensor) – tensor that is uint8 0..255 or float 0..1, either chw or hwc format, can be a batch of one, e.g. of shape (1, 3, H, W)

  • path (str) – image path where to save image; will overwrite existing

Returns: