3

I would like to apply all modifiers to an object with python.

for mod in ob.modifiers:
    bpy.ops.object.modifier_apply(modifier = mod.name)

This works fine, unless for some reason the modifier is disabled, e.g.:

  • A Boolean modifier with no object
  • A Shrinkwrap modifier with no target

General Problem:

Other than knowing all of the possible attributes of every modifier and checking to make sure the modifier is valid, is there a way to bpy.ops.object.modifier_apply.poll() while specifying which modifier I want to apply?

I see that the poll() method expects

poll(self, *args).

Perhaps I can put something in for *args which will help

Work Around: I can get the modified mesh object.to_mesh()

David
  • 49,291
  • 38
  • 159
  • 317
patmo141
  • 777
  • 7
  • 16

1 Answers1

7

The short answer is no, poll checks that the basic context is valid but it doesn't do extensive checks that every operation will succeed (It's often not trivial for tools to know this ahead of time)

You can simply use exception handling for this.

for mod in ob.modifiers:
    try:
        bpy.ops.object.modifier_apply(modifier=mod.name)
    except RuntimeError as ex:
        # print the error incase its important... but continue
        print(ex)

In this case I think you would be better off using,

  • bpy.ops.object.convert(target='MESH') - converting a mesh to a mesh will replace the mesh with a version that has modifiers applied.
  • Object.to_mesh() - gives you most control, uses Pythons internal API.
ideasman42
  • 47,387
  • 10
  • 141
  • 223