# Select object by part of their material name

I have difficulties trying to select a bunch of object by part of their materialname. eg. i've got some objects and 2 of them have a material that contains the word "text". In the end I want to parent these object to an empty and then move them to a hidden layer.

i've tried the following code, but this gives errors.

textmat = [mat for mat in bpy.data.materials if fnmatch.fnmatchcase(mat.material, "*text*")]

obj = bpy.data.objects
for ob in obj:
if ob.type == 'MESH':
for m in ob.material_slots:
if m.material == textmat:
ob.select = False


i'm obviously doing something wrong, but my python skills are still limited

thanks!

In that case, even a simple condition should do the trick.

import bpy

obj_list = []

for o in bpy.data.objects:
for m in o.material_slots:
if "text" in m.name:
obj_list.append(o)

print (obj_list)


I suggest add the items to a set instead of a list — avoids adding multiple entries of identical objects (in case there are multiple materials per object containing text as substring in their name).

import bpy

obj_set = set()

for o in bpy.data.objects:
for m in o.material_slots:
if "test" in m.name:

• Perhaps append o , if len([m for m in o.material_slots if "text" in m.name]) to avoid multiples. – batFINGER Aug 15 '17 at 12:48