blenderproc.python.utility.Utility module

This module provides a collection of utility functions tied closely to BlenderProc.

class blenderproc.python.utility.Utility.BlockStopWatch(block_name)[source]

Bases: object

Calls a print statement to mark the start and end of this block and also measures execution time.

Usage: with BlockStopWatch(‘text’):

class blenderproc.python.utility.Utility.KeyFrame(frame)[source]

Bases: object

A content manager for setting the frame correctly.

static is_any_active()[source]

Returns whether the current execution point is surrounded by a KeyFrame context manager.

Return type:

bool

Returns:

True, if there is at least one surrounding KeyFrame context manager

state = <blenderproc.python.utility.Utility._KeyFrameState object>
class blenderproc.python.utility.Utility.NumpyEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: JSONEncoder

A json encoder that is also capable of serializing numpy arrays

default(o)[source]

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
class blenderproc.python.utility.Utility.UndoAfterExecution(check_point_name=None, perform_undo_op=True)[source]

Bases: object

Reverts all changes done to the blender project inside this block.

Usage: with UndoAfterExecution():

class blenderproc.python.utility.Utility.Utility[source]

Bases: object

The main utility class, helps with different BlenderProc functions.

static add_output_entry(output)[source]

Registers the given output in the scene’s custom properties

Parameters:

output (Dict[str, str]) – A dict containing key and path of the new output type.

blenderproc_root = '/home/wink_do/PycharmProjects/BlenderProcPip/blenderproc/python/utility/../../..'
static find_registered_output_by_key(key)[source]

Returns the output which was registered with the given key.

Parameters:

key (str) – The output key to look for.

Return type:

Optional[Any]

Returns:

The dict containing all information registered for that output. If no output with the given key exists, None is returned.

static generate_equidistant_values(num, space_size_per_dimension)[source]

This function generates N equidistant values in a 3-dim space and returns num of them.

Every dimension of the space is limited by [0, K], where K is the given space_size_per_dimension. Basically it splits a cube of shape K x K x K in to N smaller blocks. Where, N = cube_length^3 and cube_length is the smallest integer for which N >= num.

If K is not a multiple of N, then the sum of all blocks might not fill up the whole K ** 3 cube.

Parameters:
  • num (int) – The total number of values required.

  • space_size_per_dimension (int) – The side length of cube.

Return type:

Tuple[List[List[int]], int]

static get_current_version()[source]

Gets the current blenderproc version.

Return type:

Optional[str]

Returns:

a string, the BlenderProc version

Searches for the OutputMaterial in the given material and finds the connected node to it, removes the connection between this node and the output and returns this node and the material_output

Parameters:

material (Material) – Material on which this operation should be performed

Return type:

Tuple[Optional[Node], Node]

static get_nodes_created_in_func(nodes, created_in_func)[source]

Returns all nodes which are created in the given function

Parameters:
  • nodes (List[Node]) – list of nodes of the current material

  • created_in_func (str) – return all nodes created in the given function

Return type:

List[Node]

Returns:

The list of nodes with the given type.

static get_nodes_with_type(nodes, node_type, created_in_func=None)[source]

Returns all nodes which are of the given node_type

Parameters:
  • nodes (List[Node]) – list of nodes of the current material

  • node_type (str) – node types

  • created_in_func (Optional[str]) – Only return nodes created by the specified function

Return type:

List[Node]

Returns:

list of nodes, which belong to the type

static get_registered_outputs()[source]

Returns a list of outputs which were registered.

Return type:

List[Dict[str, Any]]

Returns:

A list of dicts containing all information registered for the outputs.

static get_temporary_directory()[source]
Return type:

str

Returns:

default temporary directory, shared memory if it exists

static get_the_one_node_with_type(nodes, node_type, created_in_func='')[source]

Returns the one node which is of the given node_type

This function will only work if there is only one of the nodes of this type.

Parameters:
  • nodes (List[Node]) – list of nodes of the current material

  • node_type (str) – node types

  • created_in_func (str) – only return node created by the specified function

Return type:

Node

Returns:

node of the node type

static hex_to_rgba(hex_value)[source]

Converts the given hex string to rgba color values.

Parameters:

hex_value (str) – The hex string, describing rgb.

Return type:

List[float]

Returns:

The rgba color, in form of a list. Values between 0 and 1.

static insert_keyframe(obj, data_path, frame=None)[source]

Inserts a keyframe for the given object and data path at the specified frame number:

Parameters:
  • obj (Object) – The blender object to use.

  • data_path (str) – The data path of the attribute.

  • frame (Optional[int]) – The frame number to use. If None is given, the current frame number is used.

Replaces the node between source_socket and dest_socket with a new node.

Before: source_socket -> dest_socket After: source_socket -> new_node_dest_socket and new_node_src_socket -> dest_socket

Parameters:
  • links (NodeLinks) – The collection of all links.

  • source_socket (NodeSocket) – The source socket.

  • new_node_dest_socket (NodeSocket) – The new destination for the link starting from source_socket.

  • new_node_src_socket (NodeSocket) – The new source for the link towards dest_socket.

  • dest_socket (NodeSocket) – The destination socket

static map_back_from_equally_spaced_equidistant_values(values, num_splits_per_dimension, space_size_per_dimension)[source]

Maps the given values back to their original indices.

This function calculates for each given value the corresponding index in the list of values created by the generate_equidistant_values() method.

Parameters:
  • values (ndarray) – An array of shape [M, N, 3];

  • num_splits_per_dimension (int) – The number of splits per dimension that were made when building up the equidistant values.

  • space_size_per_dimension (int) – The space size used for the 3D cube.

Return type:

ndarray

Returns:

A 2-dim array of indices corresponding to the given values.

static merge_dicts(source, destination)[source]

Recursively copies all key value pairs from src to dest (Overwrites existing)

Parameters:
  • source (Dict[Any, Any]) – The source dict.

  • destination (Dict[Any, Any]) – The destination dict

Return type:

Dict[Any, Any]

Returns:

The modified destination dict.

static output_already_registered(output, output_list)[source]

Checks if the given output entry already exists in the list of outputs, by checking on the key and path. Also throws an error if it detects an entry having the same key but not the same path and vice versa since this is ambiguous.

Parameters:
  • output (Dict[str, Any]) – The output dict entry.

  • output_list (List[Dict[str, Any]]) – The list of output entries.

Return type:

bool

Returns:

bool indicating whether it already exists.

static read_suncg_lights_windows_materials()[source]

Returns the lights dictionary and windows list which contains their respective materials

Return type:

Tuple[Dict[str, Tuple[List[str], List[str]]], List[str]]

Returns:

dictionary of lights’ and list of windows’ materials

static register_output(output_dir, prefix, key, suffix, version, unique_for_camposes=True)[source]

Registers new output type using configured key and file prefix.

Parameters:
  • output_dir (str) – The output directory containing the generated files.

  • prefix (str) – The default prefix of the generated files.

  • key (str) – The default key which should be used for storing the output in merged file.

  • suffix (str) – The suffix of the generated files.

  • version (str) – The version number which will be stored at key_version in the final merged file.

  • unique_for_camposes (bool) – True if the output to be registered is unique for all the camera poses

static replace_output_entry(output)[source]

Replaces the output in the scene’s custom properties with the given one

Parameters:

output (Dict[str, str]) – A dict containing key and path of the new output type.

static rgb_to_hex(rgb)[source]

Converts the given rgb to hex values.

Parameters:

rgb (Tuple[int, int, int]) – tuple of three with rgb integers.

Return type:

str

Returns:

Hex string.

temp_dir = '/home/wink_do/PycharmProjects/BlenderProcPip/docs/source/examples/debugging/temp'
used_temp_id = None
class blenderproc.python.utility.Utility._KeyFrameState[source]

Bases: _local

This class is only used in the KeyFrame class

blenderproc.python.utility.Utility.get_file_descriptor(file_or_fd)[source]

Returns the file descriptor of the given file.

Parameters:

file_or_fd (Union[int, IO]) – Either a file or a file descriptor. If a file descriptor is given, it is returned directly.

Return type:

int

Returns:

The file descriptor of the given file.

blenderproc.python.utility.Utility.num_frames()[source]

Returns the currently total number of registered frames.

Return type:

int

Returns:

The number of frames.

blenderproc.python.utility.Utility.reset_keyframes()[source]

Removes registered keyframes from all objects and resets frame_start and frame_end

Return type:

None

blenderproc.python.utility.Utility.resolve_path(path)[source]

Returns an absolute path. If given path is relative, current working directory is put in front.

Parameters:

path (Union[str, Path]) – The path to resolve.

Return type:

str

Returns:

The absolute path.

blenderproc.python.utility.Utility.resolve_resource(relative_resource_path)[source]

Returns an absolute path to the given BlenderProc resource.

Parameters:

relative_resource_path (str) – The relative path inside the BlenderProc resource folder.

Return type:

str

Returns:

The absolute path.

blenderproc.python.utility.Utility.set_keyframe_render_interval(frame_start=None, frame_end=None)[source]

Sets frame_start and/or frame_end which determine the frames that will be rendered.

Parameters:
  • frame_start (Optional[int]) – The new frame_start value. If None, it will be ignored.

  • frame_end (Optional[int]) – The new frame_end value. If None, it will be ignored.

blenderproc.python.utility.Utility.stdout_redirected(to='/dev/null', enabled=True)[source]

Redirects all stdout to the given file.

From https://stackoverflow.com/a/22434262.

Parameters:
  • to (Union[int, IO, str]) – The file which should be the new target for stdout. Can be a path, file or file descriptor.

  • enabled (bool) – If False, then this context manager does nothing.

Return type:

IO

Returns:

The old stdout output.