Advertisement
Guest User

Import TextureSets as Material

a guest
Sep 2nd, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.67 KB | None | 0 0
  1. import bpy
  2. import os
  3.  
  4. #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.
  5.  
  6. # Steps to import material based on _C, _R, _N .png texture files, using Principled BSDF shader:
  7.  
  8. # Input: Root folder of all materials
  9. # For each folder
  10. #   For each file
  11. #       If the file ends in _C.png:
  12. #           Try to load texture file FileName[:6]_R.png - if failed, throw exception and continue.
  13. #           Try to load texture file FileName[:6]_N.png - if failed, throw exception and continue.
  14. #           # Load _C texture file. Now all necessary textures for the material are loaded.
  15.  
  16. #           Create a new material named FileName[:6], and in it:
  17.  
  18. #           Delete all nodes.
  19. #           Add a Principled BSDF node
  20. #           Create three texture nodes. (two with linear color space)("Non-Color Data")
  21. #           Put the loaded textures into the right nodes
  22. #           Link the nodes to the Principled BSDF
  23. #           Create Material output node
  24. #           Link the BSDF to Material Output
  25. #           Done?
  26.  
  27. ignoreBlueChannel = True
  28. texRootDir = "D:\\!Village\\GenericMaterials"
  29. grid = 400
  30. TexNodeWidth = 300
  31.  
  32. for root, dirs, files in os.walk(texRootDir):
  33.     for f in files:
  34.         print(f)
  35.         if(f.endswith("_C.png")): #If it's a BaseColor file
  36.             matName = f.split("_C")[0]
  37.             print("New material found:" +matName)
  38.             if(matName not in bpy.data.materials):
  39.                
  40.                 # Finding and loading textures (_C, _R and _N)
  41.                 color = bpy.data.images.load(os.path.join(root, f), check_existing=True)
  42.                 normal = ""
  43.                 rough = ""
  44.                 for f2 in files:
  45.                     if(f2.startswith(matName)):
  46.                         if(f2.endswith("_N.png")):
  47.                             normal = bpy.data.images.load(os.path.join(root, f2), check_existing=True)
  48.                         if(f2.endswith("_R.png")):
  49.                             rough = bpy.data.images.load(os.path.join(root, f2), check_existing=True)
  50.                
  51.                 # Creating and setting up material
  52.                 bpy.data.materials.new(matName)
  53.                 mat = bpy.data.materials[matName]
  54.                 mat.use_fake_user = True    # TODO: Set this to True
  55.                
  56.                 # NODES
  57.                 mat.use_nodes = True
  58.                 nodes = mat.node_tree.nodes
  59.                 nodes.clear()
  60.                
  61.                 texNodeColor = nodes.new(type="ShaderNodeTexImage")
  62.                 texNodeColor.name = "BaseColor"
  63.                 texNodeColor.label = "BaseColor"
  64.                 texNodeColor.width = TexNodeWidth
  65.                 texNodeColor.location = (-grid, grid*2)
  66.                 texNodeColor.image = color
  67.                
  68.                 texNodeRough = nodes.new(type="ShaderNodeTexImage")
  69.                 texNodeRough.name = "Roughness"
  70.                 texNodeRough.label = "Roughness"
  71.                 texNodeRough.width = TexNodeWidth
  72.                 texNodeRough.location = (-grid, grid*1)
  73.                 if(rough is not ""):
  74.                     texNodeRough.image = rough
  75.                 texNodeRough.color_space = 'NONE'
  76.                
  77.                 texNodeNormal = nodes.new(type="ShaderNodeTexImage")
  78.                 texNodeNormal.name = "Roughness"
  79.                 texNodeNormal.label = "Roughness"
  80.                 texNodeNormal.width = TexNodeWidth
  81.                 texNodeNormal.location = (-grid, 0)
  82.                 if(normal is not ""):
  83.                     texNodeNormal.image = normal
  84.                 texNodeNormal.color_space = 'NONE'
  85.                
  86.                 nodeSeparateRGB = nodes.new(type="ShaderNodeSeparateRGB")
  87.                 nodeSeparateRGB.location = (0, 0)
  88.                 nodeSeparateRGB.hide = True
  89.                
  90.                 nodeInvert = nodes.new(type="ShaderNodeInvert")
  91.                 nodeInvert.location = (grid*.25, 0)
  92.                 nodeInvert.hide = True
  93.                
  94.                 nodeCombineRGB = nodes.new(type="ShaderNodeCombineRGB")
  95.                 nodeCombineRGB.location = (grid*.5, 0)
  96.                 nodeCombineRGB.hide = True
  97.                 nodeCombineRGB.inputs["B"].default_value = 1
  98.                
  99.                 nodeNormalMap = nodes.new(type="ShaderNodeNormalMap")
  100.                 nodeNormalMap.location = (grid, 0)
  101.                 nodeNormalMap.hide = True
  102.                
  103.                 nodePrincipledBSDF = nodes.new(type="ShaderNodeBsdfPrincipled")
  104.                 nodePrincipledBSDF.location = (grid*1.5, grid*1.5)
  105.                
  106.                 nodeOutput = nodes.new(type="ShaderNodeOutputMaterial")
  107.                 nodeOutput.location = (grid*2, grid)
  108.                
  109.                 #LINKS
  110.                 links = mat.node_tree.links
  111.                 links.new(texNodeColor.outputs['Color'], nodePrincipledBSDF.inputs["Base Color"])
  112.                 links.new(texNodeRough.outputs['Color'], nodePrincipledBSDF.inputs["Roughness"])
  113.                 #NORMAL LINKS
  114.                 links.new(texNodeNormal.outputs['Color'], nodeSeparateRGB.inputs["Image"])
  115.                 links.new(nodeSeparateRGB.outputs['R'], nodeCombineRGB.inputs["R"])
  116.                 links.new(nodeSeparateRGB.outputs['G'], nodeInvert.inputs["Color"])
  117.                 links.new(nodeInvert.outputs['Color'], nodeCombineRGB.inputs["G"])
  118.                 if(not ignoreBlueChannel):
  119.                     links.new(nodeSeparateRGB.outputs['B'], nodeCombineRGB.inputs["B"])
  120.                 links.new(nodeCombineRGB.outputs['Image'], nodeNormalMap.inputs["Color"])
  121.                 links.new(nodeNormalMap.outputs['Normal'], nodePrincipledBSDF.inputs["Normal"])
  122.                
  123.                 links.new(nodePrincipledBSDF.outputs['BSDF'], nodeOutput.inputs["Surface"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement