Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SuperFromND/9643163eefa2201122cdca0f648265ab to your computer and use it in GitHub Desktop.
Save SuperFromND/9643163eefa2201122cdca0f648265ab to your computer and use it in GitHub Desktop.
Blender Python: Normalize materials to Image Texture -> BSDF -> Output
# 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