2
$\begingroup$

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()
$\endgroup$
2
$\begingroup$

Thanks to this answer on blenerartists I now know what my error was.

In order to properly convert a metaball into a mesh you have to look at the evaluated metaball. The correct code looks like this:

# 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
depsgraph = context.evaluated_depsgraph_get()
mball_mesh = mball_obj.evaluated_get(depsgraph).to_mesh()

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

# 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()
$\endgroup$
0

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.