3
$\begingroup$

Main Problem

I work on some specific script which generated list - creates new CSV file and I try to attach information about name materials so name would fit to name of object.

I am pretty new in scripting so please help me understand that. I have found many similar topics, but I just can't use it as I need; (like material index name or number, or object data, or active material) :/

I try find that in internet, but I can't find like this. Closest to my need is:

context = bpy.context


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


for mesh in mesh_objs:
    
    print("active_slot:", mesh.active_material)

And that generates this:

active_slot: <bpy_struct, Material("MaterialOne") at 0x000001EB95BE5008>
active_slot: <bpy_struct, Material("MaterialOne") at 0x000001EB95BE4A08>
active_slot: <bpy_struct, Material("MaterialOne") at 0x000001EB95BE5008>
active_slot: <bpy_struct, Material("MaterialTwo") at 0x000001EB95BE5008>
active_slot: <bpy_struct, Material("MaterialOne") at 0x000001EB95BE5008>
active_slot: <bpy_struct, Material("MaterialTwo") at 0x000001EB95BE5008>

But I need something like this (it can look different, this is no problem), for example:

  • of course only selected Objects
  • situation when you have more then 2 materials on slot 1, and on slot 2 is basically one or none
MaterialsList
Cube.001, MaterialOne and DiffrentMaterialOne
Cube.002, MaterialTwo and None
Cube.003, MaterialOne and None
Cube.004, MaterialTwo and None
Cube.005, MaterialOne and DiffrentMaterialOne
Cube.006, MaterialTwo and DiffrentMaterialOne

Let me know if it is possible.

$\endgroup$
3
  • 1
    $\begingroup$ Hello ! You can't have more than one material per slot, could you elaborate on what you are trying to do ? (the end goal and the input objects) $\endgroup$
    – Gorgious
    Jan 3, 2022 at 15:02
  • 1
    $\begingroup$ StackExchange rules require one question per question, so you should probably make additional questions for your second and third questions. Have them point back to this one for details. $\endgroup$ Jan 3, 2022 at 15:54
  • $\begingroup$ Hello and welcome. Please don't ask more than one question per post. Use the edit below, to break this into multiple posts so that each focuses on a single issue. Make as many separate questions as necessary. $\endgroup$ Jan 3, 2022 at 15:58

1 Answer 1

3
$\begingroup$

If you are serious about learning how to script, I strongly suggest you watch the video tutorial series Scripting for Artists from Blender Studio.

I will answer your first question, but as has been pointed out in the comments, you need to split the other two questions into separate posts.

Your original Python fragment is very close to what you need. But you need to append .name to mesh.active_material to print the name instead of the Python representation of the object:

import bpy

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

for mesh in mesh_objs:
    print("active_slot:", mesh.active_material.name)

But since you want all of the materials associated with the object, there's a simple way to do so. mesh.material_slots is a list of all of the material slots assigned to a material. So you can use a simple loop to print them all:

for mesh in mesh_objs:
    for slot in mesh.material_slots:
        print(slot.name)

But that prints the slots on separate lines, and it prints a blank line if the object has an empty slot.

If you don't want the print to add a newline, python has an optional argument to print, that says what to end the print with instead of the new line.

So here's some code that uses that option, and by the way includes the name of the mesh object in the output:

for mesh in mesh_objs:
    print(mesh.name, end = ", ")
    for slot in mesh.material_slots:
        print(slot.name, end = ", ")
    print()

Notice the additional print statement in the for loop? That's because the previous print statements had not terminated the line.

This code doesn't print exactly what you want. It has (at least) four problems:

  1. It uses ", " rather than "and " between the materials
  2. It adds an extra ", "
  3. It doesn't print "None" if the material slot is empty
  4. It doesn't stop at the 2nd material. It will print every material associated with your object.

There may be others, but they may be problems you won't care about or encounter.

Now I would argue that the last problem isn't a problem. You seem to be limiting things that don't need to be limited.

I leave you the first two problems as exercises. The first one is trivial, and having seen how end = works in print you should have no problem with it. The third problem is solved with an if statement that prints something different if the slot is empty. That should be easy to figure out.

The last one also requires changing the logic of the code. There are many ways to fix it, but I leave it to you to pick one.

Finally, there's an issue with the way you've described the problem. As mentioned in the comments on your question, a material slot can only have one material in it. I assumed that that was an error in the description and wrote the code and explanation using that assumption.

As I mentioned earlier, I will only answer the first question here. Please ask additional questions for the 2nd and 3rd points.

$\endgroup$

You must log in to answer this question.

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