3

I'm trying to achieve this:

I select all objects with the same type of modifier (Armature) and want to "click" Apply on all of them. I searched different scripts on here and badly put together this one:

import bpy
sel = bpy.context.selected_objects

for ob in sel:
    for i, mod in enumerate(ob.modifiers):
        if mod.type == 'ARMATURE':
            bpy.context.scene.objects.active = ob
                for x in range(0, i):
                    bpy.ops.object.modifier_apply(modifier=mod.name)

It will work for some of the selected objects then Blender will say "Python Script fail". It's obvious I don't know what I'm doing, but why is it failing after working at first? Thanks

EDIT: I found something that does exactly what I was trying to do. A user from Blender Artists named VincentG created an addon called "Massive Editor" from here. I selected all objects, typed Armature in the custom field and all objects that had that specific modifier applied. The script may be old, but it worked great. Thanks Vincent!

For the sake of learning though, I'd still want to know what went wrong...

UPDATE: While searching around I found this answer here and tried it out to see if it would work for me:

import bpy

sel = bpy.context.selected_objects
act = bpy.context.active_object

for obj in sel:
    if obj != act:
        bpy.context.scene.objects.active = obj #sets the obj accessible to bpy.ops
        bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Mirror")

bpy.context.scene.objects.active = act

It almost does what I would like but every time it would only apply to all selected objects except the active one.

Oblender
  • 43
  • 1
  • 6
  • As I understand this, you may have more than the modifier that you want to apply, and I'm not sure how to do that (hence a comment, rather than an answer). However, if you can live with having all modifiers applied, you could do Ctrl C followed by M with all the objects selected in object mode. Strictly speaking, this is used to convert meta balls, curves or text to meshes, but it also applies all modifiers, and it works just fine even when the selected objects are already meshes. –  Mar 31 '17 at 15:48
  • Thanks for the comment, but I have seen that option but unfortunately it cannot do :( – Oblender Mar 31 '17 at 16:14
  • What error are you getting? If you are on Windows you can go to Window> Toggle System Console to view the Python error. Otherwise you can look here: https://docs.blender.org/manual/en/dev/advanced/command_line/introduction.html – Ray Mairlot Mar 31 '17 at 17:29
  • What probably happens here is that you are looping over a list (obj.modifiers) but inside the list you delete (.modifier_apply) elements of the list. But as indicated @RayMairlot, you should have a look at the console. If that use "for i, mod in enumerate(ob.modifiers[:])" – lemon Apr 01 '17 at 05:02
  • I tried two ways, one with Armature modifiers at the bottom of the stack for all selected objects, and one with Armature modifiers at the top of the stack for all selected objects. The first method gave me this error:

    Info: Applied modifier was not first,...(This was said 4 times) ...line 9 (Which was bpy.ops.object.modifier_apply(modifier=mod.name)) ...UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte Error: Python script fail, look in the console for now...

    The second method just does nothing and console seems to just pause.

    – Oblender Apr 01 '17 at 07:46
  • @lemon I think I know what you mean. I figured it's looking through the list of objects, and keeps looking through them after each modifier_apply until there is none? – Oblender Apr 01 '17 at 07:54
  • @Oblender, I don't have this error here (but I have the warnings and this is normal). So I was wrong in my previous comment. But, what is the need of the "for x..." loop? Useless here, don't you think? – lemon Apr 01 '17 at 08:22
  • @lemon While I was searching here I found a script someone provided that loops through selected objects to move modifiers to specific slots. Instead of modifier_move_up I replaced it with modifier_apply. It worked fine moving, so my thinking was it should do the same for applying? To answer your question, I'm not sure if it needs the for x... thing lol. From the original script it seems to indicate the modifier stack position. I think what I'm looking for won't come from this script no matter how much I change or remove things. – Oblender Apr 01 '17 at 10:13

1 Answers1

3

Blender 2.79

This should work:

import bpy

for ob in bpy.context.selected_objects:
    bpy.context.scene.objects.active = ob
    for mod in [m for m in ob.modifiers if m.type == 'ARMATURE']:
        bpy.ops.object.modifier_apply( modifier = mod.name )

You may have some warnings though (like via the UI).

Blender 2.8

Use view_layer instead of scene:

import bpy

for ob in bpy.context.selected_objects:
    bpy.context.view_layer.objects.active = ob
    for mod in [m for m in ob.modifiers if m.type == 'ARMATURE']:
        bpy.ops.object.modifier_apply( modifier = mod.name )
lemon
  • 60,295
  • 3
  • 66
  • 136