# Is there a low level alternative for bpy.ops.object.convert()?

I am using bpy.ops.object.convert(target='MESH', keep_original= False) in a python function in blender to convert metaballs to a mesh. The script works fine when run from the text editor. However, the goal is to add the function to the render events via bpy.app.handlers.render_pre.append() and bpy.app.handlers.render_post.append()

When the function using bpy.ops.object.convert is used in the handler, a polling error occurs. The typical recommendation is to either use overrides or low-level functions to eliminate the polling/context error. I would like to use the low-level functions.

A code snippet in the Blender 2.75a low-level API for bpy.ops.object.convert(target='MESH', keep_original= False), where the selected object is a meatball, would be appreciated.

The to_mesh method

import bpy

context = bpy.context
scene = context.scene

mball = bpy.data.objects.get("Mball")

if mball:
me = mball.to_mesh(scene, False, 'PREVIEW')

o = bpy.data.objects.new("MBallMesh", me)
o.matrix_world = mball.matrix_world

# not keep original


I had the similar problem. And to_mesh did not help me, it still didn't work. I imported some curves and then tried to convert them to mesh and exception related to incorrect context occurred. After short investigation I discover that correct context for convert function means existence not-None active_object and not-None selected_object in bpy.context. So the following code should work for conversion from/to mesh:

obj = D.objects[0] # the sample
bpy.context.scene.objects.active = obj
obj.select = True
bpy.ops.object.convert.poll() # should return True now


I wanted to add that if you create the metaballs programmatically as in

metaball = bpy.data.metaballs.new('MetaBall')
obj = bpy.data.objects.new('MetaBallObject', metaball)

element = metaball.elements.new()
element.co = (0, 0, 0)

you need to add bpy.context.scene.update() after you created the metaballs. Then you can
mesh = obj.to_mesh(bpy.context.scene, False, 'PREVIEW')