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

by Ed Tate   Last Updated April 16, 2018 11:15 AM

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.



Answers 3


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

    # add an object
    o = bpy.data.objects.new("MBallMesh", me)
    scene.objects.link(o)
    o.matrix_world = mball.matrix_world

    # not keep original
    scene.objects.unlink(mball)
batFINGER
batFINGER
October 12, 2015 03:21 AM

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
Vladislav
Vladislav
December 21, 2016 22:37 PM

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)
bpy.context.scene.objects.link(obj)

element = metaball.elements.new()
element.co = (0, 0, 0)
element.radius = 1.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')
new_obj = bpy.data.objects.new('MetaBallMeshObject', mesh)
bpy.context.scene.objects.link(new_obj)

otherwise you won't have any geometry.

Nikolai Janakiev
Nikolai Janakiev
April 16, 2018 11:01 AM

Related Questions



How to create a claw out of this mesh?

Updated September 15, 2017 23:15 PM


Baking metaball solver

Updated April 07, 2015 18:06 PM

Meta balls have no interior faces

Updated March 16, 2017 06:15 AM