Converting metaball object to mesh in python returns empty mesh

In my script I’m trying to:

  • Create a metaball object (works)
  • Add metaball elements to it (works)
  • Convert it to a mesh (returns empty mesh)
  • Store the data in a bmesh (works, but is of course still empty)
  • Delete the metaball object (works)

I’d like to understand, why my mesh is always empty and to fix the underlying issue.

My source code:

# Create metaball object
mball = bpy.data.metaballs.new("TempMBall")
mball_obj = bpy.data.objects.new("TempMBallObj", mball)
context.view_layer.active_layer_collection.collection.objects.link(mball_obj)

# Add one element to it
ele = mball.elements.new()
ele.co = (0.0,0.0,0.0)
ele.use_negative = False
ele.radius = td.sk_base_radius / td.sk_min_radius

print([ele for ele in mball.elements])  # Make sure there is data in mball (there is)

# Convert it to a mesh
mball_mesh = mball_obj.to_mesh()
print([v for v in mball_mesh.vertices]) # Empty!

# Store copy of the mesh in a bmesh for further manipulation
bm = bmesh.new()
bm.from_mesh(mball_mesh)    # Still empty!

# Delete metaball object
bpy.data.objects.remove(mball_obj)
bpy.data.metaballs.remove(mball)

# My custom mesh manipulation and writing the bmesh into a different object

bm.free()

You need to get the mesh from the evaluated object.

Check the bpy.data.meshes.new_from_object() example for details.
https://docs.blender.org/api/current/bpy.types.Depsgraph.html?highlight=mesh_from_object
The same process applies to any geometry that results from modifiers.

1 Like