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
- 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.
- 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