import bpy
from mathutils import Vector
rock_mat = bpy.data.materials.new(“GlowRock_1”)
rock_mat.use_nodes = True
nodes = {
‘Texture Coordinate’: [‘TEX_COORD’, (115.0, 341.0)],
‘Emission’: [‘EMISSION’, (210.0, 193.0)],
‘Diffuse BSDF’: [‘BSDF_DIFFUSE’, (114.0, 245.0)],
‘Mix Shader’: [‘MIX_SHADER’, (319.0, 310.0)],
‘Material Output’: [‘OUTPUT_MATERIAL’, (436.0, 309.0)],
‘Image Texture’: [‘TEX_IMAGE’, (377.0, 299.0)],
‘Add Shader’: [‘ADD_SHADER’, (212.0, 255.0)],
‘Translucent BSDF’: [‘BSDF_TRANSLUCENT’, (113.0, 184.0)]
}
for k, v in nodes.items():
location = Vector(v[1])
if not k in rock_mat.node_tree.nodes:
cur_node = rock_mat.node_tree.nodes.new(v[0])
cur_node.location = location
else:
rock_mat.node_tree.nodes[k].location = location
from_node, from_socket, to_node, to_socket
unfortunately Mix Shader has two input nodes called ‘Shader’
this means you have to assign to input index instead.
link00 = [‘Diffuse BSDF’,‘BSDF’,‘Add Shader’,‘Shader’]
link01 = [‘Translucent BSDF’,‘BSDF’,‘Add Shader’,‘Shader’]
link02 = [‘Add Shader’,‘Shader’,‘Mix Shader’,‘Shader’]
link03 = [‘Emission’,‘Emission’,‘Mix Shader’,‘Shader’]
link04 = [‘Texture Coordinate’,‘UV’,‘Image Texture’,‘Vector’]
link05 = [‘Image Texture’,‘Color’,‘Mix Shader’,‘Fac’]
link06 = [‘Mix Shader’,‘Shader’,‘Material Output’,‘Surface’]
links = [link00, link01, link02, link03, link04, link05, link06]
node_target = 1
for link in links:
from_node = rock_mat.node_tree.nodes[link[0]]
output_socket = from_node.outputs[link[1]]
to_node = rock_mat.node_tree.nodes[link[2]]
if link[0] in [‘Emission’, ‘Diffuse BSDF’]:
input_socket = to_node.inputs[node_target]
node_target += 1
else:
input_socket = to_node.inputs[link[3]]
my_mat.node_tree.links.new(output_socket, input_socket)
nodes = my_mat.node_tree.nodes
node = nodes[‘Emission’]
node.inputs[‘Color’].default_value = (0.0, 1.0, 1.0, 1.0)
node.inputs[‘Strength’].default_value = 0.5
node = nodes[‘Diffuse BSDF’]
node.inputs[‘Color’].default_value = (1.0, 0.0, 0.0, 1.0)
node.inputs[‘Roughness’].default_value = 0.0
node = nodes[‘Noise Texture’]
node.inputs[‘Scale’].default_value = 5.0
node.inputs[‘Detail’].default_value = 2.0
node.inputs[‘Distortion’].default_value = 0.0