How can I check if the Active Object has a specific modifier so that when I run my code, it doesn't add the modifier again to the object? It should add the modifier only to the object which does not use that modifier.
Asked
Active
Viewed 4,587 times
1 Answers
8
Check object for modifiers
import bpy
obj = bpy.context.object
if not obj.modifiers:
print ("no modifiers")
else:
print ("object has modifier(s)")
Check object for specific modifiers
import bpy
obj = bpy.context.object
for modifier in obj.modifiers:
if modifier.type == "SUBSURF":
print ("subsurf")
For all types see: http://www.blender.org/api/blender_python_api_current/bpy.types.Modifier.html?highlight=subsurf#bpy.types.Modifier.type
In your case I'd recommend using a list comprehension which returns True or False:
print(0 < len([m for m in bpy.context.object.modifiers if m.type == "SUBSURF"]))
or Python's any() plus the list comprehension.
print(any([m for m in bpy.context.object.modifiers if m.type == "SUBSURF"]))
Adding the Modifier if it isn't present
import bpy
obj = bpy.context.active_object
subsurf = obj.modifiers.new(name='MySubSurf', type='SUBSURF')
subsurf.levels = 2
subsurf.render_levels = 3
