This should probably be moved to scripting forum, but because we started all this here, let me answer it here as well.
Below you’ll find the script that will do exactly what you want.
It requires understanding of the structure of blender’s API. Not all functions can be called from any place, so sometimes we need to use some tricks.
import bpy
scn = bpy.context.scene
active = scn.objects.active
for ob in [o for o in bpy.context.selected_objects if o.type == 'MESH']:
scn.objects.active = ob
bpy.ops.object.modifier_add(type='DECIMATE')
ob.modifiers["Decimate"].ratio = 0.1
scn.objects.active = active
and now let me go line by line explaining what and why is done.
import bpy
obvious… we simply import blender’s python module to be able to control blender via python.
scn = bpy.context.scene
just for simplification. I want to later just say “scn” instead of “bpy.context.scene” every time I want to refer to the scene I am working on.
active = scn.objects.active
“active” will from now on be the object that is active. Later on the script will make other objects active, so I simply store currently active object to make it active again after all operations are executed.
for ob in [o for o in bpy.context.selected_objects if o.type == 'MESH']:
scn.objects.active = ob
Here I iterate through all selected objects making sure that only mashes will be taken into account and begin by making the object active.
Adding modifier can be done only for the active object. It doesn’t matter if it’s selected or not.
bpy.ops.object.modifier_add(type='DECIMATE')
then I add the modifier. Please note the correct operator for adding modifiers.
ob.modifiers["Decimate"].ratio = 0.1
Now I set “ratio” for the modifier. As you can see I access the modifier that already exists (added in previous step) in completely different manner. I access it through the object.
scn.objects.active = active
The last line restores the state before executing the script. I make the object that was active at the beginning active again.