Post #4788: Exactly as you wrote, I try to make the Timeline window as clear as possible in

📋 Metadata

🏷️ Tags

macro scripting intermediate

  • Macro Editor
  • Python Scripting

💬 Content

Exactly as you wrote, I try to make the Timeline window as clear as possible in terms of the number of keyframes.

The script below creates a new material, with a proper tree of nodes, which is ready to change the transparency by using the “Object Info” node.
The script works in “Object” and “Edit Mode”, automatically assigning new material to selected polygons, which speeds up and facilitates the work.

import bpy

# Get all selected objects
selected_objects = bpy.context.selected_objects

# Iterate through each selected object
for obj in selected_objects:
    # Check if the object is of type 'MESH'
    if obj.type == 'MESH':
        # Create a new material
        material = bpy.data.materials.new(name=obj.name)

        # Enable the "Use Nodes" option on the new material
        material.use_nodes = True

        # Get the material nodes
        nodes = material.node_tree.nodes

        # Get the "Principled BSDF" and "Material Output" nodes
        principled_bsdf_node = nodes.get('Principled BSDF')
        principled_bsdf_node.location = (principled_bsdf_node.location.x - 400, principled_bsdf_node.location.y - 70)

        material_output_node = nodes.get('Material Output')

        # Add a "Mix Shader" node below the "Principled BSDF"
        mix_shader_node = nodes.new('ShaderNodeMixShader')
        mix_shader_node.location = (material_output_node.location.x - 200, material_output_node.location.y)

        # Set the "Fac" slider value to 0
        mix_shader_node.inputs[0].default_value = 0.0

        # Add a "Transparent BSDF" node
        transparent_bsdf_node = nodes.new('ShaderNodeBsdfTransparent')
        transparent_bsdf_node.location = (mix_shader_node.location.x - 200, mix_shader_node.location.y)

        # Add an "Object Info" node
        object_info_node = nodes.new('ShaderNodeObjectInfo')
        object_info_node.location = (mix_shader_node.location.x - 200, mix_shader_node.location.y + 180)

        # Connect the nodes
        links = material.node_tree.links
        links.new(object_info_node.outputs['Alpha'], mix_shader_node.inputs[0])
        links.new(principled_bsdf_node.outputs['BSDF'], mix_shader_node.inputs[2])
        links.new(transparent_bsdf_node.outputs['BSDF'], mix_shader_node.inputs[1])
        links.new(mix_shader_node.outputs['Shader'], material_output_node.inputs['Surface'])

        # Rename the "Mix Shader" node to "Mix Alpha"
        mix_shader_node.name = "Mix Alpha"

        # Change the "Node Label" to "Mix Alpha"
        mix_shader_node.label = "Mix Alpha"

        # Add the material to the object
        obj.data.materials.append(material)

        # Check if the object is in edit mode
        if bpy.context.mode == 'EDIT_MESH':
            # Assign the material to the selected mesh part
            bpy.ops.object.material_slot_assign()

        # Select the slot of the new material
        obj.active_material_index = len(obj.data.materials) - 1
        bpy.ops.object.material_slot_assign()

❤️ 1 likes


🔗 View on Blender Artists