fpete
fpete

Reputation: 113

Getting materials' and textures' names in Python

I'd like to print all materials' and it's textures' names with this script:

for ob in bpy.data.objects:
    if ob.type == "MESH":
        for mat_slot in ob.material_slots:
            if mat_slot.material:
                if mat_slot.material.node_tree:
                   print("material:" + str(mat_slot.material.node_tree.name))
                   for x in mat_slot.material.node_tree.nodes:
                        if x.type=='TEX_IMAGE':
                           print(" texture: "+str(x.name))
         

But the output is just general names like this:

material:Shader Nodetree
 texture: Image Texture
material:Shader Nodetree
 texture: Image Texture
material:Shader Nodetree
 texture: Image Texture      

So, how can I get the textures' file names like 'pic.png'?

Upvotes: 4

Views: 3645

Answers (1)

fpete
fpete

Reputation: 113

I figured it out:

for ob in bpy.data.objects:
    if ob.type == "MESH":
        for mat_slot in ob.material_slots:
            if mat_slot.material:
                if mat_slot.material.node_tree:
                   print("material:" + str(mat_slot.material.name))                
                   for x in mat_slot.material.node_tree.nodes:
                        if x.type=='TEX_IMAGE':
                           print(" texture: "+str(x.image.name))

This prints:

material:metal
 texture: metal.png
material:coppercoil
 texture: coil.png
material:steel
 texture: steel.png

Upvotes: 4

Related Questions