简体   繁体   中英

Connecting Nodes in blender | python

I've been trying to figure out for a while now how to connect 2 shader nodes for the material I'm making in a blender, Been googling all over but I can't seem to wrap my head around how to connect them; The last 2 lines of code below are my best attempts. Hopefully, someone can see through this object madness as I cannot.

class WM_OT_textOpBasic(bpy.types.Operator):
    """Creates the Base Planet"""
    bl_idname = "wm.textopbasic"
    bl_label = "                            Text Tool Operator"

        
    def execute(self, context):
        
        bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=6, radius=1.0, calc_uvs=True, enter_editmode=False, align='WORLD', location=(0.0, 0.0, 0.0), rotation=(0.0, 0.0, 0.0), scale=(1.0, 1.0, 1.0))
        planet = bpy.context.selected_objects[0]
        planet.name = "Planet"
        planet_material = bpy.data.materials.get("planet material")
        
        if planet_material is None:
            # create material
            planet_material = bpy.data.materials.new(name="planet material")
        planet.data.materials.append(planet_material)
        
        planet_material.use_nodes = True
        nodes = planet_material.node_tree.nodes
        ColorRamp1 = nodes.new('ShaderNodeValToRGB')
        ColorRamp1.location = -400,100
        ColorRamp2 = nodes.new('ShaderNodeValToRGB')
        ColorRamp2.location = -700,100
        ColorRamp3 = nodes.new('ShaderNodeValToRGB')
        ColorRamp3.location = -1000,100
        
        Noise1 = nodes.new('ShaderNodeTexNoise')
        Noise1.location = -1100,300
        Noise2 = nodes.new('ShaderNodeTexNoise')
        Noise2.location = -900,300
        
        Bump = nodes.new('ShaderNodeBump')
        Bump.location = -150,-150
    
        
        planet.active_material.node_tree.links.new(Noise1.outputs[0],Noise2.inputs[1])
        
        planet_material.node_tree.links(Noise1.outputs[0],Noise2.inputs[1])
        ```

Your question doesn't specify the exact configuration of the nodes but this code shows a bit of how it works

import bpy

bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=6, radius=1.0, calc_uvs=True, enter_editmode=False, align='WORLD', location=(0.0, 0.0, 0.0), rotation=(0.0, 0.0, 0.0), scale=(1.0, 1.0, 1.0))
planet = bpy.context.selected_objects[0]
planet.name = "Planet"
planet_material = bpy.data.materials.get("planet material")

if planet_material is None:
    # create material
    planet_material = bpy.data.materials.new(name="planet material")
planet.data.materials.append(planet_material)

planet_material.use_nodes = True

if planet_material.node_tree:
    planet_material.node_tree.links.clear()
    planet_material.node_tree.nodes.clear()

nodes = planet_material.node_tree.nodes
links = planet_material.node_tree.links

ColorRamp1 = nodes.new('ShaderNodeValToRGB')
ColorRamp1.location = -400,100

Noise1 = nodes.new('ShaderNodeTexNoise')
Noise1.location = -1100,300

output = nodes.new(type='ShaderNodeOutputMaterial') # you need an output node to display

links.new(Noise1.outputs[0], ColorRamp1.inputs[0]) # Noise1.output[0] just takes the start node of noise one at spot 0, ColorRamp1.inputs[0] is the input spot for the noise again at spot 0
links.new(Noise1.outputs[0], output.inputs[1])
links.new(ColorRamp1.outputs[0], output.inputs[1])
planet.data.materials.append(planet_material)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM