最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Blender Python Script: Musgrave Texture Not Showing on Material Node - Stack Overflow

programmeradmin0浏览0评论

I’m trying to create a plane in Blender(4.2.3) using Python and assign it a material with a Musgrave texture background. However, when I run the script, the Musgrave texture does not appear in the material as expected and I see a black background. Here's the function I’m using:


import bpy
import math

def CreatePlaneAndMaterial(collectionName, background_type):
    """
    Create a plane with a specified background material.

    Args:
        collectionName (str): The name of the collection to link the plane.
        background_type (str): The type of background to create.
    """
    # Remove existing plane if any
    plane_obj = bpy.data.objects.get('Plane')
    if plane_obj:
        bpy.data.objects.remove(plane_obj, do_unlink=True)

    # Create a plane and position it
    bpy.ops.mesh.primitive_plane_add(
        size=75, 
        enter_editmode=False, 
        align='WORLD', 
        location=(0, 4, 20), 
        rotation=(math.radians(90), 0, 0)
    )
    plane_obj = bpy.context.active_object  # Get the created plane object

    # Move plane to the specified collection
    if collectionName in bpy.data.collections:
        target_collection = bpy.data.collections[collectionName]
    else:
        target_collection = bpy.data.collections.new(collectionName)
        bpy.context.scene.collection.children.link(target_collection)

    # Unlink plane from the default collection and link to the target collection
    if plane_obj.name in bpy.context.scene.collection.objects:
        bpy.context.scene.collection.objects.unlink(plane_obj)
    target_collection.objects.link(plane_obj)

    # Create or get the material
    mat_name = f"Material_{background_type}"
    mat = bpy.data.materials.get(mat_name)
    if mat is None:
        mat = bpy.data.materials.new(name=mat_name)

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

    # Ensure material uses nodes
    mat.use_nodes = True

    # Clear existing nodes in the material
    nodes = mat.node_tree.nodes
    links = mat.node_tree.links
    nodes.clear()

    # Create a Material Output node
    output_node = nodes.new(type='ShaderNodeOutputMaterial')
    output_node.location = (400, 0)

    if background_type == "musgrave":
        # Musgrave (Noise Texture) background
        noise_node = nodes.new(type='ShaderNodeTexNoise')
        noise_node.location = (-300, 0)
        noise_node.inputs["Scale"].default_value = 2.0
        noise_node.inputs["Detail"].default_value = 9.0
        noise_node.inputs["Roughness"].default_value = 0.5

        # Add Color Ramp for adjustment
        color_ramp = nodes.new(type='ShaderNodeValToRGB')
        color_ramp.location = (-100, 0)
        links.new(noise_node.outputs['Fac'], color_ramp.inputs['Fac'])

        # Adjust Color Ramp
        color_ramp.color_ramp.interpolation = 'LINEAR'
        color_ramp.color_ramp.elements[0].position = 0.4
        color_ramp.color_ramp.elements[1].position = 0.6

        # Add Diffuse BSDF
        diffuse_bsdf = nodes.new(type='ShaderNodeBsdfDiffuse')
        diffuse_bsdf.location = (100, 0)
        links.new(color_ramp.outputs['Color'], diffuse_bsdf.inputs['Color'])

        # Connect Diffuse BSDF to Material Output
        links.new(diffuse_bsdf.outputs['BSDF'], output_node.inputs['Surface'])

    else:
        print(f"Unknown background type: {background_type}")


Expected Outcome: The plane should display a Musgrave texture with adjustable parameters like scale, detail, and roughness, as connected through the node tree.

发布评论

评论列表(0)

  1. 暂无评论