Alternative to bpy.ops.object.convert(target='MESH') command?

Hi,

I’m trying to convert several curves into mesh.
The info panel returns the code bpy.ops.object.convert(target='MESH').

However, I’ve been told (like in this thread) that bpy.ops are evil and must be used in the last resort.

So, I was wondering is there a way to call it using the bpy.data? I tried the autocomplete for methods in the console but the closest I got for “convert” is “copy”.

Is there a better way around this? Or I should just make peace with it?

Thank you for looking at my problem

I would really be interested in this too.
If anyone has a solution. I have none, sorry.

Best regards!

For curves

This assumes you already have a curve selected.

import bpy

def curve_to_mesh(context, curve):
    deg = context.evaluated_depsgraph_get()
    me = bpy.data.meshes.new_from_object(curve.evaluated_get(deg), depsgraph=deg)

    new_obj = bpy.data.objects.new(curve.name + "_mesh", me)
    context.collection.objects.link(new_obj)

    for o in context.selected_objects:
        o.select_set(False)

    new_obj.matrix_world = curve.matrix_world
    new_obj.select_set(True)
    context.view_layer.objects.active = new_obj

if __name__ == '__main__':
    context = bpy.context
    obj = context.object

    if obj and obj.type == 'CURVE':
        curve_to_mesh(context, obj)

For meshes (collapse modifiers)

import bpy

def mesh_eval_to_mesh(context, obj):
    deg = context.evaluated_depsgraph_get()
    eval_mesh = obj.evaluated_get(deg).data.copy()
    new_obj = bpy.data.objects.new(obj.name + "_collapsed", eval_mesh)

    context.collection.objects.link(new_obj)

    for o in context.selected_objects:
        o.select_set(False)

    new_obj.matrix_world = obj.matrix_world
    new_obj.select_set(True)
    context.view_layer.objects.active = new_obj


if __name__ == '__main__':
    context = bpy.context
    obj = context.object

    if obj and obj.type == 'MESH':
        mesh_eval_to_mesh(context, obj)

Edit: unnecessary bmesh

3 Likes

Thank you, that works perfectly well!

Thanks, I’ve used part of your code for an animation nodes script and it works flawlessly !