1
$\begingroup$

I have a set of meshes loaded to the scene.

When I execute following script I wrote, the modifiers collection of the obj always stays empty. Where is my error?

import bpy
for obj in bpy.data.objects:
    print(obj)
    bpy.context.scene.objects.active = obj
    print(bpy.context.scene.objects.active)
    bpy.ops.object.modifier_apply(modifier='DECIMATE')
    print(len(obj.modifiers))

Note that I check if the active object was set, and the first two prints always print exactly the same. But the print(len(...)) always prints a 0 and in the UI the modifier does not show up.

All solutions for similar problems I found only suggest to set the active object which I did.

$\endgroup$
3
  • $\begingroup$ You're using the wrong operator on line 6 (bpy.ops.object.modifier_apply). You should be using, as you point out in the title of this question, bpy.ops.object.modifier_add. $\endgroup$
    – TLousky
    Dec 3 '15 at 9:41
  • $\begingroup$ I'll be damned... I've read the script may times and always missed that. Thanks, now it works. $\endgroup$
    – Dragonseel
    Dec 3 '15 at 9:42
  • $\begingroup$ Alternatively use mod = obj.modifiers.new("Decimod", 'DECIMATE') and set modifier props mod.ratio = 0.8 $\endgroup$
    – batFINGER
    Dec 3 '15 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.