# Find any node connected to a material's displacement input and replace it with a normal map node?

I am attempting to open models that have been created in 2.7 in 2.8. But I am having some trouble with the nodes not looking correct and and final results in 2.8 are not satisfactory. And I must modify the node setups for each material, for each model..

I was wondering if someone could help me with some Python to loop through each material and look to see if there are any 'RBG to BW' nodes connected to the material's displacement input, if so then replace it with a 'normal map' nodes?

For example, from: to:

I am uncertain how to find the nodes, nor how to efficiently replace one programmatically much less the one specifically connected to the displacement input.

If anyone has any suggestions on how I could acomplish this it would help tremendously!

And thanks!!

• I can't replicate your behaviour by simply saving in 2.79a (where Displacement was a scalar socket) and re-opening in 2.8 (where Displacement is a Vector) - for me Blender automatically adds a Displacement node just before the Displacement socket. This automatically converts between the 'old' and the 'new' behaviour. Even if you're getting such issues (are you opening the Blend file in 2.8 or simply importing/appending the material from within 2.8 from another file?) you should be adding a Displacement node, not a Normal Map node as the Normal will not produce the correct result. – Rich Sedman Mar 8 at 22:53
• You precisely identified the issue, there is a discrepancy between opening and appending models from 2.79 into 2.8, as it treats the two scenarios differently. Thus my automation script are failing because they attempt to append from external .blend files. Is this a bug? How should I approach a solution? – Logic1 Mar 10 at 2:30
• It would appear to be a bug. You could raise it at blender.org/get-involved/developers – Rich Sedman Mar 10 at 6:00
• Reported: developer.blender.org/T62415 – Logic1 Mar 10 at 9:23
• Looks like this was just elevated to an "Open, Confirmed, Medium" bug. – Logic1 Mar 13 at 22:12

I have done some research on how to access Blender's material nodes via Python and ended up writing a solution which address my original issue, I will post the code to serve as a possible answer.

However I am not marking this as the best answer because as Rich Sedman pointed out, Blender 2.8 already fixes the node setups when opening a .blend file. So maybe in some future release of Blender it too will address the nodes when linking and appending from external .blend files.

In the time being I must use this script in my custom import and automation scripts to fix the shaders after all models have been appended.

FixDisplacementNodes.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = "Grant Olsen (2019)"
__credits__ = ["Rich Sedman", "batFNGER"]
__version__ = "1.0.0"

import bpy

"""Given a material this will search for any RBG to BW nodes that are connected to a Material Output's Displacement input.
If the RBG to BW node exists then this will create the proper Normalmap to Displacenment map node setup and replace the
RGB to BW node with them by linking the nNode to the dNode, then from the Image texture to the nNode, and the dNode
connects to the Material Output node. Finally it deletes the disconnected RGB to BW node.
"""

def FixDisplacementNodes(mat):
"""
Explanation: this function takes one arguments: mat.
mat is type bpy.types.Material and is the material for the object
to be checked for any RBG to BW nodes connected to the Material Output node.
Will replace with proper normal map node setup.

The return type is None.

"""
if mat is None:# an object can have an empty material slot where slot.material is None
return #nothing to process

if type(link.to_node) is bpy.types.ShaderNodeOutputMaterial: #check if this node is connected to the type: "Output Material" node
if link.to_socket.identifier == 'Displacement': #check if this node is connected to the "Displacemnent" input
if type(link.from_node) is bpy.types.ShaderNodeRGBToBW: #finally, is the child node an "RGB to BW" type node?

bwNode = link.from_node #store the RGB to BW node for deletion later

dNode = mat.node_tree.nodes.new('ShaderNodeDisplacement') #newly created Displacement Node
nNode = mat.node_tree.nodes.new('ShaderNodeNormalMap') #newly created NormalMap Node

mat.node_tree.links.new(dNode.inputs['Normal'], nNode.outputs['Normal']) #connect the Normal Map node to the Displacement node

mat.node_tree.nodes.remove(bwNode) #delete the unlinked RGB to BW node

"""Loop through all objects in the scene, and for each object's material slot, passing it to FixDisplacementNodes.
"""
if __name__ == '__main__':

for mat in bpy.data.materials: #get list of materials, will save fixing same material for each object
FixDisplacementNodes(mat)

#for ob in bpy.data.objects: #get list of objects
#   for slot in ob.material_slots:
#       FixDisplacementNodes(slot.material)


Result:

[EDIT]

I am not sure which way is more preferred (if there is even much difference) but here is a variation which exactly replicates the way 2.8 converts the shaders when opening a .blend file (notice how it keeps the RGB to BW node):

FixDisplacementNodes.py (v2)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = "Grant Olsen (2019)"
__credits__ = ["Rich Sedman", "batFinger"]
__version__ = "2.0.0"

import bpy

"""Given a material this will search for any RBG to BW nodes that are connected to a Material Output's Displacement input.
If the RBG to BW node exists then this will create the proper Displacenment map node setup and link it to the
RGB to BW node.
"""
def FixDisplacementNodes(mat):
"""
Explanation: this function takes one arguments: mat.
mat is type bpy.types.Material and is the material for the object
to be checked for any RBG to BW nodes connected to the Material Output node.
Will replace with proper normal map node setup.

The return type is None.

"""
if mat is None:# an object can have an empty material slot where slot.material is None
return #nothing to process

if type(link.to_node) is bpy.types.ShaderNodeOutputMaterial: #check if this node is connected to the type: "Output Material" node
if link.to_socket.identifier == 'Displacement': #check if this node is connected to the "Displacemnent" input
if type(link.from_node) is bpy.types.ShaderNodeRGBToBW: #finally, is the child node an "RGB to BW" type node?

bwNode = link.from_node #the RGB to BW node

dNode = mat.node_tree.nodes.new('ShaderNodeDisplacement') #newly created Displacement Node

mat.node_tree.links.new(dNode.inputs['Height'], bwNode.outputs['Val']) #connect the RGB to BW node to the Displacement node

"""Loop through all objects in the scene, and for each object's material slot, passing it to Check_Material.
"""
if __name__ == '__main__':

for mat in bpy.data.materials: #get list of materials, will save fixing same material for each object
FixDisplacementNodes(mat)

#for ob in bpy.data.objects: #get list of objects
#   for slot in ob.material_slots:
#       FixDisplacementNodes(slot.material)


Result:

• Glad you've managed to resolve this - and well written answer. You could try registering it as a bug at blender.org/get-involved/developers. – Rich Sedman Mar 10 at 5:58
• Consider using for mat in bpy.data.materials: Will save fixing same material for each object it's on, and an objject can have an empty material slot where slot.material is None – batFINGER Mar 10 at 7:27