To select all objects in Blender with the desired modifier appended using the Blender Python API, we need to loop through all the objects in the scene, check for the presence of the desired modifier and set the object selection flag to True.
Select objects with a modifier by its name
Loop through all the objects in the scene:
1 |
for obj in bpy.data.objects: |
If the object meets our condition – it is a mesh, at least one modifier is appended to it, and the desired modifier name is present in the list of modifiers (for example, for the Subdivision Surface modifier):
1 |
if obj.type == 'MESH' and obj.modifiers and 'Subdivision' in obj.modifiers: |
And select the object:
1 |
obj.select_set(True) |
Full code:
1 2 3 4 5 |
for obj in bpy.data.objects: if obj.type == 'MESH' and \ obj.modifiers and \ 'Subdivision' in obj.modifiers: obj.select_set(True) |
The same in a single line:
1 |
[obj.select_set(True) for obj in bpy.context.blend_data.objects if obj.type == 'MESH' and obj.modifiers and 'Subdivision' in obj.modifiers] |
Select objects with a modifier by its type
The name of the modifier can be changed by the user, so it is not always correct to use it in search. Instead of a modifier name, it’s better to use its type.
The modifier’s type can be found from its “type” property.
1 2 |
bpy.context.object.modifiers['Subdivision'].type # 'SUBSURF' |
The same code for selecting all objects with the Subdivision Surface modifier but by its type will look like the following:
1 2 3 4 5 |
for obj in bpy.data.objects: if obj.type == 'MESH' and \ obj.modifiers and \ 'SUBSURF' in [mod.type for mod in obj.modifiers]: obj.select_set(True) |
And the same in a single line:
1 |
[obj.select_set(True) for obj in bpy.context.blend_data.objects if obj.type == 'MESH' and obj.modifiers and 'SUBSURF' in [mod.type for mod in obj.modifiers]] |