I am attempting to create a displacement node with an attached image, and connect that to the Main Material Output Node.
I feel I understand how to create and connect these pieces (mostly) fine now. But I am unable to locate/reference the original Material Output, OR how to make a new Material Output the assigned one.
Please forgive my code, it is a hacked attempt between many forums posts and my own understanding after 1 week into python and blender.
Thank you in advance (code and pictures below)
Images:
Code
#Import python
import bpy
#Import additional refs
from bpy import context, data, ops
# Clear all nodes in a mat
def clear_material( material ):
if material.node_tree:
material.node_tree.links.clear()
material.node_tree.nodes.clear()
# Create a node corresponding to a defined group
def instanciate_group( nodes, group_name ):
group = nodes.new( type = 'ShaderNodeGroup' )
group.node_tree = bpy.data.node_groups[group_name]
#ref materials
materials = bpy.data.materials
#name material
mat_name = 'DELETE_THIS_CODE'
# get ref to material
material = materials.get( mat_name )
#if not our mat
if not material:
material = materials.new( mat_name )
# We clear it as we'll define it completely
clear_material( material )
material.use_nodes = True
nodes = material.node_tree.nodes
links = material.node_tree.links
output = nodes.new( type = 'ShaderNodeOutputMaterial' )
diffuse = nodes.new( type = 'ShaderNodeBsdfDiffuse' )
#input = nodes.new( type = 'ShaderNodeTexImage')
#With names
link = links.new( diffuse.outputs['BSDF'], output.inputs['Surface'])
#Or with indices
#link = links.new( diffuse.outputs[0], output.inputs[0] )
#______________________^^ multiple things above here might be deletable ^^_____________
#________________________________________________Adds img texture
#Create material named X
mat = bpy.data.materials.new(name="Mat_Tile")
#Set this material to use nodes
mat.use_nodes = True
#Add new Material output
#moNode = mat.node_tree.nodes.new('ShaderNodeOutputMaterial')
bsdf = mat.node_tree.nodes["Principled BSDF"]
texImage = mat.node_tree.nodes.new('ShaderNodeTexImage')
texImage.image = bpy.data.images.load("C:\\Users\\nmaestre\\Downloads\\Textures\\Downloaded\\Tile_00_Diffuse.png")
mat.node_tree.links.new(bsdf.inputs['Base Color'], texImage.outputs['Color'])
#reference selected object
ob = context.view_layer.objects.active
# Assign it to object
if ob.data.materials:
ob.data.materials[0] = mat
else:
ob.data.materials.append(mat)
#Add displace
dNode = mat.node_tree.nodes.new('ShaderNodeDisplacement')
#Make a normal map node
#nNode = mat.node_tree.nodes.new('ShaderNodeNormalMap')
#Displace Img
texImageNormal = mat.node_tree.nodes.new('ShaderNodeTexImage')
texImageNormal.image = bpy.data.images.load("C:\\Users\\nmaestre\\Downloads\\Textures\\Downloaded\\Tile_00_Normal.png")
#Attach imag to diplace node
mat.node_tree.links.new(texImageNormal.outputs['Color'], dNode.inputs['Normal'])
#connect the Displacement node to the Material Output node
mat.node_tree.links.new(dNode.outputs['Displacement'], bsdf.inputs['Normal'])
#__________________________Add unwrap
#uwrap node
tex_coord = nodes.new(type = 'ShaderNodeTexCoord')
#link to image texture
links.new(tex_coord.outputs["UV"], texImage.inputs["Vector"])