5
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.

Gorgious
  • 30,723
  • 2
  • 44
  • 101
pekkuskär
  • 307
  • 2
  • 11

1 Answers1

8

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

brockmann
  • 12,613
  • 4
  • 50
  • 93
  • 2
    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. – batFINGER Jun 18 '21 at 09:15
  • 1
    All ideas to improve it are welcome, thanks @batFINGER – brockmann Jun 18 '21 at 09:58
  • 2
    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))))] – Gorgious Jun 18 '21 at 13:13
  • 1
    Cool, got much love to spread in this case @Gorgious – brockmann Jun 18 '21 at 13:15