TDWUtils

January 19, 2024 ยท View on GitHub

from tdw.tdw_utils import TDWUtils

Utility functions for controllers.

Usage:

from tdw.tdw_utils import TDWUtils

Functions

vector3_to_array

TDWUtils.vector3_to_array(vector3)

(Static)

Convert a Vector3 object to a numpy array.

ParameterTypeDefaultDescription
vector3Dict[str, float]The Vector3 object, e.g. {"x": 0, "y": 0, "z": 0}

Returns: A numpy array.

array_to_vector3

TDWUtils.array_to_vector3(arr)

(Static)

Convert a numpy array to a Vector3.

ParameterTypeDefaultDescription
arrnp.ndarrayThe numpy array.

Returns: A Vector3, e.g. {"x": 0, "y": 0, "z": 0}

tuple_to_vector3

TDWUtils.tuple_to_vector3(tup)

(Static)

Convert a 3-element tuple to a Vector3.

ParameterTypeDefaultDescription
tupTuple[float, float, float]The tuple.

Returns: A Vector4, e.g. {"x": 0, "y": 0, "z": 0}

vector4_to_array

TDWUtils.vector4_to_array(vector4)

(Static)

Convert a Vector4 to a numpy array.

ParameterTypeDefaultDescription
vector4Dict[str, float]The Vector4 object, e.g. {"x": 0, "y": 0, "z": 0, "w": 0}

Returns: A numpy array.

array_to_vector4

TDWUtils.array_to_vector4(arr)

(Static)

Convert a numpy array to a Vector4.

ParameterTypeDefaultDescription
arrnp.ndarrayThe numpy array.

Returns: A Vector4, e.g. {"x": 0, "y": 0, "z": 0, "w": 0}

tuple_to_vector4

TDWUtils.tuple_to_vector4(tup)

(Static)

Convert a 4-element tuple to a Vector4.

ParameterTypeDefaultDescription
tupTuple[float, float, float, float]The tuple.

Returns: A Vector4, e.g. {"x": 0, "y": 0, "z": 0, "w": 0}

color_to_array

TDWUtils.color_to_array(color)

(Static)

Convert a RGB Color to a numpy array.

ParameterTypeDefaultDescription
colorDict[str, float]The Color object, e.g. {"r": 0, "g": 0, "b": 0, "a": 1}

Returns: A numpy array.

array_to_color

TDWUtils.array_to_color(arr)

(Static)

Convert a numpy array to a RGBA Color. If no A value is supplied it will default to 1.

ParameterTypeDefaultDescription
arrnp.ndarrayThe array.

Returns: A Color, e.g. {"r": 0, "g": 0, "b": 0, "a": 1}

tuple_to_color

TDWUtils.tuple_to_color(tup)

(Static)

Convert a 4-element tuple to a Color.

ParameterTypeDefaultDescription
tupTuple[float, float, float, float]The tuple.

Returns: A Color, e.g. {"r": 0, "g": 0, "b": 0, "a": 1}

get_random_point_in_circle

TDWUtils.get_random_point_in_circle(center, radius)

(Static)

Get a random point in a circle, defined by a center and radius.

ParameterTypeDefaultDescription
centernp.ndarrayThe center of the circle.
radiusfloatThe radius of the circle.

Returns: A numpy array. The y value (arr[1]) is always 0.

get_magnitude

TDWUtils.get_magnitude(vector3)

(Static)

Get the magnitude of a Vector3.

ParameterTypeDefaultDescription
vector3Dict[str, float]The Vector3 object, e.g. {"x": 0, "y": 0, "z": 0}

Returns: The vector magnitude.

extend_line

TDWUtils.extend_line(p0, p1, d)

TDWUtils.extend_line(p0, p1, d, clamp_y=True)

(Static)

Extend the line defined by p0 to p1 by distance d. Clamps the y value to 0.

ParameterTypeDefaultDescription
p0np.ndarrayThe origin.
p1np.ndarrayThe second point.
dfloatThe distance of which the line is to be extended.
clamp_yTrueClamp the y value to 0.

Returns: The position at distance d.

get_distance

TDWUtils.get_distance(vector3_0, vector3_1)

(Static)

Calculate the distance between two Vector3 (e.g. {"x": 0, "y": 0, "z": 0}) objects.

ParameterTypeDefaultDescription
vector3_0Dict[str, float]The first Vector3.
vector3_1Dict[str, float]The second Vector3.

Returns: The distance.

get_box

TDWUtils.get_box(width, length)

(Static)

Returns a list of x,y positions that can be used to create a box with the create_exterior_walls command.

ParameterTypeDefaultDescription
widthintThe width of the box.
lengthintThe length of the box.

Returns: The box as represented by a list of {"x": x, "y": y} dictionaries.

get_vector3

TDWUtils.get_vector3(x, y, z)

(Static)

ParameterTypeDefaultDescription
xThe x value.
yThe y value.
zThe z value.

Returns: A Vector3: {"x": x, "y", y, "z": z}

create_empty_room

TDWUtils.create_empty_room(width, length)

(Static)

ParameterTypeDefaultDescription
widthintThe width of the room.
lengthintThe length of the room.

Returns: A create_exterior_walls command that creates a box with dimensions (width, length).

create_room_from_image

TDWUtils.create_room_from_image(filepath)

TDWUtils.create_room_from_image(filepath, exterior_color=(255, interior_color=(0)

(Static)

Load a .png file from the disk and use it to create a room. Each pixel on the image is a grid point.

ParameterTypeDefaultDescription
filepathstrThe absolute filepath to the image.
exterior_color(255The color on the image marking exterior walls (default=red).
interior_color(0The color on the image marking interior walls (default=black).

Returns: A list of commands: The first creates the exterior walls, and the second creates the interior walls.

save_images

TDWUtils.save_images(images, filename)

TDWUtils.save_images(images, output_directory="dist", filename, resize_to=None, append_pass=True)

(Static)

Save each image in the Images object. The name of the image will be: pass_filename.extension, e.g.: "0000" -> depth_0000.png The images object includes the pass and extension information.

ParameterTypeDefaultDescription
imagesImagesThe Images object. Contains each capture pass plus metadata.
output_directory"dist"The directory to write images to.
filenamestrThe filename of each image, minus the extension. The image pass will be appended as a prefix.
resize_toNoneSpecify a (width, height) tuple to resize the images to. This is slower than saving as-is.
append_passboolTrueIf false, the image pass will not be appended to the filename as a prefix, e.g.: "0000": -> "0000.jpg"

get_shaped_depth_pass

TDWUtils.get_shaped_depth_pass(images, index)

(Static)

The _depth and _depth_simple passes are a 1D array of RGB values, as oppposed to a png or jpg like every other pass. This function reshapes the array into a 2D array of RGB values.

ParameterTypeDefaultDescription
imagesImagesThe Images output data.
indexintThe index in Images of the depth pass. See: Images.get_pass_mask().

Returns: A reshaped depth pass. Shape is: (height, width, 3).

zero_padding

TDWUtils.zero_padding(integer)

TDWUtils.zero_padding(integer, width=4)

(Static)

ParameterTypeDefaultDescription
integerintThe integer being converted.
width4The total number of digits in the string. If integer == 3 and width == 4, output is: "0003".

Returns: A string representation of an integer padded with zeroes, e.g. converts 3 to "0003".

get_pil_image

TDWUtils.get_pil_image(images, index)

(Static)

Converts Images output data to a PIL Image object. Use this function to read and analyze an image in memory. Do NOT use this function to save image data to disk; save_image is much faster.

ParameterTypeDefaultDescription
imagesImagesImages data from the build.
indexintThe index of the image in Images.get_image

Returns: A PIL image.

get_segmentation_colors

TDWUtils.get_segmentation_colors(id_pass)

(Static)

ParameterTypeDefaultDescription
id_passnp.ndarrayThe ID pass image as a numpy array.

Returns: A list of unique colors in the ID pass.

get_random_position_on_nav_mesh

TDWUtils.get_random_position_on_nav_mesh(c, width, length)

TDWUtils.get_random_position_on_nav_mesh(c, width, length, bake=True, rng=random.uniform, x_e=0, z_e=0)

(Static)

Returns a random position on a NavMesh.

ParameterTypeDefaultDescription
cControllerThe controller.
widthfloatThe width of the environment.
lengthfloatThe length of the environment.
bakeTrueIf true, send bake_nav_mesh.
rngrandom.uniformRandom number generator.
x_e0The x position of the environment.
z_e0The z position of the environment.

Returns: The coordinates as a tuple (x, y, z)

set_visual_material

TDWUtils.set_visual_material(c, substructure, object_id, material)

TDWUtils.set_visual_material(c, substructure, object_id, material, quality="med")

(Static)

ParameterTypeDefaultDescription
cControllerThe controller.
substructureList[dict]The metadata substructure of the object.
object_idintThe ID of the object in the scene.
materialstrThe name of the new material.
quality"med"The quality of the material.

Returns: A list of commands to set ALL visual materials on an object to a single material.

set_wireframe_material

TDWUtils.set_wireframe_material(substructure, object_id, color)

TDWUtils.set_wireframe_material(substructure, object_id, color, thickness=0.02)

(Static)

ParameterTypeDefaultDescription
substructureList[dict]The metadata substructure of the object.
object_idintThe ID of the object in the scene.
colorDict[str, float]The color to make the wireframe.
thicknessfloat0.02The thickness of the wireframe lines.

Returns: A list of commands to set ALL visual materials on an object to a single wireframe material.

get_depth_values

TDWUtils.get_depth_values(image)

TDWUtils.get_depth_values(image, depth_pass="_depth", width=256, height=256, near_plane=0.1, far_plane=100)

(Static)

Get the depth values of each pixel in a _depth image pass. The far plane is hardcoded as 100. The near plane is hardcoded as 0.1. (This is due to how the depth shader is implemented.)

ParameterTypeDefaultDescription
imagenp.ndarrayThe image pass as a numpy array.
depth_passstr"_depth"The type of depth pass. This determines how the values are decoded. Options: "_depth", "_depth_simple".
widthint256The width of the screen in pixels. See output data Images.get_width().
heightint256The height of the screen in pixels. See output data Images.get_height().
near_planefloat0.1The near clipping plane. See command set_camera_clipping_planes. The default value in this function is the default value of the near clipping plane.
far_planefloat100The far clipping plane. See command set_camera_clipping_planes. The default value in this function is the default value of the far clipping plane.

Returns: An array of depth values.

get_point_cloud

TDWUtils.get_point_cloud(depth, camera_matrix)

TDWUtils.get_point_cloud(depth, camera_matrix, vfov=54.43222, filename=None, near_plane=0.1, far_plane=100)

(Static)

Create a point cloud from an numpy array of depth values.

ParameterTypeDefaultDescription
depthDepth values converted from a depth pass. See: TDWUtils.get_depth_values()
camera_matrixUnion[np.ndarray, tuple]The camera matrix as a tuple or numpy array. See: send_camera_matrices.
vfovfloat54.43222The field of view. See: set_field_of_view
filenamestrNoneIf not None, the point cloud data will be written to this file.
near_planefloat0.1The near clipping plane. See command set_camera_clipping_planes. The default value in this function is the default value of the near clipping plane.
far_planefloat100The far clipping plane. See command set_camera_clipping_planes. The default value in this function is the default value of the far clipping plane.

Returns: An point cloud as a numpy array of [x, y, z] coordinates.

create_avatar

TDWUtils.create_avatar()

TDWUtils.create_avatar(avatar_type="A_Img_Caps_Kinematic", avatar_id="a", position=None, look_at=None)

(Static)

This is a wrapper for create_avatar and, optionally, teleport_avatar_to and look_at_position.

ParameterTypeDefaultDescription
avatar_type"A_Img_Caps_Kinematic"The type of avatar.
avatar_id"a"The avatar ID.
positionNoneThe position of the avatar. If this is None, the avatar won't teleport.
look_atNoneIf this isn't None, the avatar will look at this position.

Returns: A list of commands to create theavatar.

get_unit_scale

TDWUtils.get_unit_scale(record)

(Static)

ParameterTypeDefaultDescription
recordModelRecordThe model record.

Returns: The scale factor required to scale a model to 1 meter "unit scale".

validate_amazon_s3

TDWUtils.validate_amazon_s3()

(Static)

Validate that your local Amazon S3 credentials are set up correctly.

Returns: True if everything is OK.

get_base64_flex_particle_forces

TDWUtils.get_base64_flex_particle_forces(forces)

(Static)

ParameterTypeDefaultDescription
forceslistThe forces (see Flex documentation for how to arrange this array).

Returns: An array of Flex particle forces encoded in base64.

color_to_hashable

TDWUtils.color_to_hashable(color)

(Static)

ParameterTypeDefaultDescription
colorUnion[np.ndarray, Tuple[int, int, int]The color as an RGB array or tuple, where each value is between 0 and 255.

Returns: A hashable integer representation of the color array.

hashable_to_color

TDWUtils.hashable_to_color(hashable)

(Static)

ParameterTypeDefaultDescription
hashableintA hashable integer representing an RGB color.

Returns: A color as a numpy array of integers between 0 and 255: [r, g, b]

get_bounds_dict

TDWUtils.get_bounds_dict(bounds, index)

(Static)

ParameterTypeDefaultDescription
boundsBoundsBounds output data.
indexintThe index in bounds of the target object.

Returns: A dictionary of the bounds. Key = the name of the position. Value = the position as a numpy array.

get_bounds_extents

TDWUtils.get_bounds_extents(bounds)

TDWUtils.get_bounds_extents(bounds, index=0)

(Static)

ParameterTypeDefaultDescription
boundsUnion[Bounds, Dict[str, Dict[str, float]Bounds output data or cached bounds data from a record (record.bounds).
indexint0The index in bounds of the target object. Ignored if bounds is a dictionary.

Returns: The width (left to right), height (top to bottom), and length (front to back), of the bounds as a numpy array.

get_closest_position_in_bounds

TDWUtils.get_closest_position_in_bounds(origin, bounds, index)

(Static)

ParameterTypeDefaultDescription
originnp.ndarrayThe origin from which the distance is calculated.
boundsBoundsBounds output data.
indexintThe index in bounds of the target object.

Returns: The position on the object bounds that is closest to origin.

get_angle

TDWUtils.get_angle(position, origin, forward)

(Static)

ParameterTypeDefaultDescription
positionnp.ndarrayThe target position.
originnp.ndarrayThe origin position of the directional vector.
forwardnp.ndarrayThe forward directional vector.

Returns: The angle in degrees between forward and the direction vector from origin to position.

get_angle_between

TDWUtils.get_angle_between(v1, v2)

(Static)

ParameterTypeDefaultDescription
v1np.ndarrayThe first directional vector.
v2np.ndarrayThe second directional vector.

Returns: The angle in degrees between two directional vectors.

rotate_position_around

TDWUtils.rotate_position_around(position, angle)

TDWUtils.rotate_position_around(origin=None, position, angle)

(Static)

Rotate a position by a given angle around a given origin.

ParameterTypeDefaultDescription
originnp.ndarrayNoneThe origin position. If None, the origin is [0, 0, 0]
positionnp.ndarrayThe point being rotated.
anglefloatThe angle in degrees.

Returns: The rotated position.

euler_angles_to_rpy

TDWUtils.euler_angles_to_rpy(euler_angles)

(Static)

Convert Euler angles to ROS RPY angles.

ParameterTypeDefaultDescription
euler_anglesnp.ndarrayA numpy array: [x, y, z] Euler angles in degrees.

Returns: A numpy array: [r, p, y] angles in radians.

bytes_to_megabytes

TDWUtils.bytes_to_megabytes(b)

(Static)

ParameterTypeDefaultDescription
bintA quantity of bytes.

Returns: A quantity of megabytes.

get_circle_mask

TDWUtils.get_circle_mask(shape, row, column, radius)

(Static)

Get elements in an array within a circle.

ParameterTypeDefaultDescription
shapeTuple[int, int]The shape of the source array as (rows, columns).
rowintThe row (axis 0) of the center of the circle.
columnintThe column (axis 1) of the circle.
radiusintThe radius of the circle in indices.

Returns: A boolean array with shape shape. Elements that are True are within the circle.

download_asset_bundles

TDWUtils.download_asset_bundles(path, models, scenes, materials, hdri_skyboxes, robots, humanoids, humanoid_animations)

(Static)

Download asset bundles from TDW's remote S3 server. Create local librarian .json files for each type (models, scenes, etc.). This can be useful to speed up the process of scene creation; it is always faster to load local asset bundles though it still takes time to load them into memory.

Note that if you wish to download asset bundles from tdw-private (models_full.json) you need valid S3 credentials.

For each parameter (models, scenes, etc.), if the value is None, no asset bundles will be downloaded.

Asset bundles will only be downloaded for your operating system. For example, if you want Linux asset bundles, call this function on Linux.

ParameterTypeDefaultDescription
pathPATHThe root directory of all of the asset bundles and librarian files.
modelsDict[str, List[str]A dictionary of models. Key = The model library, for example "models_core.json". Value = A list of model names.
scenesDict[str, List[str]A dictionary of scenes. Key = The model library, for example "scenes.json". Value = A list of scene names.
materialsDict[str, List[str]A dictionary of materials. Key = The material library, for example "materials_med.json". Value = A list of material names.
hdri_skyboxesDict[str, List[str]A dictionary of HDRI skyboxes. Key = The HDRI skybox library, for example "hdri_skyboxes.json". Value = A list of HDRI skybox names.
robotsDict[str, List[str]A dictionary of robots. Key = The robot library, for example "robots.json". Value = A list of robot names.
humanoidsDict[str, List[str]A dictionary of humanoids. Key = The model library, for example "humanoids.json". Value = A list of humanoid names.
humanoid_animationsDict[str, List[str]A dictionary of humanoid animations. Key = The model library, for example "humanoid_animations.json". Value = A list of humanoid animation names.

set_default_libraries

TDWUtils.set_default_libraries()

TDWUtils.set_default_libraries(model_library=None, scene_library=None, material_library=None, hdri_skybox_library=None, robot_library=None, humanoid_library=None, humanoid_animation_library=None)

(Static)

Set the path to the default libraries.

If any of the parameters of this function are left as None, the default remote S3 librarian will be used.

ParameterTypeDefaultDescription
model_libraryPATHNoneThe absolute path to a local model library file.
scene_libraryPATHNoneThe absolute path to a local scene library file.
material_libraryPATHNoneThe absolute path to a local material library file.
hdri_skybox_libraryPATHNoneThe absolute path to a local HDRI skybox library file.
robot_libraryPATHNoneThe absolute path to a local robot library file.
humanoid_libraryPATHNoneThe absolute path to a local humanoid library file.
humanoid_animation_libraryPATHNoneThe absolute path to a local humanoid animation library file.

get_corners_from_wall

TDWUtils.get_corners_from_wall(wall)

(Static)

ParameterTypeDefaultDescription
wallCardinalDirectionThe wall as a CardinalDirection.

Returns: The corners of the wall as a 2-element list of OrdinalDirection.

get_direction_from_corner

TDWUtils.get_direction_from_corner(corner, wall)

(Static)

Given a corner and a wall, get the direction that a lateral arrangement will run along.

ParameterTypeDefaultDescription
cornerOrdinalDirectionThe corner as an OrdinalDirection.
wallCardinalDirectionThe wall as a CardinalDirection.

Returns: Tuple: direction, wall

get_expected_window_position

TDWUtils.get_expected_window_position()

TDWUtils.get_expected_window_position(window_width=256, window_height=256, monitor_index=0, title_bar_height=None)

(Static)

When the TDW build launches, it usually appears at the center of the primary monitor. The expected position of the top-left corner of the build window is therefore:

{"x": monitor.x + monitor.width / 2 - window_width / 2,
"y": monitor.y + monitor.height / 2 - window_height / 2 + title_bar_height}

Where monitor is the monitor corresponding to monitor_index.

To get a list of monitors:

import screeninfo
print(screeninfo.get_monitors())
ParameterTypeDefaultDescription
window_widthint256The width of the TDW build's window.
window_heightint256The height of the TDW build's window.
monitor_indexint0The index of the monitor. Usually, 0 is the index of the primary monitor.
title_bar_heightintNoneThe height of the window title bar in pixels. If None, this method will use a default value based on the operating system.

Returns: The expected position of the top-left corner of the build window.

get_path

TDWUtils.get_path(path)

(Static)

ParameterTypeDefaultDescription
pathPATHA path as either a string or a Path.

Returns: The path as a Path.

get_string_path

TDWUtils.get_string_path(path)

(Static)

ParameterTypeDefaultDescription
pathPATHA path as either a string or a Path.

Returns: The path as a string.

lerp

TDWUtils.lerp(a, b, t)

(Static)

ParameterTypeDefaultDescription
afloatThe first value.
bfloatThe second value. This must be greater than a.
tfloatThe lerp value (0 to 1).

Returns: A linearly interpolated value at point t between a and b.

inv_lerp

TDWUtils.inv_lerp(a, b, v)

(Static)

ParameterTypeDefaultDescription
afloatThe first value.
bfloatThe second value. This must be greater than a.
vfloatThe value. This must be between a and b (inclusive).

Returns: A value between 0 and 1 describing where v is with respect to a and b.

lerp_array

TDWUtils.lerp_array(a, b, t)

(Static)

ParameterTypeDefaultDescription
anp.ndarrayThe first array.
bnp.ndarrayThe second array.
tfloatThe lerp value (0 to 1).

Returns: A linearly interpolated array at point t between a and b.