3
$\begingroup$

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()

$\endgroup$
6
$\begingroup$

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.
$\endgroup$
  • $\begingroup$ just wanted to follow up that to_mesh works fine for this! Thanks $\endgroup$ – patmo141 Mar 25 '14 at 21:00
  • $\begingroup$ @patmo141 If this answer was helpful you should mark it as 'accepted'. See: stackoverflow.com/help/someone-answers $\endgroup$ – Ray Mairlot Feb 14 '16 at 16:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.