# Using object.convert function after creating object

I'm creating an object from code

obj = bpy.data.objects.new("ObjectName", SomeMeshData)


Now, i want to convert this object into a Curve, using blender console i can see it's quite simple: bpy.ops.object.convert(target='CURVE') from code this doesn't work causing: RuntimeError: Operator bpy.ops.object.convert.poll()

Using obj.convert(target='CURVE') causes different error:

AttributeError: 'Object' object has no attribute 'convert'

Should i 'select' the object and then use this function? This doesn't sound like proper approach cause i want to do this for thousands of objects and i also tried selecting and couldn't make it working.

New Blender 2.8 API is so confusing, after hours of searching all forums i can not find any way to make it work. This should be fairly easy task but seems to be impossible for some reason. I'm a newbie, please help.

Ok, so i found the answer:

Since bpy.ops.object.convert() needs a context the only way i managed it to work is:

obj.select_set(True)
bpy.context.view_layer.objects.active = obj
bpy.ops.object.convert(target='CURVE')


Though, it's quite weird that i have to 'manually' select an object to be able to use it, why not just obj.convert()? I'm not sure if method i used is the only one possible but i tried A LOT of stuff.

If there is a better, more elegant and robust method, please let me know!

Blender API changes so fast that it took me couple of hours to find the answer, even Blender 2.81 Docs were saying

bpy.context.object = obj will raise an error.

But

bpy.context.scene.objects.active = obj will work as expected.

While bpy.context.scene.objects.active is already deprecated!