Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bpy
- import os
- #What this does: Looks through a folder looking for textures named Something_C, Something_R, Something_N and sets up a material for each set using the Principled BSDF shader.
- # Steps to import material based on _C, _R, _N .png texture files, using Principled BSDF shader:
- # Input: Root folder of all materials
- # For each folder
- # For each file
- # If the file ends in _C.png:
- # Try to load texture file FileName[:6]_R.png - if failed, throw exception and continue.
- # Try to load texture file FileName[:6]_N.png - if failed, throw exception and continue.
- # # Load _C texture file. Now all necessary textures for the material are loaded.
- # Create a new material named FileName[:6], and in it:
- # Delete all nodes.
- # Add a Principled BSDF node
- # Create three texture nodes. (two with linear color space)("Non-Color Data")
- # Put the loaded textures into the right nodes
- # Link the nodes to the Principled BSDF
- # Create Material output node
- # Link the BSDF to Material Output
- # Done?
- ignoreBlueChannel = True
- texRootDir = "D:\\!Village\\GenericMaterials"
- grid = 400
- TexNodeWidth = 300
- for root, dirs, files in os.walk(texRootDir):
- for f in files:
- print(f)
- if(f.endswith("_C.png")): #If it's a BaseColor file
- matName = f.split("_C")[0]
- print("New material found:" +matName)
- if(matName not in bpy.data.materials):
- # Finding and loading textures (_C, _R and _N)
- color = bpy.data.images.load(os.path.join(root, f), check_existing=True)
- normal = ""
- rough = ""
- for f2 in files:
- if(f2.startswith(matName)):
- if(f2.endswith("_N.png")):
- normal = bpy.data.images.load(os.path.join(root, f2), check_existing=True)
- if(f2.endswith("_R.png")):
- rough = bpy.data.images.load(os.path.join(root, f2), check_existing=True)
- # Creating and setting up material
- bpy.data.materials.new(matName)
- mat = bpy.data.materials[matName]
- mat.use_fake_user = True # TODO: Set this to True
- # NODES
- mat.use_nodes = True
- nodes = mat.node_tree.nodes
- nodes.clear()
- texNodeColor = nodes.new(type="ShaderNodeTexImage")
- texNodeColor.name = "BaseColor"
- texNodeColor.label = "BaseColor"
- texNodeColor.width = TexNodeWidth
- texNodeColor.location = (-grid, grid*2)
- texNodeColor.image = color
- texNodeRough = nodes.new(type="ShaderNodeTexImage")
- texNodeRough.name = "Roughness"
- texNodeRough.label = "Roughness"
- texNodeRough.width = TexNodeWidth
- texNodeRough.location = (-grid, grid*1)
- if(rough is not ""):
- texNodeRough.image = rough
- texNodeRough.color_space = 'NONE'
- texNodeNormal = nodes.new(type="ShaderNodeTexImage")
- texNodeNormal.name = "Roughness"
- texNodeNormal.label = "Roughness"
- texNodeNormal.width = TexNodeWidth
- texNodeNormal.location = (-grid, 0)
- if(normal is not ""):
- texNodeNormal.image = normal
- texNodeNormal.color_space = 'NONE'
- nodeSeparateRGB = nodes.new(type="ShaderNodeSeparateRGB")
- nodeSeparateRGB.location = (0, 0)
- nodeSeparateRGB.hide = True
- nodeInvert = nodes.new(type="ShaderNodeInvert")
- nodeInvert.location = (grid*.25, 0)
- nodeInvert.hide = True
- nodeCombineRGB = nodes.new(type="ShaderNodeCombineRGB")
- nodeCombineRGB.location = (grid*.5, 0)
- nodeCombineRGB.hide = True
- nodeCombineRGB.inputs["B"].default_value = 1
- nodeNormalMap = nodes.new(type="ShaderNodeNormalMap")
- nodeNormalMap.location = (grid, 0)
- nodeNormalMap.hide = True
- nodePrincipledBSDF = nodes.new(type="ShaderNodeBsdfPrincipled")
- nodePrincipledBSDF.location = (grid*1.5, grid*1.5)
- nodeOutput = nodes.new(type="ShaderNodeOutputMaterial")
- nodeOutput.location = (grid*2, grid)
- #LINKS
- links = mat.node_tree.links
- links.new(texNodeColor.outputs['Color'], nodePrincipledBSDF.inputs["Base Color"])
- links.new(texNodeRough.outputs['Color'], nodePrincipledBSDF.inputs["Roughness"])
- #NORMAL LINKS
- links.new(texNodeNormal.outputs['Color'], nodeSeparateRGB.inputs["Image"])
- links.new(nodeSeparateRGB.outputs['R'], nodeCombineRGB.inputs["R"])
- links.new(nodeSeparateRGB.outputs['G'], nodeInvert.inputs["Color"])
- links.new(nodeInvert.outputs['Color'], nodeCombineRGB.inputs["G"])
- if(not ignoreBlueChannel):
- links.new(nodeSeparateRGB.outputs['B'], nodeCombineRGB.inputs["B"])
- links.new(nodeCombineRGB.outputs['Image'], nodeNormalMap.inputs["Color"])
- links.new(nodeNormalMap.outputs['Normal'], nodePrincipledBSDF.inputs["Normal"])
- links.new(nodePrincipledBSDF.outputs['BSDF'], nodeOutput.inputs["Surface"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement