[Python] I can't clear DEFAULT nodes in Blender it ALWAYS appears

Discussão em 'Python' iniciado por Stack, Março 11, 2025.

  1. Stack

    Stack Membro Participativo

    Here's my function, It basically takes the textures of MINECRAFT blocks and links it to the material output directly.

    Whenever I run the code it generates this boilerplate (Texture Coordinate > Mapping > Image Texture > Principled BSDF > Material Output)

    I don't want that!, I want (Image Textures > Material Output) directly.

    I'm using **blender 4.3 LTS**

    [​IMG]

    Here's my CODE.

    def create_block(context, position, texture_path):
    # Create cube
    bpy.ops.mesh.primitive_cube_add(size=block_size, enter_editmode=False, align='WORLD', location=position)
    block = context.active_object
    block.name = f"minecraft_block_{os.path.basename(texture_path).split('.')[0]}"

    # Create a new material
    mat_name = f"minecraft_mat_{os.path.basename(texture_path).split('.')[0]}"
    mat = bpy.data.materials.new(name=mat_name)
    mat.use_nodes = True

    # Clear all default nodes (including Principled BSDF)
    nodes = mat.node_tree.nodes
    links = mat.node_tree.links
    nodes.clear() # This removes ALL nodes, including the default Principled BSDF

    # Create ONLY an Image Texture node and a Material Output node
    tex = nodes.new(type='ShaderNodeTexImage')
    tex.location = (0, 0)

    output = nodes.new(type='ShaderNodeOutputMaterial')
    output.location = (300, 0)

    # Load the texture
    try:
    tex.image = bpy.data.images.load(texture_path, check_existing=True)
    except Exception as e:
    print(f"Failed to load texture: {texture_path}, Error: {e}")

    # Connect the Image Texture directly to the Material Output
    links.new(tex.outputs["Color"], output.inputs["Surface"])

    # Assign the material to the block
    if block.data.materials:
    block.data.materials[0] = mat
    else:
    block.data.materials.append(mat)

    # Fix UV mapping using "Project from View (Bounds)"
    bpy.ops.object.mode_set(mode='EDIT')

    # Store the current view
    original_view = context.area.spaces.active.region_3d.view_perspective

    # Switch to orthographic view for proper projection
    context.area.spaces.active.region_3d.view_perspective = 'ORTHO'

    # Select all faces
    bpy.ops.mesh.select_all(action='SELECT')

    # Project UVs for each face individually
    for view in ['FRONT', 'BACK', 'LEFT', 'RIGHT', 'TOP', 'BOTTOM']:
    # Set the view
    bpy.ops.view3d.view_axis(type=view)
    # Project UVs from this view with bounds
    bpy.ops.uv.project_from_view(scale_to_bounds=True)

    # Restore the original view
    context.area.spaces.active.region_3d.view_perspective = original_view

    # Return to object mode
    bpy.ops.object.mode_set(mode='OBJECT')

    return block

    Continue reading...

Compartilhe esta Página