Applying modifiers without having to copy the object

I am writing an exporter plugin. Before exporting, the existing modifiers should be applied to the objects (like subdivision surface, etc.). However, modifiers like Armature should not get applied.

The old version of the plugin copies the whole scene, applies the modifiers to the objects, and then removes that scene. However, this already produced many issues, e.g. if there occurs an error the copied scene will stay, effectively duplicating all materials and objects.

I already had a look at the to_mesh function. However, that only provides the option to either apply all modifiers or none, which is not what I want.

Is there a different way to apply only a specific set of modifiers without having to copy the object/scene?

Not entirely true, it will only apply the modifiers whic are enabled in viewport or render, depending on an option.

Turn off the preview or render settings of modifiers you don’t want. Like you said, you can turn off the armature modifier.


ob.modifiers['Armature'].show_render = False

or


ob.modifiers['Armature'].show_viewport = False

Then use the ob.to_mesh()
apply_modifiers = True
settings = ‘PREVIEW’
http://www.blender.org/api/blender_python_api_2_74_release/bpy.types.Object.html?highlight=to_mesh#bpy.types.Object.to_mesh

Then when the export is done, turn the modifiers back on. With python of course!

Thanks, I gonna try that! There is still the issue that when the script crashes, the modifiers would still be disabled in render mode I guess. Thats definitely a smaller issue than having a lot of old scene copies, tho.

You can try/except the entire operation and tell the user about the error, but always revert the modifier settings to the original state in the finally-block.