Created
April 6, 2024 20:02
-
-
Save SuperFromND/9643163eefa2201122cdca0f648265ab to your computer and use it in GitHub Desktop.
Blender Python: Normalize materials to Image Texture -> BSDF -> Output
This file contains hidden or 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
# Script by SuperFromND. | |
# This was written to partially clean up exported model data from ModNao, a Sega Dreamcast model ripping website. | |
# License: CC0. Go nuts! | |
import bpy | |
for obj in bpy.data.objects: | |
if obj.type == 'MESH' and not obj.active_material == None: | |
for item in obj.material_slots: | |
mat = bpy.data.materials[item.name] | |
if mat.use_nodes: | |
mat.blend_method = 'CLIP' | |
# first pass to just remove anything that's not necessary, i.e. we keep images and material out ONLY | |
for node in mat.node_tree.nodes: | |
if (node.type == 'TEX_IMAGE' or node.type == 'OUTPUT_MATERIAL'): | |
# do nothing, but we keep this node | |
pass | |
else: | |
mat.node_tree.nodes.remove(node) | |
# second pass to re-assemble the nodes into a normal material | |
pbsdf = mat.node_tree.nodes.new("ShaderNodeBsdfPrincipled") | |
matoutput = mat.node_tree.nodes.get("Material Output") | |
for node in mat.node_tree.nodes: | |
if node.type == 'TEX_IMAGE': | |
links = mat.node_tree.links | |
links.new(node.outputs[0], pbsdf.inputs["Base Color"]) | |
links.new(node.outputs[1], pbsdf.inputs["Alpha"]) | |
links.new(pbsdf.outputs["BSDF"], matoutput.inputs["Surface"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment