0
$\begingroup$

Topic starts here, so check it if you need more information about subject:

How to print material names (two materials) for selected object

Main Problem

I need generate list of selected object which will print: name of object, name material 1st index + 2nd index material will be checked on sides (this will be always cube).

Goal is to try make list like this:

#side on 1 dimension is checked with materialside - so max is 2 sides print = 2 (or 1)
#if is none - print = 0
#side on 2 dimension is checked with materialside - so max is 2 sides print = 2 (or 1)
#if is none - print = 0


Model; Material; Edges1Dimension(e.g. width); Edges2Dimesion(e.g. length)
Cube.001; Material 1; 2; 2
Cube.003; Material 2; 2; 0

Explain:

  • Cube.001 - object name
  • Material 1 - 1st index material of that selected object
  • 2, 2 - numbers of faces which is assigned material with 2nd index of that selected object. (for cube i define as: 2 fronts, and 4 sides [2 for dimiension x axis, and 2 for dimension y axis] )

Example file: enter image description here

Link to file presented up (1 week acces to download from now): https://we.tl/t-kbbkSjJlpg

Imported part for me is that could be translated into CSV (and open that file by Excel)- i have already list with dimension and names - now i want to add this next step.

Thank you for your time - best regards!

$\endgroup$
0

1 Answer 1

4
$\begingroup$

Once again, I shall give you a slight variation on your request, and leave the final adaptation as an exercise.

Here is code that prints the information you want, but not in precisely the format you want.

import bpy

mesh_objs = [ob for ob in bpy.context.selected_objects if ob.type == 'MESH']

def polys(mesh, index):
    n = 0
    for p in mesh.data.polygons:
        if p.material_index == index:
            n += 1
    return n

for mesh in mesh_objs:
    print(mesh.name, end = "; ")
    for index, slot in enumerate(mesh.material_slots):
        print(f"{slot.name}; {polys(mesh, index)}", end = "; ")
    print()

and here is a print out from your test file:

Cube.001;  Material 1 White; 2; Material Edge; 4; 
Cube.002;  Material 1 White; 2; Material Edge; 4; 
Cube;  Material 1 White; 4; Material Edge; 2; 
Cube 1.001;  Material 1 White; 2; Material Edge; 4; 
Cube 1.002;  Material 3 Red; 2; Material Edge; 4; 
Cube 1;  Material 3 Red; 4; Material Edge; 2; 
Cube 2;  Material 2 Green; 2; Material Edge; 4; 
Cube 2.001;  Material 1 White; 2; Material Edge; 4; 
Cube 2.002;  Material 1 White; 4; Material Edge; 2; 
Cube Rotate;  Material 2 Green; 2; Material Edge; 4; 
Cube Rotate.001;  Material 3 Red; 2; Material Edge; 4; 
Cube Rotate.002;  Material 1 White; 2; Material Edge; 4; 
$\endgroup$
1
  • $\begingroup$ Thank you! That looks like what i needed! I will try use this in my code and I will let you know if this work for me! Thank you! $\endgroup$ Jan 23, 2022 at 8:28

You must log in to answer this question.

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