Source code for ddg.visualization.blender.material

import bpy


[docs]def set_material(ob, material, protect=True): """ Assigns given material or material with given name to the object. A new material is creates if it did not exist before! Parameters ---------- ob: bpy.types.Object Blender object the material is assigned to. material: str or bpy.types.Material Name of the material or actual material object protect: bool (default=True) If True, the material is protected from being deleted when unused. Is only applied if the material did not exist before! """ if isinstance(material, str): mat_name = material material = bpy.data.materials.get(mat_name) if material is None: material = bpy.data.materials.new(name=mat_name) if protect is True: material.use_fake_user = True if ob.data.materials: ob.data.materials[0] = material else: ob.data.materials.append(material)
[docs]def protect_material(material): """ Protects given material from being deleted after reloading the file if not in use. Parameters ---------- mat_name: str or bpy.types.Material Name of the material or actual material object. Must exist. """ if isinstance(material, str): material = bpy.data.materials.get(material) material.use_fake_user = True
[docs]def material( name="DDG Material", color=(1, 1, 1), specular=1, roughness=0.5, alpha=1, input_dict={}, ): """Create a Principled BSDF material. Parameters ---------- name : str, (default="DDG Material") The name of the material. color : 3-tuple, (default=(1,1,1)) The color in RGB base. specular : int, (default=1) The specular value in [0,1] roughness : int, (default=0.5) The roughness value in [0,1] alpha : int, (default=1) The alpha value in [0,1] input_dict : dict, (default={}) An input dictionary for the the bsdf inputs Returns ------- bpy.types.Material """ material = bpy.data.materials.new(name) material.use_nodes = True base_color = color + (1,) # add alpha # Setup BSDF node bsdf_inputs = material.node_tree.nodes["Principled BSDF"].inputs bsdf_inputs["Base Color"].default_value = base_color bsdf_inputs["Specular"].default_value = specular bsdf_inputs["Roughness"].default_value = roughness bsdf_inputs["Alpha"].default_value = alpha if input_dict: for key, value in input_dict.items(): bsdf_inputs[key].default_value = value # Setup rendering in Viewport material.diffuse_color = base_color material.specular_intensity = specular material.roughness = roughness return material