apply a modifier for all objects in a scene

Hi everybody!

A little question about scripting in blender 2.5

I would like to add a ‘SUBSURF’ modifier on each objects of the scene wich doesn’t have yet.

But my operator works only for the selected object (as expected).

Could you help me to find the right loop please?

def lowres():
    
    add_modifier = bpy.ops.object.modifier_add(type='SUBSURF')
    
    for objs in bpy.data.objects:
        
        for mod in objs.modifiers:
        
            if mod.type == 'MULTIRES':    

                mod.show_only_control_edges = True
                mod.levels = 0 
                mod.render_levels = 2          
                
            elif mod.type == 'SUBSURF':
                    
                mod.levels = 0 
                mod.render_levels = 2       
  
#HERE I WOULD APPLY MY MODIFIER (if object hasn't yet) 
#BUT WITH THIS CODE IT WORKS ONLY FOR THE CURRENT OBJECT.  
                          
            else:
                
                add_modifier()

It is probably going to be something like object.modifiers.new(type=‘SUBSURF’) or so. Check the API docs.

Hi!

The modifier actually works. It’s not my problem. But i would like to apply it not just on the selected object but all scene objects. But the operator works only on the selected object.
(If you selected 3 objects in viewport you can’t apply a subsurf modifier simultaneously - same problem with my script - I guess i have to select each object one by one)

Here you go:

import bpy

for n in bpy.context.scene.objects:
    if n.type == 'MESH':
        mod = n.modifiers.new('some name', type = 'SUBSURF')
        mod.levels = 2
        

# Silly way to force all windows to refresh
bpy.context.scene.frame_current = bpy.context.scene.frame_current

I always try to find out if there is a way to do a thing without using the viewport operators, as they always bitch about context and are not elegant to use.

Yes! that’s exactly what I was looking for. Thank you!!

Sorry to resurrect this topic.

The above example adds modifiers to all objects in the scene.

Besides adding, would apply the modifiers on all objects too?