I would like to get the name of of image of "Image Texture" like "Wood8" or "Wood8.png" below with python.
I tried to use object.material_slots[index].material
to get materials, and it work.
However, then I tried to use material.texture_slots.texture.name
to get it, it didn't work at all.
I have no idea what to do next?
2017/06/05 edited @TLousky
I tried to use
import bpy
import bmesh
'''
I would like to get all textures
'''
scene = bpy.context.scene
objects_in_scene = bpy.data.objects
texture_list = []
for obj in objects_in_scene :
# Enter EDIT mode of this object since bmesh only can be acess in EDIT mode
if obj.type == 'MESH':
scene.objects.active = obj
bpy.ops.object.mode_set(mode = 'EDIT')
me = obj.data
# Acess bmesh of this mesh
bm = bmesh.from_edit_mesh(me)
for f in bm.faces:
if len(obj.material_slots) != 0:
mat = obj.material_slots[f.material_index].material
# Check there is a material in this material slot
if mat is not None:
# go through all texture slots in the material slot
for mtext_slot in mat.texture_slots:
#print(mtext_slot)
# Check the texture slot is not None
if mtext_slot is not None:
text = mtext_slot.texture
# Check there is a texture in this texture slot
if text is not None:
# If the texture hasn't be counted ever, then Count it
if text.type == 'IMAGE':
if text.image.name not in tList:
tList.append(text.image.name)
print(text.image.name)
print(texture_list)`
to get all textures of all objects in the scene.
However, I got "[]" meaning nothing as feedback.
Then I added print(mtext_slot)
in line 24 to get what is in the material.texture_slots
And I got the following feedback
`<bpy_struct, MaterialTextureSlot("Tex")>
None
None
None
None
None
None
None
None
None
<bpy_struct, MaterialTextureSlot("Tex")>
None
None
None
None
None
...
It seems there was something wrong in my codes
objects_in_scene = scene.objects
is a list of objects in a scene, whereasbpy.data.objects
is all objects in a file, linked or unlinked to a scene. Test forobj.material_slots
, for length ( and hence the validity offace.material_index
), once per object, rather than on a per face basis. $\endgroup$