5
$\begingroup$
import bpy 

for material_slot in bpy.data.objects['Cube'].material_slots:
    material_slot.material.show_transparent_back = False

I have managed to access to the data of one object by name in Blender 2.93, but I cannot find a way to do it for all selected objects instead of by name.

$\endgroup$

1 Answer 1

8
$\begingroup$

Iterate through all objects in selection, iterate through all material slots using a nested loop to get the material reference and finally set the property:

import bpy

C = bpy.context

# Iterate through all objects in selection
for ob in C.selected_objects:
    if ob.type not in ('LIGHTS', 'CAMERA', 'VOLUME'): # OPTIONAL
        # Iterate through all material slots
        for slot in ob.material_slots:
            if slot.material is not None:
                # Set the property
                slot.material.show_transparent_back = False
                slot.material.diffuse_color = (1.0, 0.0, 0.0, 1.0)
                slot.material.use_backface_culling = True
                # ... whatever

In case some materials are assigned to several objects, you can also create a set out of unique materials and assign the value per item of the set instead of iterating through the same slots over and over again which will improve performance (thanks to @batFINGER).

import bpy

C = bpy.context

# Create the set
mats = set(slot.material for o in C.selected_objects for slot in o.material_slots)

# Iterate through all unique materials
for mat in mats:
    # Set the property
    mat.show_transparent_back = False
    mat.diffuse_color = (0.0, 1.0, 0.0, 1.0)
    mat.use_backface_culling = True
    # ... whatever

Related

$\endgroup$
4
  • 2
    $\begingroup$ Couldn't find link, going to be a pest and suggest set(filter(slot.material for o in context.selected_objects for slot in o.material_slots)) will return a set of all materials used by selected objects, 1000 objects could all use same material, in which case it there is no need to set same prop 1000 times. All objects have a material_slots collection. $\endgroup$
    – batFINGER
    Commented Jun 18, 2021 at 9:15
  • 1
    $\begingroup$ All ideas to improve it are welcome, thanks @batFINGER $\endgroup$
    – brockmann
    Commented Jun 18, 2021 at 9:58
  • 2
    $\begingroup$ For the one liner lovers : [setattr(m, "show_transparent_back", False) for m in (set(filter(lambda m: m is not None, (slot.material for o in bpy.context.selected_objects for slot in o.material_slots))))] $\endgroup$
    – Gorgious
    Commented Jun 18, 2021 at 13:13
  • 1
    $\begingroup$ Cool, got much love to spread in this case @Gorgious $\endgroup$
    – brockmann
    Commented Jun 18, 2021 at 13:15

You must log in to answer this question.

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