Same texture file name for same material name

Hello

I have a cube with 6 materials named from 1 to 6, then i have 6 textures named from 1 to 6, need to assign them

If anyone can help me with a python script here please, the simple example is only 6 material & i have quite a few more!:spin:!

Thanks !

You don’t even tell which rendering engine should use the materials. Cycles and Blender Internal materials are construted totally differently.

Hi helluvamesh, blender 2.79b and Blender Render,

I see your point, both codes can help for different productions isn’t that right ? but then i use Blender Render for now.

Thanks

Since you told (PM-ed) me that you use Blender Internal, here’s a short script that works on the active object:

If the images are in the blend file:

 
count = 6

import bpy

obj = bpy.context.object

for n in range (1, count+1):
    slot = obj.data.materials[str(n)].texture_slots.add ()
    texture = bpy.data.textures.new (name=str(n), type='IMAGE')
    texture.image = bpy.data.images[str(n)]
    slot.texture = texture

If you need to load them first:

 
count = 6
folder = 'D:/folder/'
extension = 'png'

import bpy

obj = bpy.context.object

for n in range (1, count+1):
    image = bpy.data.images.new (name=str(n), width=4, height=4)
    image.source = 'FILE'
    image.filepath = folder + str(n)+'.'+extension
    slot = obj.data.materials[str(n)].texture_slots.add ()
    texture = bpy.data.textures.new (name=str(n), type='IMAGE')
    texture.image = bpy.data.images[str(n)]
    slot.texture = texture

1 Like

Hello helluvamesh

The 2nd code works perfect with the cube example,

Then if i have an unique string for every material Name and not a number;
example: red.png, blue.png, brown.png … etc

Thank you

names = []

replace the line:
for n in range…
with:
for n in names:

Thank @helluvamesh for the help
:beers: :beers: :beers: