synthpic2.blender.utilities

Useful Blender functions that don’t have their final place yet.

Module Contents

Classes

_RenamingTracker

Helper class to copy data blocks of blender objects and keep track of their

Functions

duplicate_and_assign_material(→ Dict[str, ...)

Copy a material with given material_name and assign it to a given _object.

assign_material(→ None)

Assign a material with given material_name and to a given _object.

adapt_interface_iors(→ None)

Calculate the effective index of refraction (IOR) for a refractive material

render_to_file(→ None)

Renders an image to a file. The output root will be created, if necessary.

duplicate_and_link_object(→ Tuple[bpy.types.Object, ...)

Creates an independent duplicate of a blender object and links it to a

replace_object_material(→ None)

Replace the material of an object (specified by its name) with a new material

get_material(→ bpy.types.Material)

Returns a blender material with the specified name.

add_object_to_collection(→ None)

get_object(→ bpy.types.Object)

Returns a blender object with the specified name.

get_collection(→ bpy.types.Collection)

Returns a blender collection with the specified name.

create_collection(→ bpy.types.Collection)

Create a collection with name collection_name and link it to the given scene.

flip_face_normals_with_material_name(→ None)

Flip normals of the object faces, if the faces use a certain material.

replace_material(→ None)

Replace the material of an object with a new material (both identified by their

create_emission_shader() → bpy.types.Material)

Create an emission shader with a specified color and a strength of 1.

set_rigidity(→ None)

Set the rigidity of a given object.

selected_only(object_)

Temporarily select the given object_.

select_only(→ None)

Deselect all objects, select and activate the given object_.

delete(→ None)

Delete the object_.

convert_blender_object_to_blender_mesh(→ bpy.types.Object)

Convert a blender object to a blender mesh and restore the transformation and

convert_blender_object_to_trimesh(→ trimesh.Trimesh)

set_parent(→ None)

update_dependency_graph(→ None)

hide_in_render(→ None)

show_in_render(→ None)

select(→ None)

deselect(→ None)

deselect_all(→ None)

activate(→ None)

export_selected_objects_as_stl(→ None)

Exports selected objects of the current blender scene as stl file. The output

Attributes

set_context

synthpic2.blender.utilities.duplicate_and_assign_material(object_: bpy.types.Object, material_name: str, suffix: str) Dict[str, synthpic2.custom_types.RenamingMap]

Copy a material with given material_name and assign it to a given _object.

Args:

object_ (bpy.types.Object): Blender object to assign the material to. material_name (str): the name of the material suffix (str): suffix that is appended to the name of the material

synthpic2.blender.utilities.assign_material(object_: bpy.types.Object, material_name: str) None

Assign a material with given material_name and to a given _object.

Args:

object_ (bpy.types.Object): Blender object to assign the material to. material_name (str): the name of the material

synthpic2.blender.utilities.adapt_interface_iors(material_name_outside: str, material_name_inside: str) None

Calculate the effective index of refraction (IOR) for a refractive material inside another refractive material, based on the vacuum IORs of both materials.

In Blender: The outside object “sees” blue normals, while the inside object “sees” red normals.

Args:
material_name_outside (str): Name of the Blender material that is on the

outside.

material_name_inside (str): Name of the Blender material that is on the inside.

Raises:

ConventionError: Raised, if the outside material has multiple nodes with IORs.

synthpic2.blender.utilities.render_to_file(output_path: synthpic2.custom_types.AnyPath) None

Renders an image to a file. The output root will be created, if necessary.

Args:

output_path (AnyPath): Path of the output image file.

class synthpic2.blender.utilities._RenamingTracker(suffix: str)

Helper class to copy data blocks of blender objects and keep track of their renaming.

renaming_maps: Dict[str, synthpic2.custom_types.RenamingMap]
copy(data_block: synthpic2.custom_types.BlenderDataBlock) synthpic2.custom_types.BlenderDataBlock
Creates an independent duplicate of a blender object and links it to a

collection.

Args:

object_ (bpy.types.Object): object to be duplicated duplicate_name_suffix (str): suffix for the name of the duplicate target_collection (bpy.types.Collection): collection, to which the object will

be linked

Returns:

bpy.types.Object: duplicate Dict[str, RenamingMap]: RenamingMap with information on how relevant data blocks

were renamed.

synthpic2.blender.utilities.replace_object_material(object_: bpy.types.Object, old_material_name: str, new_material_name: str) None
Replace the material of an object (specified by its name) with a new material

(specified by its name).

Source:

https://blender.stackexchange.com/questions/53366/how-can-i-replace-a-material-from-python

Raises:
ValueError: Raised, if the object does not use a material named

old_material_name.

Args:

object_ (bpy.types.Object): blender object, whose material shall be replaced old_material_name (str): name of the old material new_material_name (str): name of the new material

synthpic2.blender.utilities.get_material(material_name: str) bpy.types.Material

Returns a blender material with the specified name.

Args:

material_name (str): Name of the material to return.

Raises:

ValueError: Raised, if the material does not exist.

Returns:

bpy.types.Material: Blender material

synthpic2.blender.utilities.add_object_to_collection(object_: bpy.types.Object, collection_name: str) None
synthpic2.blender.utilities.get_object(object_name: str) bpy.types.Object

Returns a blender object with the specified name.

Args:

object_name (str): Name of the object to return.

Raises:

ValueError: Raised, if the object does not exist.

Returns:

bpy.types.Object: Blender object

synthpic2.blender.utilities.get_collection(collection_name: str) bpy.types.Collection

Returns a blender collection with the specified name.

Args:

collection_name (str): Name of the collection to return.

Raises:

ValueError: Raised, if the collection does not exist.

Returns:

bpy.types.Collection: Blender collection

synthpic2.blender.utilities.create_collection(collection_name: str, scene: bpy.types.Scene | None = None) bpy.types.Collection

Create a collection with name collection_name and link it to the given scene.

Args:

collection_name (str): name of the new collection. scene (Optional[bpy.types.Scene], optional): Scene that the collection is

linked to. Defaults to None, which means that the collection is linked to the scene in context.

Returns:

bpy.types.Collection: the new collection

synthpic2.blender.utilities.flip_face_normals_with_material_name(object_: bpy.types.Object, material_name: str) None

Flip normals of the object faces, if the faces use a certain material.

Args:

object_ (bpy.types.Object): [description] material_name (str): Name of the material to identify faces to flip.

synthpic2.blender.utilities.replace_material(object_: bpy.types.Object, old_material_name: str, new_material_name: str) None
Replace the material of an object with a new material (both identified by their

names).

Args:

object (bpy.types.Object): object for which we are replacing the material old_material (str): the old material name new_material (str): the new material name

synthpic2.blender.utilities.create_emission_shader(color: Tuple[float, float, float, float] = (1, 1, 1, 1)) bpy.types.Material

Create an emission shader with a specified color and a strength of 1.

Taken from https://vividfax.github.io/2021/01/14/blender-materials.html

Args:
color (Tuple[float,float,float,float,], optional): 4-Tuple (RGBA) of floats

between 0 and 1. Defaults to (1,1,1,1).

Returns:

bpy.types.Material

synthpic2.blender.utilities.set_rigidity(object_: bpy.types.Object, state: bool) None

Set the rigidity of a given object.

Args:

state (bool): new rigidity state

synthpic2.blender.utilities.selected_only(object_: bpy.types.Object)

Temporarily select the given object_.

Args:

object_ (bpy.types.Object): Object to be temporarily selected.

Yields:

bpy.types.Object: The selected object.

synthpic2.blender.utilities.select_only(object_: bpy.types.Object) None

Deselect all objects, select and activate the given object_.

Args:

object_ (bpy.types.Object): object to be selected and activated.

synthpic2.blender.utilities.delete(object_: bpy.types.Object) None

Delete the object_.

Args:

object_ (bpy.types.Object): object to be deleted.

synthpic2.blender.utilities.convert_blender_object_to_blender_mesh(object_: bpy.types.Object) bpy.types.Object

Convert a blender object to a blender mesh and restore the transformation and parent/child relation of potential children.

Args:

object_ (bpy.types.Object): object to be converted.

Returns:

bpy.types.Object: converted mesh object.

synthpic2.blender.utilities.convert_blender_object_to_trimesh(object_: bpy.types.Object) trimesh.Trimesh
synthpic2.blender.utilities.set_parent(child: bpy.types.Object, parent: bpy.types.Object | None, keep_transform: bool = False) None
synthpic2.blender.utilities.update_dependency_graph() None
synthpic2.blender.utilities.hide_in_render(object_: bpy.types.Object) None
synthpic2.blender.utilities.show_in_render(object_: bpy.types.Object) None
synthpic2.blender.utilities.select(object_: bpy.types.Object) None
synthpic2.blender.utilities.deselect(object_: bpy.types.Object) None
synthpic2.blender.utilities.deselect_all() None
synthpic2.blender.utilities.activate(object_: bpy.types.Object) None
synthpic2.blender.utilities.set_context
synthpic2.blender.utilities.export_selected_objects_as_stl(output_path: synthpic2.custom_types.AnyPath) None
Exports selected objects of the current blender scene as stl file. The output

root will be created, if necessary.

Args:

output_path (AnyPath): Path of the output image file.