Can anyone Fix these Little Errors in My Script

I Just Have a Test Script Which Has Some Errors, As I Am Noob To Scripting I don’t Know How to Fix these errors

  1. This script creates a Test Material and a Panel in Physics Panel and a Button is Created according to me when this Button is Pressed a Material Should be created but this is created when script is run I don’t wan’t that.
  2. This Script Creates a Node Group but I am Unable to Connect that Node -group to Material Output Node.

My script is -

import bpy

#Context for Test Material
def T_M(context):
    bpy.context.scene.use_nodes = True
    tree = bpy.context.scene.node_tree 
    
    #removes unwanted nodes
    for node in tree.nodes:
        tree.nodes.remove(node) 

Testshader_mat = bpy.data.materials.new("TestMat")
mesh = bpy.context.object.data
mesh.materials.clear()
mesh.materials.append(Testshader_mat)
bpy.context.object.active_material.use_nodes = True
mat_name = "TestMat"
for mat in bpy.data.materials:
    if "TestMat" in mat.name:
        nodes = mat.node_tree.nodes
        for node in nodes:
            if node.type != 'OUTPUT_MATERIAL': # skip the material output node as we'll need it later
                nodes.remove(node) 
 
       
 
#Creating Node Group Test_Material


group = bpy.data.node_groups.new(type="ShaderNodeTree", name="Test_Material")


#Creating Group Input
group.inputs.new("NodeSocketColor", "Diffuse Color")
group.inputs.new("NodeSocketColor", "Glossy Color")
group.inputs.new("NodeSocketFloat", "Mix Factor")
group.inputs.new("NodeSocketFloat", "Glossyness")
input_node = group.nodes.new("NodeGroupInput")
input_node.location = (-800, 0)

# Creating Group Output Node
group.outputs.new("NodeSocketShader", "Diffuse Color")
group.outputs.new("NodeSocketShader", "Glossy Color")
group.outputs.new("NodeSocketShader", "Mix Output")


output_node = group.nodes.new("NodeGroupOutput")
output_node.location = (1500, 0)



#Creating Diffuse Node
Diffuse_node = group.nodes.new(type = 'ShaderNodeBsdfDiffuse')
Diffuse_node.location = (150, 100)

#Creating Glossy Node
Glossy_node = group.nodes.new(type = 'ShaderNodeBsdfGlossy')
Glossy_node.location = (300, 250)

#Creating Mix Shader Node
Mix_Shader_node = group.nodes.new(type = 'ShaderNodeMixShader')
Mix_Shader_node.location = (450,100)

# Creating Links Between Nodes
group.links.new(Diffuse_node.outputs["BSDF"], Mix_Shader_node.inputs[1])
group.links.new(Glossy_node.outputs["BSDF"], Mix_Shader_node.inputs[2])
group.links.new(input_node.outputs["Diffuse Color"], Diffuse_node.inputs[0])
group.links.new(input_node.outputs["Glossy Color"], Glossy_node.inputs[0])
group.links.new(input_node.outputs["Mix Factor"], Mix_Shader_node.inputs[0])
group.links.new(input_node.outputs["Glossyness"], Glossy_node.inputs[1])
group.links.new(output_node.inputs["Diffuse Color"], Diffuse_node.outputs[0])
group.links.new(output_node.inputs["Glossy Color"], Glossy_node.outputs[0])
group.links.new(output_node.inputs["Mix Output"], Mix_Shader_node.outputs[0])

# Putting Node Group to the node editor
tree = bpy.context.object.active_material.node_tree
group_node = tree.nodes.new("ShaderNodeGroup")
group_node.node_tree = group
group_node.location = (-40,300)
group_node.use_custom_color = True
group_node.color = (1,0.341,0.034)
group_node.width = (250)

#These Two Lines are Commented Because these Lines have some errors these line should connect material output to nodegroup.
#links = tree.links
    
#link = links.new(group_node.outputs[0], ShaderNodeOutputMaterial_node.inputs[1])


#operation to add Test Material Node   
class Test_MaterialOP(bpy.types.Operator):
    """Click this Button to Add Test Material."""
    bl_idname = "t_m.addnodes"
    bl_label = " Test Material"

    def execute(self, context):
        Test_MaterialOP(context)
        return {'FINISHED'}
    

    
        
    
    #operation for Panel
class LayoutPanel(bpy.types.Panel):
    bl_label = "Test Material Node"
    bl_idname = "SCENE_PT_layout"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "physics"
    

    def draw(self, context):
        layout = self.layout

        scene = context.scene
        
        layout.label(text="_______________________________________________")
        
        #Welcome text
        
        row = layout.row()
        layout.label(text=" Click on Test Material Button,")
        layout.label(text=" and the required nodes will be added to the")
        layout.label(text=" Node Editor in the form of Test Material")
        layout.label(text=" Node Group.")
        row = layout.row()
        row = layout.row()
        layout.label(text="________________________________________________")
        
        #Test Material Add UI
        
        row = layout.row()
        row = layout.row()
        row = layout.row()
        
        layout.label(text=" Test Material", icon= 'MATERIAL_DATA')
        row = layout.row()
        row = layout.row()
        row = layout.row()
        layout.label(text=" This Button will Add a Material")
        layout.label(text=" For Your Object.")
        row = layout.row()
        row = layout.row()
        row = layout.row()
        row.operator("t_m.addnodes", icon= 'IMPORT')
        row = layout.row()
        layout.label(text="_______________________________________________")
        
    #register and unregister
def register():
    bpy.utils.register_class(Test_MaterialOP)
    bpy.utils.register_class(LayoutPanel)
    
    


def unregister():
    
    bpy.utils.unregister_class(Test_MaterialOP)
    bpy.utils.unregister_class(LayoutPanel)
    
    
    


if __name__ == "__main__":
    register()


Please Help me by Fixing Errors in this Script

Sorry but, no, that’s not how it works ^^
Here you can ask for a specific question like “Why is this line failing ?” but asking for us to debug it, first it’s not our job, then it won’t help you at all…

Are you ? Because your script is 170 lines long :stuck_out_tongue:

So, if you want to improve your coding skills :

  • Start with something simple that works. (like an example from the available templates, for example)
  • Then add a few lines to add a feature
  • Test it again. If it works great, if it fails, it must come from these new lines, and then you can debug it, and continue, and so on.
  • If you can’t manage to fix a specific bug for a specific new feature, then you can ask on a forum like BA and people like me would help you with pleasure :slight_smile:

But coding a 170 lines script and then doing testing is not the right way when you are a beginner, trust me.

See you :slight_smile: ++
Tricotou

@tricotou

Yeah, not buying the noob thing either. Can add and link a bunch of nodes and create a node group ,but can’t figure out how to link to output node.

You are True But When i try to connect group node to material output node blender throws an error that cannot find ShaderNodeOutputMaterial_node
I think blender is trying to find it(Material Output node) inside the node group that’s why it is not finding material output node or it may be other reason but i can’t figure it out,
at this point i am stuck so i commented these lines out and when i run the script the node group is created successfully but it’s not connected to material output as i don’t know what is the error at this point.

And the other error is here

I don’t Know what is the error that’s why I asked for the help

@R_animation_studios

#These Two Lines are Commented Because these Lines have some errors these line should connect material output to nodegroup.
ShaderNodeOutputMaterial_node = tree.nodes["Material Output"]
links = tree.links    
link = links.new(group_node.outputs[0], ShaderNodeOutputMaterial_node.inputs[0])