I am attempting to open models that have been created in 2.7 in 2.8. But they do not look correct in 2.8 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?
from:
to:
How might I go about programmatically finding all nodes connected to each material’s displacement input and replace them with normal map nodes?
Thanks
To answer this it appears to be a bug, because if the same object is opened instead of being appended then Blender will do the conversion and add the Displacement node automatically.
I have reported this issue on the developer’s site.
Also I have written a script that can be used to automatically fix the node setup for all materials.
FixDisplacementNodes.py
#!/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
for link in mat.node_tree.links: #loop over all node links for this material.
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
mat.node_tree.links.new(link.to_socket, dNode.outputs['Displacement']) #connect the Displacement node to the Material Output 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:
More info regarding this subject can be found on my StackExchange post