Skip to content

Instantly share code, notes, and snippets.

@amelkor
Last active November 2, 2022 16:11
Show Gist options
  • Select an option

  • Save amelkor/b18bcb7e169822c5cda2bbd5a40fa825 to your computer and use it in GitHub Desktop.

Select an option

Save amelkor/b18bcb7e169822c5cda2bbd5a40fa825 to your computer and use it in GitHub Desktop.
This script for Blender 3.0+ parses a directory with textures and creates materials with the corresponding textures assigned
import bpy
import os
textures_dir_path = 'D:/Textures'
use_fake_user = True
def run():
files = list_materials()
for file in files:
material = bpy.data.materials.new(file.split('.')[0])
material.use_fake_user = use_fake_user
imgpath = textures_dir_path + '/' + file
image = bpy.data.images.load(imgpath)
material.use_nodes = True
material.specular_intensity = 0
texname = material.name
principled_BSDF = material.node_tree.nodes.get('Principled BSDF')
tex_node = material.node_tree.nodes.new('ShaderNodeTexImage')
tex_node.image = image
tex_node.interpolation = 'Closest'
material.node_tree.links.new(tex_node.outputs[0], principled_BSDF.inputs[0])
texture = bpy.data.textures.new(texname, type='IMAGE')
texture.use_fake_user = use_fake_user
texture.image = image
# =====================================
def list_materials():
matches = []
for root, dirnames, filenames in os.walk(textures_dir_path):
for filename in filenames:
if filename.endswith(('.png')):
matches.append(filename)
return matches
# =====================================
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment