1
$\begingroup$

I have a project with multiple mesh objects and each mesh has multiple materials with textures. Some have 1 and other have 10 used textures and more.

How can I please export a TXT/CSV list containing information about all meshes materials? All mesh with all material names and all texture names?

I have seen a lot of python APIs for everything possible, but can't find one for this. Thank you for any help. Best regards Miro

$\endgroup$

1 Answer 1

2
$\begingroup$
filepath = r"E:/output.csv"

import bpy
from bpy import data as D

with open(filepath, 'w') as f:
    for ob in D.objects:
#        if ob.type != 'MESH': continue
        names = [s.material.name for s in ob.material_slots if s.material]
        print(ob.name, *names, sep=",", file=f)

If you really want to save meshes (which is not the same as objects) materials:

filepath = r"E:/output.csv"

import bpy
from bpy import data as D

with open(filepath, 'w') as f:
    for me in D.meshes:
        names = [m.name for m in me.materials if m]
        print(me.name, *names, sep=",", file=f)

Edit: output also names of the textures:

filepath = r"E:/output.csv"

import bpy
from bpy import data as D

with open(filepath, 'w') as f:
    for ob in D.objects:
#        if ob.type != 'MESH': continue
        materials = [s.material for s in ob.material_slots if s.material]
        names = [
            (m.name, m.node_tree.nodes['Image Texture'].image.name)
            for m in materials if m.node_tree
        ]
        names = sum(names, ())  # flatten [(1, 2), (3, 4)] -> (1, 2, 3, 4)
        print(ob.name, *names, sep=",", file=f)
$\endgroup$
10
  • $\begingroup$ wau, this is great. I like the second script, as this is giving me also mesh without any material. I would need one more thing in this script please. It is exporting the mesh with all its materials as expected. Can you please expand the script so it will export also the texture name used in the material? Thank you very much! $\endgroup$
    – Miro
    Commented Oct 10, 2022 at 11:18
  • $\begingroup$ @Miro currently the structure is, first column: object/mesh name, next columns, material names. With texture names it's not obvious what the structure should be... $\endgroup$ Commented Oct 10, 2022 at 13:59
  • $\begingroup$ Hi Markus, can the struct be for example: first column - Object/mesh, 2nd column - 1st material name, 3rd column - texture name of 1st material, 4th column next material then its texture, etc? Or can you maybe change the material names in your second script for the texture names in those materials pelase? I could then easily combine the 2 csv files into one. So first column: object/mesh, next columns texture names. $\endgroup$
    – Miro
    Commented Oct 10, 2022 at 14:19
  • $\begingroup$ @Miro what if a material has multiple image textures, or none? $\endgroup$ Commented Oct 10, 2022 at 15:20
  • 1
    $\begingroup$ @ Markus. It works perfectly! I have used the script as debugging tool too, to find materials without texture. The script did stop always before badly mapped mesh. Thank you Markus very much!!! $\endgroup$
    – Miro
    Commented Oct 11, 2022 at 10:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .