From b8a53fa65b2569bf5ce7b1e26b1ef4fd2fb520a5 Mon Sep 17 00:00:00 2001 From: random-geek <35757396+random-geek@users.noreply.github.com> Date: Thu, 30 Dec 2021 22:11:57 -0800 Subject: [PATCH] materials.py: Only modify selected object(s), various code maintenance See issue #9 --- materials.py | 107 ++++++++++++++++++++++++++++----------------------- 1 file changed, 59 insertions(+), 48 deletions(-) diff --git a/materials.py b/materials.py index 7d78391..68ca6c6 100644 --- a/materials.py +++ b/materials.py @@ -1,57 +1,68 @@ -# Original script (interpolation): Techy5 -# Expanded script: GreenXenith +# Original script: random-geek +# Contributors: GreenXenith, sbrl, VorTechnix ### Use better materials and support alpha ### # Usage: Open or copy script in Blender -# Run script while object is selected +# Run script WHILE OBJECT IS SELECTED import bpy -for mat in bpy.data.materials: +targetMats = [] + +for obj in bpy.context.selected_objects: + for slot in obj.material_slots: + mat = slot.material + + if mat not in targetMats: + targetMats.append(mat) + +for mat in targetMats: + nodes = mat.node_tree.nodes + links = mat.node_tree.links + + # Remove all nodes except texture + for node in nodes: + if node.bl_idname != "ShaderNodeTexImage": + nodes.remove(node) + + # Get texture node try: - nodes = mat.node_tree.nodes - links = mat.node_tree.links - - # Remove all nodes except texture - for node in nodes: - if node.type != "TEX_IMAGE": - nodes.remove(node) - - # Change image interpolation tex = nodes["Image Texture"] - tex.interpolation = "Closest" - tex.location = 0, 0 - - # Create texture coordinate node - coord = nodes.new("ShaderNodeTexCoord") - coord.location = -400, 0 - - # Create mapping node - map = nodes.new("ShaderNodeMapping") - map.location = -200, 0 - - # Create principled shader - prin = nodes.new("ShaderNodeBsdfPrincipled") - prin.location = 300, 0 - prin.inputs["Specular"].default_value = 0 - - # Create output - out = nodes.new("ShaderNodeOutputMaterial") - out.location = 600, 0 - - # Link everything - links.new(coord.outputs[2], map.inputs[0]) # Coord > Map - links.new(map.outputs[0], tex.inputs[0]) # Map > Texture - links.new(tex.outputs[0], prin.inputs[0]) # Texture Color > Principled Color - links.new(tex.outputs[1], prin.inputs[19]) # Texture Alpha > Principled Alpha - links.new(prin.outputs[0], out.inputs[0]) # Principled > Output - - # Deselect all - for node in nodes: - node.select = False - - # Set blend mode - mat.blend_method = "CLIP" - - except: + except KeyError: + print(f"[materials.py] Skipped material '{mat.name}': Image texture not found.") continue + + # Change image interpolation + tex.interpolation = "Closest" + tex.location = 0, 0 + + # Create texture coordinate node + coord = nodes.new("ShaderNodeTexCoord") + coord.location = -400, 0 + + # Create mapping node + map = nodes.new("ShaderNodeMapping") + map.location = -200, 0 + + # Create principled shader + prin = nodes.new("ShaderNodeBsdfPrincipled") + prin.location = 300, 0 + prin.inputs["Specular"].default_value = 0 + + # Create output + out = nodes.new("ShaderNodeOutputMaterial") + out.location = 600, 0 + + # Link everything + links.new(coord.outputs["UV"], map.inputs["Vector"]) + links.new(map.outputs["Vector"], tex.inputs["Vector"]) + links.new(tex.outputs["Color"], prin.inputs["Base Color"]) + links.new(tex.outputs["Alpha"], prin.inputs["Alpha"]) + links.new(prin.outputs["BSDF"], out.inputs["Surface"]) + + # Deselect all + for node in nodes: + node.select = False + + # Set blend mode + mat.blend_method = "CLIP"