materials.py: Only modify selected object(s), various code maintenance
See issue #9
This commit is contained in:
parent
9f4118c7b3
commit
b8a53fa65b
41
materials.py
41
materials.py
@ -1,24 +1,38 @@
|
|||||||
# Original script (interpolation): Techy5
|
# Original script: random-geek
|
||||||
# Expanded script: GreenXenith
|
# Contributors: GreenXenith, sbrl, VorTechnix
|
||||||
|
|
||||||
### Use better materials and support alpha ###
|
### Use better materials and support alpha ###
|
||||||
# Usage: Open or copy script in Blender
|
# Usage: Open or copy script in Blender
|
||||||
# Run script while object is selected
|
# Run script WHILE OBJECT IS SELECTED
|
||||||
|
|
||||||
import bpy
|
import bpy
|
||||||
|
|
||||||
for mat in bpy.data.materials:
|
targetMats = []
|
||||||
try:
|
|
||||||
|
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
|
nodes = mat.node_tree.nodes
|
||||||
links = mat.node_tree.links
|
links = mat.node_tree.links
|
||||||
|
|
||||||
# Remove all nodes except texture
|
# Remove all nodes except texture
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
if node.type != "TEX_IMAGE":
|
if node.bl_idname != "ShaderNodeTexImage":
|
||||||
nodes.remove(node)
|
nodes.remove(node)
|
||||||
|
|
||||||
# Change image interpolation
|
# Get texture node
|
||||||
|
try:
|
||||||
tex = nodes["Image Texture"]
|
tex = nodes["Image Texture"]
|
||||||
|
except KeyError:
|
||||||
|
print(f"[materials.py] Skipped material '{mat.name}': Image texture not found.")
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Change image interpolation
|
||||||
tex.interpolation = "Closest"
|
tex.interpolation = "Closest"
|
||||||
tex.location = 0, 0
|
tex.location = 0, 0
|
||||||
|
|
||||||
@ -40,11 +54,11 @@ for mat in bpy.data.materials:
|
|||||||
out.location = 600, 0
|
out.location = 600, 0
|
||||||
|
|
||||||
# Link everything
|
# Link everything
|
||||||
links.new(coord.outputs[2], map.inputs[0]) # Coord > Map
|
links.new(coord.outputs["UV"], map.inputs["Vector"])
|
||||||
links.new(map.outputs[0], tex.inputs[0]) # Map > Texture
|
links.new(map.outputs["Vector"], tex.inputs["Vector"])
|
||||||
links.new(tex.outputs[0], prin.inputs[0]) # Texture Color > Principled Color
|
links.new(tex.outputs["Color"], prin.inputs["Base Color"])
|
||||||
links.new(tex.outputs[1], prin.inputs[19]) # Texture Alpha > Principled Alpha
|
links.new(tex.outputs["Alpha"], prin.inputs["Alpha"])
|
||||||
links.new(prin.outputs[0], out.inputs[0]) # Principled > Output
|
links.new(prin.outputs["BSDF"], out.inputs["Surface"])
|
||||||
|
|
||||||
# Deselect all
|
# Deselect all
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
@ -52,6 +66,3 @@ for mat in bpy.data.materials:
|
|||||||
|
|
||||||
# Set blend mode
|
# Set blend mode
|
||||||
mat.blend_method = "CLIP"
|
mat.blend_method = "CLIP"
|
||||||
|
|
||||||
except:
|
|
||||||
continue
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user