-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
materials.py: Only modify selected object(s), various code maintenance
See issue #9
- Loading branch information
1 parent
9f4118c
commit b8a53fa
Showing
1 changed file
with
51 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
try: | ||
nodes = mat.node_tree.nodes | ||
links = mat.node_tree.links | ||
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.type != "TEX_IMAGE": | ||
nodes.remove(node) | ||
# Remove all nodes except texture | ||
for node in nodes: | ||
if node.bl_idname != "ShaderNodeTexImage": | ||
nodes.remove(node) | ||
|
||
# Change image interpolation | ||
# Get texture node | ||
try: | ||
tex = nodes["Image Texture"] | ||
tex.interpolation = "Closest" | ||
tex.location = 0, 0 | ||
except KeyError: | ||
print(f"[materials.py] Skipped material '{mat.name}': Image texture not found.") | ||
continue | ||
|
||
# Create texture coordinate node | ||
coord = nodes.new("ShaderNodeTexCoord") | ||
coord.location = -400, 0 | ||
# Change image interpolation | ||
tex.interpolation = "Closest" | ||
tex.location = 0, 0 | ||
|
||
# Create mapping node | ||
map = nodes.new("ShaderNodeMapping") | ||
map.location = -200, 0 | ||
# Create texture coordinate node | ||
coord = nodes.new("ShaderNodeTexCoord") | ||
coord.location = -400, 0 | ||
|
||
# Create principled shader | ||
prin = nodes.new("ShaderNodeBsdfPrincipled") | ||
prin.location = 300, 0 | ||
prin.inputs["Specular"].default_value = 0 | ||
# Create mapping node | ||
map = nodes.new("ShaderNodeMapping") | ||
map.location = -200, 0 | ||
|
||
# Create output | ||
out = nodes.new("ShaderNodeOutputMaterial") | ||
out.location = 600, 0 | ||
# Create principled shader | ||
prin = nodes.new("ShaderNodeBsdfPrincipled") | ||
prin.location = 300, 0 | ||
prin.inputs["Specular"].default_value = 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 | ||
# Create output | ||
out = nodes.new("ShaderNodeOutputMaterial") | ||
out.location = 600, 0 | ||
|
||
# Deselect all | ||
for node in nodes: | ||
node.select = False | ||
# 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"]) | ||
|
||
# Set blend mode | ||
mat.blend_method = "CLIP" | ||
# Deselect all | ||
for node in nodes: | ||
node.select = False | ||
|
||
except: | ||
continue | ||
# Set blend mode | ||
mat.blend_method = "CLIP" |